Skip to content

Commit 98b6d01

Browse files
committed
add test
1 parent 9726c3d commit 98b6d01

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Lib/test/test_asyncio/test_taskgroups.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,30 @@ class MyKeyboardInterrupt(KeyboardInterrupt):
997997
self.assertIsNotNone(exc)
998998
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
999999

1000+
async def test_cancels_task_if_created_during_creation(self):
1001+
ran = False
1002+
class MyError(Exception):
1003+
pass
1004+
1005+
try:
1006+
async with asyncio.TaskGroup() as tg:
1007+
async def third_task():
1008+
raise MyError("third task failed")
1009+
1010+
async def second_task():
1011+
nonlocal ran
1012+
tg.create_task(third_task())
1013+
with self.assertRaises(asyncio.CancelledError):
1014+
await asyncio.sleep(0) # eager tasks cancel here
1015+
await asyncio.sleep(0) # lazy tasks cancel here
1016+
ran = True
1017+
1018+
tg.create_task(second_task())
1019+
except* MyError as excs:
1020+
exc = excs.exceptions[0]
1021+
1022+
self.assertIsInstance(exc, MyError)
1023+
self.assertTrue(ran)
10001024

10011025
if __name__ == "__main__":
10021026
unittest.main()

0 commit comments

Comments
 (0)