Skip to content

Commit

Permalink
pythongh-96021: Explicitly close the IsolatedAsyncioTestCase runner i…
Browse files Browse the repository at this point in the history
…n tests (pythonGH-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 4de06e3)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
serhiy-storchaka authored and miss-islington committed Aug 24, 2022
1 parent 2b8fd74 commit cb2487b
Showing 1 changed file with 7 additions and 2 deletions.
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 @@ -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):
Expand Down Expand Up @@ -151,6 +151,7 @@ async def on_cleanup(self):

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

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

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

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

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

0 comments on commit cb2487b

Please sign in to comment.