]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] GH-95736: fix IsolatedAsyncioTestCase to initialize Runner bef… (#96042)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Thu, 18 Aug 2022 13:42:16 +0000 (19:12 +0530)
committerGitHub <noreply@github.com>
Thu, 18 Aug 2022 13:42:16 +0000 (14:42 +0100)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/unittest/async_case.py
Lib/unittest/test/test_async_case.py
Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst [new file with mode: 0644]

index a90eed98f871402144f33343037959f225792ba7..3457e92e5da298fa0995daee8ff9871aa968e13f 100644 (file)
@@ -79,6 +79,10 @@ class IsolatedAsyncioTestCase(TestCase):
         return result
 
     def _callSetUp(self):
+        # 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.
+        self._asyncioRunner.get_loop()
         self._asyncioTestContext.run(self.setUp)
         self._callAsync(self.asyncSetUp)
 
index beadcac070b434d2b9edc7f4dd6d9ed84e5d9526..f59fc760d3812fcb534450867a5f1149a6bca734 100644 (file)
@@ -434,6 +434,21 @@ class TestAsyncCase(unittest.TestCase):
         test.doCleanups()
         self.assertEqual(events, ['asyncSetUp', 'test', 'cleanup'])
 
+    def test_setup_get_event_loop(self):
+        # See https://github.com/python/cpython/issues/95736
+        # Make sure the default event loop is not used
+        asyncio.set_event_loop(None)
+
+        class TestCase1(unittest.IsolatedAsyncioTestCase):
+            def setUp(self):
+                asyncio.get_event_loop_policy().get_event_loop()
+
+            async def test_demo1(self):
+                pass
+
+        test = TestCase1('test_demo1')
+        result = test.run()
+        self.assertTrue(result.wasSuccessful())
 
 if __name__ == "__main__":
     unittest.main()
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 (file)
index 0000000..abc270f
--- /dev/null
@@ -0,0 +1 @@
+Fix :class:`unittest.IsolatedAsyncioTestCase` to set event loop before calling setup functions. Patch by Kumar Aditya.