Skip to content

Commit 49a7e34

Browse files
authored
bpo-37137: Fix test_asyncio: use TestCase.set_event_loop() (GH-13779)
Replace asyncio.set_event_loop() with TestCase.set_event_loop() of test_asyncio.utils: this method calls TestCase.close_loop() which waits until the executor completes, to avoid leaking dangling threads. Inherit from test_asyncio.utils.TestCase rather than unittest.TestCase.
1 parent 0b9956e commit 49a7e34

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

Lib/test/test_asyncio/test_tasks.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -2775,7 +2775,7 @@ def test__unregister_task_not_registered(self):
27752775
self.assertEqual(asyncio.all_tasks(loop), set())
27762776

27772777

2778-
class PyIntrospectionTests(unittest.TestCase, BaseTaskIntrospectionTests):
2778+
class PyIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
27792779
_register_task = staticmethod(tasks._py_register_task)
27802780
_unregister_task = staticmethod(tasks._py_unregister_task)
27812781
_enter_task = staticmethod(tasks._py_enter_task)
@@ -2784,7 +2784,7 @@ class PyIntrospectionTests(unittest.TestCase, BaseTaskIntrospectionTests):
27842784

27852785
@unittest.skipUnless(hasattr(tasks, '_c_register_task'),
27862786
'requires the C _asyncio module')
2787-
class CIntrospectionTests(unittest.TestCase, BaseTaskIntrospectionTests):
2787+
class CIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
27882788
if hasattr(tasks, '_c_register_task'):
27892789
_register_task = staticmethod(tasks._c_register_task)
27902790
_unregister_task = staticmethod(tasks._c_unregister_task)
@@ -2799,12 +2799,7 @@ class BaseCurrentLoopTests:
27992799
def setUp(self):
28002800
super().setUp()
28012801
self.loop = asyncio.new_event_loop()
2802-
asyncio.set_event_loop(self.loop)
2803-
2804-
def tearDown(self):
2805-
self.loop.close()
2806-
asyncio.set_event_loop(None)
2807-
super().tearDown()
2802+
self.set_event_loop(self.loop)
28082803

28092804
def new_task(self, coro):
28102805
raise NotImplementedError
@@ -2828,15 +2823,15 @@ async def coro():
28282823
self.assertIsNone(asyncio.current_task(loop=self.loop))
28292824

28302825

2831-
class PyCurrentLoopTests(BaseCurrentLoopTests, unittest.TestCase):
2826+
class PyCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):
28322827

28332828
def new_task(self, coro):
28342829
return tasks._PyTask(coro, loop=self.loop)
28352830

28362831

28372832
@unittest.skipUnless(hasattr(tasks, '_CTask'),
28382833
'requires the C _asyncio module')
2839-
class CCurrentLoopTests(BaseCurrentLoopTests, unittest.TestCase):
2834+
class CCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):
28402835

28412836
def new_task(self, coro):
28422837
return getattr(tasks, '_CTask')(coro, loop=self.loop)
@@ -3245,7 +3240,7 @@ class SleepTests(test_utils.TestCase):
32453240
def setUp(self):
32463241
super().setUp()
32473242
self.loop = asyncio.new_event_loop()
3248-
asyncio.set_event_loop(None)
3243+
self.set_event_loop(self.loop)
32493244

32503245
def tearDown(self):
32513246
self.loop.close()
@@ -3279,7 +3274,7 @@ class WaitTests(test_utils.TestCase):
32793274
def setUp(self):
32803275
super().setUp()
32813276
self.loop = asyncio.new_event_loop()
3282-
asyncio.set_event_loop(None)
3277+
self.set_event_loop(self.loop)
32833278

32843279
def tearDown(self):
32853280
self.loop.close()
@@ -3306,7 +3301,7 @@ class CompatibilityTests(test_utils.TestCase):
33063301
def setUp(self):
33073302
super().setUp()
33083303
self.loop = asyncio.new_event_loop()
3309-
asyncio.set_event_loop(None)
3304+
self.set_event_loop(self.loop)
33103305

33113306
def tearDown(self):
33123307
self.loop.close()

0 commit comments

Comments
 (0)