Skip to content

Commit bc069b0

Browse files
gh-96021: Explicitly tear down the IsolatedAsyncioTestCase loop 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 tear down the loop explicitly even if currently there is no a public API for this. (cherry picked from commit 4de06e3) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 203b598 commit bc069b0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Lib/unittest/test/test_async_case.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def tearDownModule():
1414
class TestAsyncCase(unittest.TestCase):
1515
maxDiff = None
1616

17-
def tearDown(self):
17+
def setUp(self):
1818
# Ensure that IsolatedAsyncioTestCase instances are destroyed before
1919
# starting a new event loop
20-
support.gc_collect()
20+
self.addCleanup(support.gc_collect)
2121

2222
def test_full_cycle(self):
2323
class Test(unittest.IsolatedAsyncioTestCase):
@@ -108,6 +108,7 @@ async def on_cleanup(self):
108108

109109
events = []
110110
test = Test("test_func")
111+
self.addCleanup(test._tearDownAsyncioLoop)
111112
try:
112113
test.debug()
113114
except MyException:
@@ -143,6 +144,7 @@ async def on_cleanup(self):
143144

144145
events = []
145146
test = Test("test_func")
147+
self.addCleanup(test._tearDownAsyncioLoop)
146148
try:
147149
test.debug()
148150
except MyException:
@@ -178,6 +180,7 @@ async def on_cleanup(self):
178180

179181
events = []
180182
test = Test("test_func")
183+
self.addCleanup(test._tearDownAsyncioLoop)
181184
try:
182185
test.debug()
183186
except MyException:
@@ -219,6 +222,7 @@ async def on_cleanup2(self):
219222

220223
events = []
221224
test = Test("test_func")
225+
self.addCleanup(test._tearDownAsyncioLoop)
222226
try:
223227
test.debug()
224228
except MyException:
@@ -331,6 +335,7 @@ async def cleanup(self, fut):
331335

332336
events = []
333337
test = Test("test_func")
338+
self.addCleanup(test._tearDownAsyncioLoop)
334339
try:
335340
test.debug()
336341
except MyException:

0 commit comments

Comments
 (0)