MCPcopy Index your code
hub / github.com/python/cpython / test_cancel_awaited_task

Method test_cancel_awaited_task

Lib/test/test_asyncio/test_tasks.py:996–1030  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

994 self.assertFalse(t.cancel())
995
996 def test_cancel_awaited_task(self):
997 # This tests for a relatively rare condition when
998 # a task cancellation is requested for a task which is not
999 # currently blocked, such as a task cancelling itself.
1000 # In this situation we must ensure that whatever next future
1001 # or task the cancelled task blocks on is cancelled correctly
1002 # as well. See also bpo-34872.
1003 loop = asyncio.new_event_loop()
1004 self.addCleanup(lambda: loop.close())
1005
1006 task = nested_task = None
1007 fut = self.new_future(loop)
1008
1009 async def nested():
1010 await fut
1011
1012 async def coro():
1013 nonlocal nested_task
1014 # Create a sub-task and wait for it to run.
1015 nested_task = self.new_task(loop, nested())
1016 await asyncio.sleep(0)
1017
1018 # Request the current task to be cancelled.
1019 task.cancel()
1020 # Block on the nested task, which should be immediately
1021 # cancelled.
1022 await nested_task
1023
1024 task = self.new_task(loop, coro())
1025 with self.assertRaises(asyncio.CancelledError):
1026 loop.run_until_complete(task)
1027
1028 self.assertTrue(task.cancelled())
1029 self.assertTrue(nested_task.cancelled())
1030 self.assertTrue(fut.cancelled())
1031
1032 def assert_text_contains(self, text, substr):
1033 if substr not in text:

Callers

nothing calls this directly

Calls 9

new_futureMethod · 0.95
new_taskMethod · 0.95
addCleanupMethod · 0.80
assertTrueMethod · 0.80
new_event_loopMethod · 0.45
closeMethod · 0.45
assertRaisesMethod · 0.45
run_until_completeMethod · 0.45
cancelledMethod · 0.45

Tested by

no test coverage detected