mirror of
https://github.com/python/cpython.git
synced 2025-08-17 15:21:26 +00:00
[3.9] GH-89074: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (GH-27765) (#91471)
(cherry picked from commit 2cb1a6806c
)
Co-authored-by: Bar Harel <bar.harel@biocatch.com>
This commit is contained in:
parent
8cd0f27492
commit
4cc4fe2789
3 changed files with 22 additions and 1 deletions
|
@ -134,7 +134,7 @@ class IsolatedAsyncioTestCase(TestCase):
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
loop.run_until_complete(
|
loop.run_until_complete(
|
||||||
asyncio.gather(*to_cancel, loop=loop, return_exceptions=True))
|
asyncio.gather(*to_cancel, return_exceptions=True))
|
||||||
|
|
||||||
for task in to_cancel:
|
for task in to_cancel:
|
||||||
if task.cancelled():
|
if task.cancelled():
|
||||||
|
|
|
@ -278,6 +278,26 @@ class TestAsyncCase(unittest.TestCase):
|
||||||
output = test.run()
|
output = test.run()
|
||||||
self.assertFalse(output.wasSuccessful())
|
self.assertFalse(output.wasSuccessful())
|
||||||
|
|
||||||
|
def test_cancellation_hanging_tasks(self):
|
||||||
|
cancelled = False
|
||||||
|
class Test(unittest.IsolatedAsyncioTestCase):
|
||||||
|
async def test_leaking_task(self):
|
||||||
|
async def coro():
|
||||||
|
nonlocal cancelled
|
||||||
|
try:
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
cancelled = True
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Leave this running in the background
|
||||||
|
asyncio.create_task(coro())
|
||||||
|
|
||||||
|
test = Test("test_leaking_task")
|
||||||
|
output = test.run()
|
||||||
|
self.assertTrue(cancelled)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
:class:`~unittest.IsolatedAsyncioTestCase` will no longer throw an exception while cancelling leaked tasks. Patch by Bar Harel.
|
Loading…
Add table
Add a link
Reference in a new issue