def test_regenerating_connections_tolerates_exceptions(self, connect_mock): num_failures = 20 pool = TCPConnectionPool('foobar', 80, 10, 1, 1) mock_sock = FakeSocket(set_failures=num_failures) connect_mock.return_value = mock_sock # Simulate a prior failure pool.pool = Queue() pool.size = 1 pool.pool._put(_DefunctConnection) # Try a number of failed operations. # Each time the defunct connection should be returned to the queue # for subsequent calls to reattempt. for i in range(num_failures): with pytest.raises(Exception): with pool.connection(): assert False # Should not be reached assert pool.pool.qsize() == pool.size == 1 assert pool.pool.get_nowait() is _DefunctConnection pool.pool._put(_DefunctConnection) # Failures are exhausted, we should be able to now # regenerate a valid context. with pool.connection(): pass assert pool.pool.qsize() == 1 assert pool.pool.get_nowait() is mock_sock
def test_defunct_connections_are_regenerated(self, connect_mock): pool = TCPConnectionPool('foobar', 80, 10, 1, 1) mock_sock = FakeSocket() connect_mock.return_value = mock_sock pool.pool = Queue() pool.pool.put(_DefunctConnection) with pool.connection() as conn: assert conn is mock_sock