Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Feb 21, 2022
1 parent 2cd9c75 commit 0e99dc8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,32 @@ async def coro():
self.assertEqual(actual,
(asyncio.CancelledError, expected_args, 0))

def test_cancellation_exception_context(self):
loop = asyncio.new_event_loop()
self.set_event_loop(loop)

async def sleep():
await asyncio.sleep(10)

async def coro():
inner_task = self.new_task(loop, sleep())
await asyncio.sleep(0)
loop.call_soon(inner_task.cancel, 'msg')
try:
await inner_task
except asyncio.CancelledError as ex:
raise ValueError("cancelled") from ex

task = self.new_task(loop, coro())
with self.assertRaises(ValueError) as cm:
loop.run_until_complete(task)
exc = cm.exception
self.assertEqual(exc.args, ('cancelled',))

actual = get_innermost_context(exc)
self.assertEqual(actual,
(asyncio.CancelledError, ('msg',), 1))

def test_cancel_with_message_before_starting_task(self):
loop = asyncio.new_event_loop()
self.set_event_loop(loop)
Expand Down

0 comments on commit 0e99dc8

Please sign in to comment.