[3.11] GH-95736: fix IsolatedAsyncioTestCase to initialize Runner bef… (#96042)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Kumar Aditya 2022-08-18 19:12:16 +05:30 committed by GitHub
parent 1b9b4856c8
commit b68ea2a3e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -79,6 +79,10 @@ async def enterAsyncContext(self, cm):
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)

View File

@ -434,6 +434,21 @@ 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
# 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()

View File

@ -0,0 +1 @@
Fix :class:`unittest.IsolatedAsyncioTestCase` to set event loop before calling setup functions. Patch by Kumar Aditya.