Skip to content

Commit 4de06e3

Browse files
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.
1 parent 420f39f commit 4de06e3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Lib/test/test_unittest/test_async_case.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ async def __aenter__(self):
4343
class TestAsyncCase(unittest.TestCase):
4444
maxDiff = None
4545

46-
def tearDown(self):
46+
def setUp(self):
4747
# Ensure that IsolatedAsyncioTestCase instances are destroyed before
4848
# starting a new event loop
49-
support.gc_collect()
49+
self.addCleanup(support.gc_collect)
5050

5151
def test_full_cycle(self):
5252
class Test(unittest.IsolatedAsyncioTestCase):
@@ -151,6 +151,7 @@ async def on_cleanup(self):
151151

152152
events = []
153153
test = Test("test_func")
154+
self.addCleanup(test._tearDownAsyncioRunner)
154155
try:
155156
test.debug()
156157
except MyException:
@@ -186,6 +187,7 @@ async def on_cleanup(self):
186187

187188
events = []
188189
test = Test("test_func")
190+
self.addCleanup(test._tearDownAsyncioRunner)
189191
try:
190192
test.debug()
191193
except MyException:
@@ -221,6 +223,7 @@ async def on_cleanup(self):
221223

222224
events = []
223225
test = Test("test_func")
226+
self.addCleanup(test._tearDownAsyncioRunner)
224227
try:
225228
test.debug()
226229
except MyException:
@@ -262,6 +265,7 @@ async def on_cleanup2(self):
262265

263266
events = []
264267
test = Test("test_func")
268+
self.addCleanup(test._tearDownAsyncioRunner)
265269
try:
266270
test.debug()
267271
except MyException:
@@ -424,6 +428,7 @@ async def cleanup(self, fut):
424428

425429
events = []
426430
test = Test("test_func")
431+
self.addCleanup(test._tearDownAsyncioRunner)
427432
try:
428433
test.debug()
429434
except MyException:

0 commit comments

Comments
 (0)