Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Add BaseSelectorEventLoopTests.test_accept_connection_skip_connection…
…abortederror to test graceful handling of ConnectionAbortedError
  • Loading branch information
jb2170 committed Dec 29, 2024
commit 317923d7bd8a4740e379922c2c5f44d70154dd34
25 changes: 25 additions & 0 deletions Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,31 @@ def test_accept_connection_multiple(self):
self.loop.run_until_complete(asyncio.sleep(0))
self.assertEqual(sock.accept.call_count, backlog)

def test_accept_connection_skip_connectionabortederror(self):
sock = mock.Mock()

def mock_sock_accept():
# mock accept(2) returning -ECONNABORTED every-other
# time that it's called. This applies most to OpenBSD
# whose sockets generate this errno more reproducibly than
# Linux and other OSs.
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
if sock.accept.call_count % 2 == 0:
raise ConnectionAbortedError
return (mock.Mock(), mock.Mock())

sock.accept.side_effect = mock_sock_accept
backlog = 100
# test that _accept_connection's loop calls sock.accept
# all 100 times, continuing past ConnectionAbortedError
# instead of unnecessarily returning early
mock_obj = mock.patch.object
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
self.loop._accept_connection(
mock.Mock(), sock, backlog=backlog)
# as in test_accept_connection_multiple avoid task pending
# warnings by using asyncio.sleep(0)
self.loop.run_until_complete(asyncio.sleep(0))
self.assertEqual(sock.accept.call_count, backlog)

class SelectorTransportTests(test_utils.TestCase):

Expand Down