'Test to ensure that on down calls to clusters with connections still active don\'t result in
a host being marked down. The second part of the test kills the connection then invokes
on_down, and ensures the state changes for host\'s metadata.
@since 3.7
@jira_ticket PYTHON-498
@expected_result host should never be toggled down while a connection is active.
@test_category connection'

def test_down_event_with_active_connection(self):

    with Cluster(protocol_version=PROTOCOL_VERSION) as cluster:
        session = cluster.connect(wait_for_all_pools=True)
        random_host = cluster.metadata.all_hosts()[0]
        cluster.on_down(random_host, False)
        for _ in range(10):
            new_host = cluster.metadata.all_hosts()[0]
            self.assertTrue(new_host.is_up, 'Host    was    not    up    on    iteration    {0}'.format(_))
            time.sleep(0.01)
        pool = session._pools.get(random_host)
        pool.shutdown()
        cluster.on_down(random_host, False)
        was_marked_down = False
        for _ in range(20):
            new_host = cluster.metadata.all_hosts()[0]
            if (not new_host.is_up):
                was_marked_down = True
                break
            time.sleep(0.01)
        self.assertTrue(was_marked_down)
