mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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
This commit is contained in:
parent
6eb814b8ce
commit
0f39c2b191
1 changed files with 8 additions and 6 deletions
|
@ -363,14 +363,16 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
|
||||||
sock.accept.return_value = (mock.Mock(), mock.Mock())
|
sock.accept.return_value = (mock.Mock(), mock.Mock())
|
||||||
backlog = 100
|
backlog = 100
|
||||||
# Mock the coroutine generation for a connection to prevent
|
# Mock the coroutine generation for a connection to prevent
|
||||||
# warnings related to un-awaited coroutines.
|
# warnings related to un-awaited coroutines. _accept_connection2
|
||||||
|
# is an async function that is patched with AsyncMock. create_task
|
||||||
|
# creates a task out of coroutine returned by AsyncMock, so use
|
||||||
|
# asyncio.sleep(0) to ensure created tasks are complete to avoid
|
||||||
|
# task pending warnings.
|
||||||
mock_obj = mock.patch.object
|
mock_obj = mock.patch.object
|
||||||
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
|
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
|
||||||
accept2_mock.return_value = None
|
self.loop._accept_connection(
|
||||||
with mock_obj(self.loop, 'create_task') as task_mock:
|
mock.Mock(), sock, backlog=backlog)
|
||||||
task_mock.return_value = None
|
self.loop.run_until_complete(asyncio.sleep(0))
|
||||||
self.loop._accept_connection(
|
|
||||||
mock.Mock(), sock, backlog=backlog)
|
|
||||||
self.assertEqual(sock.accept.call_count, backlog)
|
self.assertEqual(sock.accept.call_count, backlog)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue