Improve CI stability for timeouts and branch coverage
MODIFY
src/urllib3/connection.py
src/urllib3/connection.py
@@ -19,10 +19,11 @@ except (ImportError, AttributeError): # Platform-specific: No SSL.
pass
-try: # Python 3:
- # Not a no-op, we're adding this to the namespace so it can be imported.
+try:
+ # Python 3: not a no-op, we're adding this to the namespace so it can be imported.
ConnectionError = ConnectionError
-except NameError: # Python 2:
+except NameError:
+ # Python 2
class ConnectionError(Exception):
pass
@@ -101,7 +102,7 @@ class HTTPConnection(_HTTPConnection, object):
is_verified = False
def __init__(self, *args, **kw):
- if six.PY3: # Python 3
+ if six.PY3:
kw.pop('strict', None)
# Pre-set source_address.

MODIFY
src/urllib3/connectionpool.py
src/urllib3/connectionpool.py
@@ -373,9 +373,11 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
# Receive the response from the server
try:
- try: # Python 2.7, use buffering of HTTP responses
+ try:
+ # Python 2.7, use buffering of HTTP responses
httplib_response = conn.getresponse(buffering=True)
- except TypeError: # Python 3
+ except TypeError:
+ # Python 3
try:
httplib_response = conn.getresponse()
except Exception as e:

MODIFY
src/urllib3/response.py
src/urllib3/response.py
@@ -538,9 +538,10 @@ class HTTPResponse(io.IOBase):
headers = r.msg
if not isinstance(headers, HTTPHeaderDict):
- if PY3: # Python 3
+ if PY3:
headers = HTTPHeaderDict(headers.items())
- else: # Python 2
+ else:
+ # Python 2.7
headers = HTTPHeaderDict.from_httplib(headers)
# HTTPResponse objects in Python 3 don't have a .strict attribute

MODIFY
src/urllib3/util/timeout.py
src/urllib3/util/timeout.py
@@ -131,7 +131,8 @@ class Timeout(object):
raise ValueError("Attempted to set %s timeout to %s, but the "
"timeout cannot be set to a value less "
"than or equal to 0." % (name, value))
- except TypeError: # Python 3
+ except TypeError:
+ # Python 3
raise ValueError("Timeout value %s was %s, but it must be an "
"int, float or None." % (name, value))

MODIFY
test/with_dummyserver/test_connectionpool.py
test/with_dummyserver/test_connectionpool.py
@@ -214,6 +214,8 @@ class TestConnectionPoolTimeouts(SocketDummyServerTestCase):
self.assertRaises(ReadTimeoutError, pool.request, 'GET', '/')
def test_create_connection_timeout(self):
+ self.start_basic_handler(block_send=Event(), num=0) # needed for self.port
+
timeout = Timeout(connect=SHORT_TIMEOUT, total=LONG_TIMEOUT)
pool = HTTPConnectionPool(TARPIT_HOST, self.port, timeout=timeout, retries=False)
self.addCleanup(pool.close)

MODIFY
test/with_dummyserver/test_https.py
test/with_dummyserver/test_https.py
@@ -507,7 +507,7 @@ class TestHTTPS(HTTPSDummyServerTestCase):
self.addCleanup(https_pool.close)
self.assertRaises(ConnectTimeoutError, https_pool.request, 'GET', '/')
- timeout = Timeout(read=0.001)
+ timeout = Timeout(read=0.01)
https_pool = HTTPSConnectionPool(self.host, self.port,
timeout=timeout, retries=False,
cert_reqs='CERT_REQUIRED')

MODIFY
test/with_dummyserver/test_socketlevel.py
test/with_dummyserver/test_socketlevel.py
@@ -371,7 +371,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
self._start_server(socket_handler)
http = HTTPConnectionPool(self.host, self.port,
- timeout=0.001,
+ timeout=0.01,
retries=False,
maxsize=3,
block=True)
@@ -394,7 +394,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
sock.close()
self._start_server(socket_handler)
- pool = HTTPConnectionPool(self.host, self.port, timeout=0.001, retries=True)
+ pool = HTTPConnectionPool(self.host, self.port, timeout=0.01, retries=True)
self.addCleanup(pool.close)
try:
@@ -415,7 +415,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
sock.close()
self._start_server(socket_handler)
- pool = HTTPSConnectionPool(self.host, self.port, timeout=0.001, retries=False)
+ pool = HTTPSConnectionPool(self.host, self.port, timeout=0.01, retries=False)
self.addCleanup(pool.close)
try:
self.assertRaises(ReadTimeoutError, pool.request, 'GET', '/')
@@ -454,7 +454,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
try:
self._start_server(socket_handler)
- t = Timeout(connect=0.001, read=0.001)
+ t = Timeout(connect=0.001, read=0.01)
pool = HTTPConnectionPool(self.host, self.port, timeout=t)
self.addCleanup(pool.close)
@@ -487,7 +487,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
self.addCleanup(pool.close)
response = pool.urlopen('GET', '/', retries=0, preload_content=False,
- timeout=Timeout(connect=1, read=0.001))
+ timeout=Timeout(connect=1, read=0.01))
try:
self.assertRaises(ReadTimeoutError, response.read)
finally:
@@ -517,7 +517,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
try:
self.assertRaises(ReadTimeoutError, pool.urlopen,
'GET', '/', retries=False,
- timeout=Timeout(connect=1, read=0.001))
+ timeout=Timeout(connect=1, read=0.01))
finally:
timed_out.set()
@@ -614,7 +614,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
with HTTPConnectionPool(self.host, self.port) as pool:
poolsize = pool.pool.qsize()
response = pool.urlopen('GET', '/', retries=0, preload_content=False,
- timeout=Timeout(connect=1, read=0.001))
+ timeout=Timeout(connect=1, read=0.01))
try:
self.assertRaises(ReadTimeoutError, response.read)
self.assertEqual(poolsize, pool.pool.qsize())
@@ -712,7 +712,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
# Second should succeed.
response = pool.urlopen('GET', '/', retries=0,
preload_content=False,
- timeout=Timeout(connect=1, read=0.1))
+ timeout=Timeout(connect=1, read=1))
self.assertEqual(len(response.read()), 8)
def test_closing_response_actually_closes_connection(self):
@@ -803,7 +803,7 @@ class TestSocketClosing(SocketDummyServerTestCase):
# save it.
response = pool.urlopen('GET', '/', retries=1,
release_conn=False, preload_content=False,
- timeout=Timeout(connect=1, read=0.001))
+ timeout=Timeout(connect=1, read=0.01))
# The connection should still be on the response object, and none
# should be in the pool. We opened two though.
@@ -1093,7 +1093,7 @@ class TestSSL(SocketDummyServerTestCase):
self.addCleanup(pool.close)
response = pool.urlopen('GET', '/', retries=0, preload_content=False,
- timeout=Timeout(connect=1, read=0.001))
+ timeout=Timeout(connect=1, read=0.01))
try:
self.assertRaises(ReadTimeoutError, response.read)
finally:
@@ -1127,7 +1127,7 @@ class TestSSL(SocketDummyServerTestCase):
assert_fingerprint=fingerprint)
try:
response = pool.urlopen('GET', '/', preload_content=False,
- timeout=Timeout(connect=1, read=0.001),
+ timeout=Timeout(connect=1, read=0.01),
retries=0)
response.read()
finally:

Improve CI stability for timeouts and branch coverage (#1554)