Skip to content

Commit 0f39c2b

Browse files
tirkarthimiss-islington
authored andcommitted
bpo-37015: Ensure tasks created by _accept_connection2 due to AsyncMock are completed (GH-13661)
From 3.8 async functions used with mock.patch return an `AsyncMock`. `_accept_connection2` is an async function where create_task is also mocked. Don't mock `create_task` so that tasks are created out of coroutine returned by `AsyncMock` and the tasks are completed. https://bugs.python.org/issue37015
1 parent 6eb814b commit 0f39c2b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Lib/test/test_asyncio/test_selector_events.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,14 +363,16 @@ def test_accept_connection_multiple(self):
363363
sock.accept.return_value = (mock.Mock(), mock.Mock())
364364
backlog = 100
365365
# Mock the coroutine generation for a connection to prevent
366-
# warnings related to un-awaited coroutines.
366+
# warnings related to un-awaited coroutines. _accept_connection2
367+
# is an async function that is patched with AsyncMock. create_task
368+
# creates a task out of coroutine returned by AsyncMock, so use
369+
# asyncio.sleep(0) to ensure created tasks are complete to avoid
370+
# task pending warnings.
367371
mock_obj = mock.patch.object
368372
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
369-
accept2_mock.return_value = None
370-
with mock_obj(self.loop, 'create_task') as task_mock:
371-
task_mock.return_value = None
372-
self.loop._accept_connection(
373-
mock.Mock(), sock, backlog=backlog)
373+
self.loop._accept_connection(
374+
mock.Mock(), sock, backlog=backlog)
375+
self.loop.run_until_complete(asyncio.sleep(0))
374376
self.assertEqual(sock.accept.call_count, backlog)
375377

376378

0 commit comments

Comments
 (0)