diff options
author | Yury Selivanov <yury@magic.io> | 2016-10-21 21:22:17 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2016-10-21 21:22:17 (GMT) |
commit | 3d67615a485f4769eec5927e17989b31d6917e1c (patch) | |
tree | 9e93ca98520dd3c407c6e87bff613707e5b46d78 /Lib/test/test_asyncio | |
parent | f8c1505736cb6eeb264e58c28d1f94d8fe7cad34 (diff) | |
download | cpython-3d67615a485f4769eec5927e17989b31d6917e1c.zip cpython-3d67615a485f4769eec5927e17989b31d6917e1c.tar.gz cpython-3d67615a485f4769eec5927e17989b31d6917e1c.tar.bz2 |
Issue #26923: Fix asyncio.Gather to refuse being cancelled once all children are done.
Patch by Johannes Ebke.
Diffstat (limited to 'Lib/test/test_asyncio')
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index a5af7d1..1ceb9b2 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1899,6 +1899,36 @@ class TaskTests(test_utils.TestCase): def test_cancel_wait_for(self): self._test_cancel_wait_for(60.0) + def test_cancel_gather(self): + """Ensure that a gathering future refuses to be cancelled once all + children are done""" + loop = asyncio.new_event_loop() + self.addCleanup(loop.close) + + fut = asyncio.Future(loop=loop) + # The indirection fut->child_coro is needed since otherwise the + # gathering task is done at the same time as the child future + def child_coro(): + return (yield from fut) + gather_future = asyncio.gather(child_coro(), loop=loop) + gather_task = asyncio.ensure_future(gather_future, loop=loop) + + cancel_result = None + def cancelling_callback(_): + nonlocal cancel_result + cancel_result = gather_task.cancel() + fut.add_done_callback(cancelling_callback) + + fut.set_result(42) # calls the cancelling_callback after fut is done() + + # At this point the task should complete. + loop.run_until_complete(gather_task) + + # Python issue #26923: asyncio.gather drops cancellation + self.assertEqual(cancel_result, False) + self.assertFalse(gather_task.cancelled()) + self.assertEqual(gather_task.result(), [42]) + class GatherTestsBase: |