From 5e06482b8c953a79bc13b17470a1752c457d3721 Mon Sep 17 00:00:00 2001 From: Xtreak Date: Thu, 30 May 2019 00:20:58 +0530 Subject: [PATCH 1/3] Use MagicMock for _accept_connection2 which is an async function not awaited --- Lib/test/test_asyncio/test_selector_events.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py index 2e52e9df5c3b6e..84b1df29d6866c 100644 --- a/Lib/test/test_asyncio/test_selector_events.py +++ b/Lib/test/test_asyncio/test_selector_events.py @@ -363,9 +363,12 @@ 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. Use MagicMock + # since the coroutine is not awaited. mock_obj = mock.patch.object - with mock_obj(self.loop, '_accept_connection2') as accept2_mock: + with mock_obj(self.loop, '_accept_connection2', + new=mock.MagicMock()) as accept2_mock: accept2_mock.return_value = None with mock_obj(self.loop, 'create_task') as task_mock: task_mock.return_value = None From 39ddfcfe24a401de051c0c072a6b163f1f91f937 Mon Sep 17 00:00:00 2001 From: Xtreak Date: Thu, 30 May 2019 08:14:01 +0530 Subject: [PATCH 2/3] Ensure tasks created out of _accept_connection2 patched as AsyncMock are completed --- Lib/test/test_asyncio/test_selector_events.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py index 84b1df29d6866c..a9223eb8fd7ed6 100644 --- a/Lib/test/test_asyncio/test_selector_events.py +++ b/Lib/test/test_asyncio/test_selector_events.py @@ -364,16 +364,15 @@ def test_accept_connection_multiple(self): backlog = 100 # Mock the coroutine generation for a connection to prevent # warnings related to un-awaited coroutines. _accept_connection2 - # is an async function that is patched with AsyncMock. Use MagicMock - # since the coroutine is not awaited. + # 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', - new=mock.MagicMock()) as accept2_mock: - accept2_mock.return_value = None - with mock_obj(self.loop, 'create_task') as task_mock: - task_mock.return_value = None + with mock_obj(self.loop, '_accept_connection2') as accept2_mock: self.loop._accept_connection( mock.Mock(), sock, backlog=backlog) + self.loop.run_until_complete(asyncio.sleep(0)) self.assertEqual(sock.accept.call_count, backlog) From dfc83bbc253a50784e8639d29b9ea34cc7f9ab39 Mon Sep 17 00:00:00 2001 From: Xtreak Date: Thu, 30 May 2019 08:23:11 +0530 Subject: [PATCH 3/3] Fix whitespace error --- Lib/test/test_asyncio/test_selector_events.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py index a9223eb8fd7ed6..68b7853b2eba24 100644 --- a/Lib/test/test_asyncio/test_selector_events.py +++ b/Lib/test/test_asyncio/test_selector_events.py @@ -370,8 +370,8 @@ def test_accept_connection_multiple(self): # task pending warnings. mock_obj = mock.patch.object with mock_obj(self.loop, '_accept_connection2') as accept2_mock: - 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)