Skip to content

Commit 0ba2423

Browse files
author
Martin Panter
committed
Ensure ECONNREFUSED does not trigger a retry
1 parent 0fc2243 commit 0ba2423

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

test.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
from io import BytesIO, TextIOWrapper, BufferedReader
1010
from iview.utils import fastforward
11+
from errno import ECONNREFUSED
1112

1213
try: # Python 3.4
1314
from importlib import reload
@@ -242,9 +243,14 @@ def test_close_error(self):
242243
"Server handle() retried for POST")
243244

244245
class TestMockHttp(TestPersistentHttp):
246+
def run(self, *pos, **kw):
247+
with substattr(iview.utils.http.client, self.HTTPConnection):
248+
return TestPersistentHttp.run(self, *pos, **kw)
249+
250+
class TestHttpSocket(TestMockHttp):
245251
class HTTPConnection(http.client.HTTPConnection):
246252
def connect(self):
247-
self.sock = TestMockHttp.Socket(
253+
self.sock = TestHttpSocket.Socket(
248254
b"HTTP/1.1 200 First response\r\n"
249255
b"Content-Length: 12\r\n"
250256
b"\r\n"
@@ -267,10 +273,6 @@ def close(self, *pos, **kw):
267273
def makefile(self, *pos, **kw):
268274
return self.reader
269275

270-
def run(self, *pos, **kw):
271-
with substattr(iview.utils.http.client, self.HTTPConnection):
272-
return TestPersistentHttp.run(self, *pos, **kw)
273-
274276
def test_reuse(self):
275277
"""Test existing connection is reused"""
276278
with self.session.open("http://localhost/one") as response:
@@ -297,6 +299,30 @@ def test_new_host(self):
297299
self.assertIsNot(sock1, sock2, "Expected new socket connection")
298300
self.assertTrue(sock2.reader, "Disconnected after second request")
299301

302+
class TestHttpEstablishError(TestMockHttp):
303+
"""Connection establishment errors should not trigger a retry"""
304+
class HTTPConnection(http.client.HTTPConnection):
305+
def __init__(self, *pos, **kw):
306+
self.connect_count = 0
307+
super().__init__(*pos, **kw)
308+
def connect(self):
309+
self.connect_count += 1
310+
raise self.connect_exception
311+
312+
def test_refused(self):
313+
exception = EnvironmentError(ECONNREFUSED, "Mock connection refusal")
314+
self.HTTPConnection.connect_exception = exception
315+
try:
316+
self.session.open("http://dummy")
317+
except http.client.HTTPException:
318+
raise
319+
except EnvironmentError as err:
320+
if err.errno != ECONNREFUSED:
321+
raise
322+
else:
323+
self.fail("ECONNREFUSED not raised")
324+
self.assertEqual(1, self.connection._connection.connect_count)
325+
300326
import iview.comm
301327

302328
class TestProxy(TestCase):

0 commit comments

Comments
 (0)