[Python-checkins] bpo-37015: Ensure tasks created by _accept_connection2 due to AsyncMock are completed (GH-13661)

Miss Islington (bot) webhook-mailer at python.org
Thu May 30 06:00:33 EDT 2019


https://github.com/python/cpython/commit/0f39c2b1919727904f4fac2d79cb41dc6bfe41fe
commit: 0f39c2b1919727904f4fac2d79cb41dc6bfe41fe
branch: master
author: Xtreak <tir.karthi at gmail.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-05-30T03:00:29-07:00
summary:

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

files:
M Lib/test/test_asyncio/test_selector_events.py

diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py
index 2e52e9df5c3b..68b7853b2eba 100644
--- a/Lib/test/test_asyncio/test_selector_events.py
+++ b/Lib/test/test_asyncio/test_selector_events.py
@@ -363,14 +363,16 @@ def test_accept_connection_multiple(self):
         sock.accept.return_value = (mock.Mock(), mock.Mock())
         backlog = 100
         # 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
         with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
-            accept2_mock.return_value = None
-            with mock_obj(self.loop, 'create_task') as task_mock:
-                task_mock.return_value = None
-                self.loop._accept_connection(
-                    mock.Mock(), sock, backlog=backlog)
+            self.loop._accept_connection(
+                mock.Mock(), sock, backlog=backlog)
+        self.loop.run_until_complete(asyncio.sleep(0))
         self.assertEqual(sock.accept.call_count, backlog)
 
 



More information about the Python-checkins mailing list