]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] GH-89074: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 12 Apr 2022 04:14:43 +0000 (21:14 -0700)
committerGitHub <noreply@github.com>
Tue, 12 Apr 2022 04:14:43 +0000 (23:14 -0500)
(cherry picked from commit 2cb1a6806c0cefab0c3a40fdd428a89a4392570e)

Co-authored-by: Bar Harel <bar.harel@biocatch.com>
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 [new file with mode: 0644]

index a2980e797ac5b5b88a0d2b48b8a601eb683a3bf5..23231199f9870604fd7a33882b51ec2c829545a3 100644 (file)
@@ -134,7 +134,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():
index 2b6e751f3b027fad50809923be9fdd4ab0fde420..22e2d0864ee045e6918be3caefcae09efd4328ea 100644 (file)
@@ -278,6 +278,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__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2021-08-14-00-55-16.bpo-44911.uk3hYk.rst b/Misc/NEWS.d/next/Library/2021-08-14-00-55-16.bpo-44911.uk3hYk.rst
new file mode 100644 (file)
index 0000000..f8aed69
--- /dev/null
@@ -0,0 +1 @@
+:class:`~unittest.IsolatedAsyncioTestCase` will no longer throw an exception while cancelling leaked tasks. Patch by Bar Harel.
\ No newline at end of file