Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-96021: Explicitly tear down the IsolatedAsyncioTestCase loop in te…
…sts (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 <storchaka@gmail.com>
  • Loading branch information
serhiy-storchaka committed Aug 27, 2022
commit bc069b0fffa484c5938cda41384fc031c89c11bb
9 changes: 7 additions & 2 deletions Lib/unittest/test/test_async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def tearDownModule():
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):
Expand Down Expand Up @@ -108,6 +108,7 @@ async def on_cleanup(self):

events = []
test = Test("test_func")
self.addCleanup(test._tearDownAsyncioLoop)
try:
test.debug()
except MyException:
Expand Down Expand Up @@ -143,6 +144,7 @@ async def on_cleanup(self):

events = []
test = Test("test_func")
self.addCleanup(test._tearDownAsyncioLoop)
try:
test.debug()
except MyException:
Expand Down Expand Up @@ -178,6 +180,7 @@ async def on_cleanup(self):

events = []
test = Test("test_func")
self.addCleanup(test._tearDownAsyncioLoop)
try:
test.debug()
except MyException:
Expand Down Expand Up @@ -219,6 +222,7 @@ async def on_cleanup2(self):

events = []
test = Test("test_func")
self.addCleanup(test._tearDownAsyncioLoop)
try:
test.debug()
except MyException:
Expand Down Expand Up @@ -331,6 +335,7 @@ async def cleanup(self, fut):

events = []
test = Test("test_func")
self.addCleanup(test._tearDownAsyncioLoop)
try:
test.debug()
except MyException:
Expand Down