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

Method test_timeout_taskgroup

Lib/test/test_asyncio/test_timeouts.py:368–407  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

366 cm.reschedule(0.02)
367
368 async def test_timeout_taskgroup(self):
369 async def task():
370 try:
371 await asyncio.sleep(2) # Will be interrupted after 0.01 second
372 finally:
373 1/0 # Crash in cleanup
374
375 with self.assertRaises(ExceptionGroup) as cm:
376 async with asyncio.timeout(0.01):
377 async with asyncio.TaskGroup() as tg:
378 tg.create_task(task())
379 try:
380 raise ValueError
381 finally:
382 await asyncio.sleep(1)
383 eg = cm.exception
384 # Expect ExceptionGroup raised during handling of TimeoutError caused
385 # by CancelledError raised during handling of ValueError.
386 self.assertIsNone(eg.__cause__)
387 e_1 = eg.__context__
388 self.assertIsInstance(e_1, TimeoutError)
389 e_2 = e_1.__cause__
390 self.assertIsInstance(e_2, asyncio.CancelledError)
391 self.assertIsNone(e_2.__cause__)
392 self.assertIsInstance(e_2.__context__, ValueError)
393 self.assertIs(e_1.__context__, e_2)
394
395 self.assertEqual(len(eg.exceptions), 1, eg)
396 e1 = eg.exceptions[0]
397 # Expect ZeroDivisionError raised during handling of TimeoutError
398 # caused by CancelledError (it is a different CancelledError).
399 self.assertIsInstance(e1, ZeroDivisionError)
400 self.assertIsNone(e1.__cause__)
401 e2 = e1.__context__
402 self.assertIsInstance(e2, TimeoutError)
403 e3 = e2.__cause__
404 self.assertIsInstance(e3, asyncio.CancelledError)
405 self.assertIsNone(e3.__context__)
406 self.assertIsNone(e3.__cause__)
407 self.assertIs(e2.__context__, e3)
408
409
410if __name__ == '__main__':

Callers

nothing calls this directly

Calls 9

taskFunction · 0.85
assertIsNoneMethod · 0.80
assertIsInstanceMethod · 0.80
assertRaisesMethod · 0.45
timeoutMethod · 0.45
create_taskMethod · 0.45
sleepMethod · 0.45
assertIsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected