Skip to content

Commit 317923d

Browse files
committed
Add BaseSelectorEventLoopTests.test_accept_connection_skip_connectionabortederror to test graceful handling of ConnectionAbortedError
1 parent 0f05a32 commit 317923d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Lib/test/test_asyncio/test_selector_events.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,31 @@ def test_accept_connection_multiple(self):
364364
self.loop.run_until_complete(asyncio.sleep(0))
365365
self.assertEqual(sock.accept.call_count, backlog)
366366

367+
def test_accept_connection_skip_connectionabortederror(self):
368+
sock = mock.Mock()
369+
370+
def mock_sock_accept():
371+
# mock accept(2) returning -ECONNABORTED every-other
372+
# time that it's called. This applies most to OpenBSD
373+
# whose sockets generate this errno more reproducibly than
374+
# Linux and other OSs.
375+
if sock.accept.call_count % 2 == 0:
376+
raise ConnectionAbortedError
377+
return (mock.Mock(), mock.Mock())
378+
379+
sock.accept.side_effect = mock_sock_accept
380+
backlog = 100
381+
# test that _accept_connection's loop calls sock.accept
382+
# all 100 times, continuing past ConnectionAbortedError
383+
# instead of unnecessarily returning early
384+
mock_obj = mock.patch.object
385+
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
386+
self.loop._accept_connection(
387+
mock.Mock(), sock, backlog=backlog)
388+
# as in test_accept_connection_multiple avoid task pending
389+
# warnings by using asyncio.sleep(0)
390+
self.loop.run_until_complete(asyncio.sleep(0))
391+
self.assertEqual(sock.accept.call_count, backlog)
367392

368393
class SelectorTransportTests(test_utils.TestCase):
369394

0 commit comments

Comments
 (0)