def test_pool_size(self): POOL_SIZE = 1 pool = HTTPConnectionPool(host='localhost', maxsize=POOL_SIZE, block=True) def _raise(ex): raise ex() def _test(exception, expect): pool._make_request = lambda *args, **kwargs: _raise(exception) self.assertRaises(expect, pool.request, 'GET', '/') self.assertEqual(pool.pool.qsize(), POOL_SIZE) # Make sure that all of the exceptions return the connection to the pool _test(Empty, EmptyPoolError) _test(BaseSSLError, SSLError) _test(CertificateError, SSLError) # The pool should never be empty, and with these two exceptions being raised, # a retry will be triggered, but that retry will fail, eventually raising # MaxRetryError, not EmptyPoolError # See: https://github.com/shazow/urllib3/issues/76 pool._make_request = lambda *args, **kwargs: _raise(HTTPException) self.assertRaises(MaxRetryError, pool.request, 'GET', '/', retries=1, pool_timeout=0.01) self.assertEqual(pool.pool.qsize(), POOL_SIZE)
def _test(exception): pool = HTTPConnectionPool(host='localhost', maxsize=1, block=True) # Verify that the request succeeds after two attempts, and that the # connection is left on the response object, instead of being # released back into the pool. pool._make_request = _raise_once_make_request_function(exception) response = pool.urlopen('GET', '/', retries=1, release_conn=False, preload_content=False, chunked=True) self.assertEqual(pool.pool.qsize(), 0) self.assertEqual(pool.num_connections, 2) self.assertTrue(response.connection is not None) response.release_conn() self.assertEqual(pool.pool.qsize(), 1) self.assertTrue(response.connection is None)