From f96d46742824b9c20fb50b1e2093dc787f06cff4 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Thu, 11 Aug 2022 17:55:08 +0000 Subject: [PATCH 1/5] fix IsolatedAsyncioTestCase to set event loop --- Lib/test/test_unittest/test_async_case.py | 24 +++++++++++++++++++++++ Lib/unittest/async_case.py | 1 + 2 files changed, 25 insertions(+) diff --git a/Lib/test/test_unittest/test_async_case.py b/Lib/test/test_unittest/test_async_case.py index beadcac070b434..882af206a99de1 100644 --- a/Lib/test/test_unittest/test_async_case.py +++ b/Lib/test/test_unittest/test_async_case.py @@ -434,6 +434,30 @@ async def cleanup(self, fut): test.doCleanups() self.assertEqual(events, ['asyncSetUp', 'test', 'cleanup']) + def test_setup_get_event_loop(self): + # See https://github.com/python/cpython/issues/95736 + events = [] + class TestCase1(unittest.IsolatedAsyncioTestCase): + def setUp(self): + self.loop = asyncio.get_event_loop_policy().get_event_loop() + + async def test_demo1(self): + events.append('called demo1') + + + class TestCase2(unittest.IsolatedAsyncioTestCase): + def setUp(self): + self.loop = asyncio.get_event_loop_policy().get_event_loop() + + async def test_demo2(self): + events.append('called demo2') + + test = TestCase1('test_demo1') + test.debug() + self.assertEqual(events, ['called demo1']) + test = TestCase2('test_demo2') + test.debug() + self.assertEqual(events, ['called demo1', 'called demo2']) if __name__ == "__main__": unittest.main() diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index a90eed98f87140..70f165fd963beb 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -116,6 +116,7 @@ def _setupAsyncioRunner(self): assert self._asyncioRunner is None, 'asyncio runner is already initialized' runner = asyncio.Runner(debug=True) self._asyncioRunner = runner + asyncio.set_event_loop(runner.get_loop()) def _tearDownAsyncioRunner(self): runner = self._asyncioRunner From 02f369b0023c5efdf0e4da243a9e643ed77fce50 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 18:22:31 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst diff --git a/Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst b/Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst new file mode 100644 index 00000000000000..abc270fe35ca65 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst @@ -0,0 +1 @@ +Fix :class:`unittest.IsolatedAsyncioTestCase` to set event loop before calling setup functions. Patch by Kumar Aditya. From bf77bb67d8d5f1778111ab38fd4ec8a921a45ed1 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 15 Aug 2022 17:14:49 +0000 Subject: [PATCH 3/5] add comment & simplify --- Lib/unittest/async_case.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index 70f165fd963beb..fc77add6e38e30 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -116,7 +116,8 @@ def _setupAsyncioRunner(self): assert self._asyncioRunner is None, 'asyncio runner is already initialized' runner = asyncio.Runner(debug=True) self._asyncioRunner = runner - asyncio.set_event_loop(runner.get_loop()) + # Force loop to be initialized + runner.get_loop() def _tearDownAsyncioRunner(self): runner = self._asyncioRunner From 06cdeea7c0c2f874f1a7a47cda70ed51ce7de1b3 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 16 Aug 2022 10:54:36 +0000 Subject: [PATCH 4/5] simplify --- Lib/test/test_unittest/test_async_case.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/Lib/test/test_unittest/test_async_case.py b/Lib/test/test_unittest/test_async_case.py index 882af206a99de1..6fe99cf9810ae1 100644 --- a/Lib/test/test_unittest/test_async_case.py +++ b/Lib/test/test_unittest/test_async_case.py @@ -436,28 +436,19 @@ async def cleanup(self, fut): def test_setup_get_event_loop(self): # See https://github.com/python/cpython/issues/95736 - events = [] + # Make sure the default event loop is not used + asyncio.set_event_loop(None) + class TestCase1(unittest.IsolatedAsyncioTestCase): def setUp(self): self.loop = asyncio.get_event_loop_policy().get_event_loop() async def test_demo1(self): - events.append('called demo1') - - - class TestCase2(unittest.IsolatedAsyncioTestCase): - def setUp(self): - self.loop = asyncio.get_event_loop_policy().get_event_loop() - - async def test_demo2(self): - events.append('called demo2') + pass test = TestCase1('test_demo1') - test.debug() - self.assertEqual(events, ['called demo1']) - test = TestCase2('test_demo2') - test.debug() - self.assertEqual(events, ['called demo1', 'called demo2']) + result = test.run() + self.assertTrue(result.wasSuccessful()) if __name__ == "__main__": unittest.main() From 33243540d5839e48d45525551dbc9d620ae1ddd3 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 16 Aug 2022 10:58:33 +0000 Subject: [PATCH 5/5] code review --- Lib/test/test_unittest/test_async_case.py | 2 +- Lib/unittest/async_case.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_unittest/test_async_case.py b/Lib/test/test_unittest/test_async_case.py index 6fe99cf9810ae1..f59fc760d3812f 100644 --- a/Lib/test/test_unittest/test_async_case.py +++ b/Lib/test/test_unittest/test_async_case.py @@ -441,7 +441,7 @@ def test_setup_get_event_loop(self): class TestCase1(unittest.IsolatedAsyncioTestCase): def setUp(self): - self.loop = asyncio.get_event_loop_policy().get_event_loop() + asyncio.get_event_loop_policy().get_event_loop() async def test_demo1(self): pass diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index fc77add6e38e30..8b06fad0620946 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -116,7 +116,9 @@ def _setupAsyncioRunner(self): assert self._asyncioRunner is None, 'asyncio runner is already initialized' runner = asyncio.Runner(debug=True) self._asyncioRunner = runner - # Force loop to be initialized + # Force loop to be initialized and set as the current loop + # so that setUp functions can use get_event_loop() and get the + # correct loop instance. runner.get_loop() def _tearDownAsyncioRunner(self):