Browse Source
bpo-44911: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (GH-27765)
pull/27779/head
Bar Harel
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
22 additions and
1 deletions
-
Lib/unittest/async_case.py
-
Lib/unittest/test/test_async_case.py
-
Misc/NEWS.d/next/Library/2021-08-14-00-55-16.bpo-44911.uk3hYk.rst
|
|
|
@ -135,7 +135,7 @@ class IsolatedAsyncioTestCase(TestCase): |
|
|
|
task.cancel() |
|
|
|
|
|
|
|
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: |
|
|
|
if task.cancelled(): |
|
|
|
|
|
|
|
@ -216,6 +216,26 @@ class TestAsyncCase(unittest.TestCase): |
|
|
|
output = test.run() |
|
|
|
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__": |
|
|
|
|
|
|
|
@ -0,0 +1 @@ |
|
|
|
:class:`~unittest.IsolatedAsyncioTestCase` will no longer throw an exception while cancelling leaked tasks. Patch by Bar Harel. |