]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-96021: Explicitly close the IsolatedAsyncioTestCase runner in tests (GH-96135)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 24 Aug 2022 12:07:20 +0000 (15:07 +0300)
committerGitHub <noreply@github.com>
Wed, 24 Aug 2022 12:07:20 +0000 (15:07 +0300)
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.

Lib/test/test_unittest/test_async_case.py

index f59fc760d3812fcb534450867a5f1149a6bca734..d7d4dc91316c6c323daf0e63ba50ce1455a4b119 100644 (file)
@@ -43,10 +43,10 @@ VAR = contextvars.ContextVar('VAR', default=())
 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 @@ class TestAsyncCase(unittest.TestCase):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -186,6 +187,7 @@ class TestAsyncCase(unittest.TestCase):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -221,6 +223,7 @@ class TestAsyncCase(unittest.TestCase):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -262,6 +265,7 @@ class TestAsyncCase(unittest.TestCase):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException:
@@ -424,6 +428,7 @@ class TestAsyncCase(unittest.TestCase):
 
         events = []
         test = Test("test_func")
+        self.addCleanup(test._tearDownAsyncioRunner)
         try:
             test.debug()
         except MyException: