From f03e3cf71977fd49f53af1a7d2feb34aae95baff Mon Sep 17 00:00:00 2001 From: Timm Wagener Date: Sat, 13 Jun 2020 10:17:03 +0000 Subject: [PATCH] Adjust and extend test coverage for _GatheringFuture.cancelled() --- Lib/test/test_asyncio/test_tasks.py | 41 ++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index d58ee4f5175..4e0cb37eeac 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -3342,7 +3342,7 @@ class FutureGatherTests(GatherTestsBase, test_utils.TestCase): self._run_loop(self.one_loop) self.assertTrue(fut.done()) cb.assert_called_once_with(fut) - self.assertFalse(fut.cancelled()) + self.assertTrue(fut.cancelled()) self.assertIsInstance(fut.exception(), asyncio.CancelledError) # Does nothing c.set_result(3) @@ -3373,7 +3373,7 @@ class FutureGatherTests(GatherTestsBase, test_utils.TestCase): self.assertEqual(res, [1, zde, None, 3, None, rte]) cb.assert_called_once_with(fut) - def test_gather_is_cancelled_is_true_when_gather_cancel_is_true(self): + def test_gather_is_cancelled_when_gather_cancelled_directly(self): for re in [True, False]: with self.subTest(return_exceptions=re): @@ -3392,7 +3392,42 @@ class FutureGatherTests(GatherTestsBase, test_utils.TestCase): self.one_loop.run_until_complete(gather_) self.assertTrue(gather_.cancelled()) - def test_gather_is_cancelled_is_false_when_gather_cancel_is_false(self): + def test_gather_is_cancelled_when_child_cancelled_externally_and_return_exceptions_false(self): + + # thread state and cleanup + asyncio.set_event_loop(self.one_loop) + self.addCleanup(asyncio.set_event_loop, None) + + # setup gather + child = asyncio.ensure_future(asyncio.sleep(1, result=1)) + gather_ = asyncio.gather(child, return_exceptions=False) + self.assertTrue(child.cancel()) + + # run and assert + self.assertFalse(gather_.cancelled()) + with self.assertRaises(asyncio.CancelledError): + self.one_loop.run_until_complete(gather_) + self.assertTrue(gather_.cancelled()) + + def test_gather_is_not_cancelled_when_child_cancelled_externally_and_return_exceptions_true(self): + + # thread state and cleanup + asyncio.set_event_loop(self.one_loop) + self.addCleanup(asyncio.set_event_loop, None) + + # setup gather + child = asyncio.ensure_future(asyncio.sleep(1, result=1)) + gather_ = asyncio.gather(child, return_exceptions=True) + self.assertTrue(child.cancel()) + + # run and assert + self.assertFalse(gather_.cancelled()) + result = self.one_loop.run_until_complete(gather_) + self.assertEqual(len(result), 1) + self.assertTrue(isinstance(result[0], asyncio.CancelledError)) + self.assertFalse(gather_.cancelled()) + + def test_gather_is_cancelled_is_false_when_gather_child_succeeds(self): for re in [True, False]: with self.subTest(return_exceptions=re):