[Python-checkins] gh-96021: Explicitly close the IsolatedAsyncioTestCase runner in tests (GH-96135)

miss-islington webhook-mailer at python.org
Thu Aug 25 00:53:57 EDT 2022


https://github.com/python/cpython/commit/f51216df073310c783f1890993fe9b67ccb3c6b4
commit: f51216df073310c783f1890993fe9b67ccb3c6b4
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-08-24T21:53:39-07:00
summary:

gh-96021: Explicitly close the IsolatedAsyncioTestCase runner in tests (GH-96135)


Tests for IsolatedAsyncioTestCase.debug() rely on the runner be closed
in __del__. It makes tests depending on the GC an unreliable on other
implementations. It is better to close the runner explicitly even if
currently there is no a public API for this.
(cherry picked from commit 4de06e3cc0a58d73934f9a2759ad9cd2f6b031b0)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
M Lib/unittest/test/test_async_case.py

diff --git a/Lib/unittest/test/test_async_case.py b/Lib/unittest/test/test_async_case.py
index f59fc760d38..d7d4dc91316 100644
--- a/Lib/unittest/test/test_async_case.py
+++ b/Lib/unittest/test/test_async_case.py
@@ -43,10 +43,10 @@ async def __aenter__(self):
 class TestAsyncCase(unittest.TestCase):
     maxDiff = None
 
-    def tearDown(self):
+    def setUp(self):
         # Ensure that IsolatedAsyncioTestCase instances are destroyed before
         # starting a new event loop
-        support.gc_collect()
+        self.addCleanup(support.gc_collect)
 
     def test_full_cycle(self):
         class Test(unittest.IsolatedAsyncioTestCase):
@@ -151,6 +151,7 @@ async def on_cleanup(self):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -186,6 +187,7 @@ async def on_cleanup(self):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -221,6 +223,7 @@ async def on_cleanup(self):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -262,6 +265,7 @@ async def on_cleanup2(self):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -424,6 +428,7 @@ async def cleanup(self, fut):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:



More information about the Python-checkins mailing list