Debian Codegraph @

Lib/test/test_asyncio/test_taskgroups.py

raw
1 # Adapted with permission from the EdgeDB project;
2 # license: PSFL.
3
4 import weakref
5 import sys
6 import gc
7 import asyncio
8 import contextvars
9 import contextlib
10 from asyncio import taskgroups
11 import unittest
12 import warnings
13
14 from test.test_asyncio.utils import await_without_task
15
16 # To prevent a warning "test altered the execution environment"
17 def tearDownModule():
18 asyncio.events._set_event_loop_policy(None)
19
20
21 class MyExc(Exception):
22 pass
23
24
25 class MyBaseExc(BaseException):
26 pass
27
28
29 def get_error_types(eg):
30 return {type(exc) for exc in eg.exceptions}
31
32
33 def no_other_refs():
34 # due to gh-124392 coroutines now refer to their locals
35 coro = asyncio.current_task().get_coro()
36 frame = sys._getframe(1)
37 while coro.cr_frame != frame:
38 coro = coro.cr_await
39 return [coro]
40
41
42 def set_gc_state(enabled):
43 was_enabled = gc.isenabled()
44 if enabled:
45 gc.enable()
46 else:
47 gc.disable()
48 return was_enabled
49
50
51 @contextlib.contextmanager
52 def disable_gc():
53 was_enabled = set_gc_state(enabled=False)
54 try:
55 yield
56 finally:
57 set_gc_state(enabled=was_enabled)
58
59
60 class BaseTestTaskGroup:
61
62 async def test_taskgroup_01(self):
63
64 async def foo1():
65 await asyncio.sleep(0.1)
66 return 42
67
68 async def foo2():
69 await asyncio.sleep(0.2)
70 return 11
71
72 async with taskgroups.TaskGroup() as g:
73 t1 = g.create_task(foo1())
74 t2 = g.create_task(foo2())
75
76 self.assertEqual(t1.result(), 42)
77 self.assertEqual(t2.result(), 11)
78
79 async def test_taskgroup_02(self):
80
81 async def foo1():
82 await asyncio.sleep(0.1)
83 return 42
84
85 async def foo2():
86 await asyncio.sleep(0.2)
87 return 11
88
89 async with taskgroups.TaskGroup() as g:
90 t1 = g.create_task(foo1())
91 await asyncio.sleep(0.15)
92 t2 = g.create_task(foo2())
93
94 self.assertEqual(t1.result(), 42)
95 self.assertEqual(t2.result(), 11)
96
97 async def test_taskgroup_03(self):
98
99 async def foo1():
100 await asyncio.sleep(1)
101 return 42
102
103 async def foo2():
104 await asyncio.sleep(0.2)
105 return 11
106
107 async with taskgroups.TaskGroup() as g:
108 t1 = g.create_task(foo1())
109 await asyncio.sleep(0.15)
110 # cancel t1 explicitly, i.e. everything should continue
111 # working as expected.
112 t1.cancel()
113
114 t2 = g.create_task(foo2())
115
116 self.assertTrue(t1.cancelled())
117 self.assertEqual(t2.result(), 11)
118
119 async def test_taskgroup_04(self):
120
121 NUM = 0
122 t2_cancel = False
123 t2 = None
124
125 async def foo1():
126 await asyncio.sleep(0.1)
127 1 / 0
128
129 async def foo2():
130 nonlocal NUM, t2_cancel
131 try:
132 await asyncio.sleep(1)
133 except asyncio.CancelledError:
134 t2_cancel = True
135 raise
136 NUM += 1
137
138 async def runner():
139 nonlocal NUM, t2
140
141 async with taskgroups.TaskGroup() as g:
142 g.create_task(foo1())
143 t2 = g.create_task(foo2())
144
145 NUM += 10
146
147 with self.assertRaises(ExceptionGroup) as cm:
148 await asyncio.create_task(runner())
149
150 self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
151
152 self.assertEqual(NUM, 0)
153 self.assertTrue(t2_cancel)
154 self.assertTrue(t2.cancelled())
155
156 async def test_cancel_children_on_child_error(self):
157 # When a child task raises an error, the rest of the children
158 # are cancelled and the errors are gathered into an EG.
159
160 NUM = 0
161 t2_cancel = False
162 runner_cancel = False
163
164 async def foo1():
165 await asyncio.sleep(0.1)
166 1 / 0
167
168 async def foo2():
169 nonlocal NUM, t2_cancel
170 try:
171 await asyncio.sleep(5)
172 except asyncio.CancelledError:
173 t2_cancel = True
174 raise
175 NUM += 1
176
177 async def runner():
178 nonlocal NUM, runner_cancel
179
180 async with taskgroups.TaskGroup() as g:
181 g.create_task(foo1())
182 g.create_task(foo1())
183 g.create_task(foo1())
184 g.create_task(foo2())
185 try:
186 await asyncio.sleep(10)
187 except asyncio.CancelledError:
188 runner_cancel = True
189 raise
190
191 NUM += 10
192
193 # The 3 foo1 sub tasks can be racy when the host is busy - if the
194 # cancellation happens in the middle, we'll see partial sub errors here
195 with self.assertRaises(ExceptionGroup) as cm:
196 await asyncio.create_task(runner())
197
198 self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
199 self.assertEqual(NUM, 0)
200 self.assertTrue(t2_cancel)
201 self.assertTrue(runner_cancel)
202
203 async def test_cancellation(self):
204
205 NUM = 0
206
207 async def foo():
208 nonlocal NUM
209 try:
210 await asyncio.sleep(5)
211 except asyncio.CancelledError:
212 NUM += 1
213 raise
214
215 async def runner():
216 async with taskgroups.TaskGroup() as g:
217 for _ in range(5):
218 g.create_task(foo())
219
220 r = asyncio.create_task(runner())
221 await asyncio.sleep(0.1)
222
223 self.assertFalse(r.done())
224 r.cancel()
225 with self.assertRaises(asyncio.CancelledError) as cm:
226 await r
227
228 self.assertEqual(NUM, 5)
229
230 async def test_taskgroup_07(self):
231
232 NUM = 0
233
234 async def foo():
235 nonlocal NUM
236 try:
237 await asyncio.sleep(5)
238 except asyncio.CancelledError:
239 NUM += 1
240 raise
241
242 async def runner():
243 nonlocal NUM
244 async with taskgroups.TaskGroup() as g:
245 for _ in range(5):
246 g.create_task(foo())
247
248 try:
249 await asyncio.sleep(10)
250 except asyncio.CancelledError:
251 NUM += 10
252 raise
253
254 r = asyncio.create_task(runner())
255 await asyncio.sleep(0.1)
256
257 self.assertFalse(r.done())
258 r.cancel()
259 with self.assertRaises(asyncio.CancelledError):
260 await r
261
262 self.assertEqual(NUM, 15)
263
264 async def test_taskgroup_08(self):
265
266 async def foo():
267 try:
268 await asyncio.sleep(10)
269 finally:
270 1 / 0
271
272 async def runner():
273 async with taskgroups.TaskGroup() as g:
274 for _ in range(5):
275 g.create_task(foo())
276
277 await asyncio.sleep(10)
278
279 r = asyncio.create_task(runner())
280 await asyncio.sleep(0.1)
281
282 self.assertFalse(r.done())
283 r.cancel()
284 with self.assertRaises(ExceptionGroup) as cm:
285 await r
286 self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
287
288 async def test_taskgroup_09(self):
289
290 t1 = t2 = None
291
292 async def foo1():
293 await asyncio.sleep(1)
294 return 42
295
296 async def foo2():
297 await asyncio.sleep(2)
298 return 11
299
300 async def runner():
301 nonlocal t1, t2
302 async with taskgroups.TaskGroup() as g:
303 t1 = g.create_task(foo1())
304 t2 = g.create_task(foo2())
305 await asyncio.sleep(0.1)
306 1 / 0
307
308 try:
309 await runner()
310 except ExceptionGroup as t:
311 self.assertEqual(get_error_types(t), {ZeroDivisionError})
312 else:
313 self.fail('ExceptionGroup was not raised')
314
315 self.assertTrue(t1.cancelled())
316 self.assertTrue(t2.cancelled())
317
318 async def test_taskgroup_10(self):
319
320 t1 = t2 = None
321
322 async def foo1():
323 await asyncio.sleep(1)
324 return 42
325
326 async def foo2():
327 await asyncio.sleep(2)
328 return 11
329
330 async def runner():
331 nonlocal t1, t2
332 async with taskgroups.TaskGroup() as g:
333 t1 = g.create_task(foo1())
334 t2 = g.create_task(foo2())
335 1 / 0
336
337 try:
338 await runner()
339 except ExceptionGroup as t:
340 self.assertEqual(get_error_types(t), {ZeroDivisionError})
341 else:
342 self.fail('ExceptionGroup was not raised')
343
344 self.assertTrue(t1.cancelled())
345 self.assertTrue(t2.cancelled())
346
347 async def test_taskgroup_11(self):
348
349 async def foo():
350 try:
351 await asyncio.sleep(10)
352 finally:
353 1 / 0
354
355 async def runner():
356 async with taskgroups.TaskGroup():
357 async with taskgroups.TaskGroup() as g2:
358 for _ in range(5):
359 g2.create_task(foo())
360
361 await asyncio.sleep(10)
362
363 r = asyncio.create_task(runner())
364 await asyncio.sleep(0.1)
365
366 self.assertFalse(r.done())
367 r.cancel()
368 with self.assertRaises(ExceptionGroup) as cm:
369 await r
370
371 self.assertEqual(get_error_types(cm.exception), {ExceptionGroup})
372 self.assertEqual(get_error_types(cm.exception.exceptions[0]), {ZeroDivisionError})
373
374 async def test_taskgroup_12(self):
375
376 async def foo():
377 try:
378 await asyncio.sleep(10)
379 finally:
380 1 / 0
381
382 async def runner():
383 async with taskgroups.TaskGroup() as g1:
384 g1.create_task(asyncio.sleep(10))
385
386 async with taskgroups.TaskGroup() as g2:
387 for _ in range(5):
388 g2.create_task(foo())
389
390 await asyncio.sleep(10)
391
392 r = asyncio.create_task(runner())
393 await asyncio.sleep(0.1)
394
395 self.assertFalse(r.done())
396 r.cancel()
397 with self.assertRaises(ExceptionGroup) as cm:
398 await r
399
400 self.assertEqual(get_error_types(cm.exception), {ExceptionGroup})
401 self.assertEqual(get_error_types(cm.exception.exceptions[0]), {ZeroDivisionError})
402
403 async def test_taskgroup_13(self):
404
405 async def crash_after(t):
406 await asyncio.sleep(t)
407 raise ValueError(t)
408
409 async def runner():
410 async with taskgroups.TaskGroup() as g1:
411 g1.create_task(crash_after(0.1))
412
413 async with taskgroups.TaskGroup() as g2:
414 g2.create_task(crash_after(10))
415
416 r = asyncio.create_task(runner())
417 with self.assertRaises(ExceptionGroup) as cm:
418 await r
419
420 self.assertEqual(get_error_types(cm.exception), {ValueError})
421
422 async def test_taskgroup_14(self):
423
424 async def crash_after(t):
425 await asyncio.sleep(t)
426 raise ValueError(t)
427
428 async def runner():
429 async with taskgroups.TaskGroup() as g1:
430 g1.create_task(crash_after(10))
431
432 async with taskgroups.TaskGroup() as g2:
433 g2.create_task(crash_after(0.1))
434
435 r = asyncio.create_task(runner())
436 with self.assertRaises(ExceptionGroup) as cm:
437 await r
438
439 self.assertEqual(get_error_types(cm.exception), {ExceptionGroup})
440 self.assertEqual(get_error_types(cm.exception.exceptions[0]), {ValueError})
441
442 async def test_taskgroup_15(self):
443
444 async def crash_soon():
445 await asyncio.sleep(0.3)
446 1 / 0
447
448 async def runner():
449 async with taskgroups.TaskGroup() as g1:
450 g1.create_task(crash_soon())
451 try:
452 await asyncio.sleep(10)
453 except asyncio.CancelledError:
454 await asyncio.sleep(0.5)
455 raise
456
457 r = asyncio.create_task(runner())
458 await asyncio.sleep(0.1)
459
460 self.assertFalse(r.done())
461 r.cancel()
462 with self.assertRaises(ExceptionGroup) as cm:
463 await r
464 self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
465
466 async def test_taskgroup_16(self):
467
468 async def crash_soon():
469 await asyncio.sleep(0.3)
470 1 / 0
471
472 async def nested_runner():
473 async with taskgroups.TaskGroup() as g1:
474 g1.create_task(crash_soon())
475 try:
476 await asyncio.sleep(10)
477 except asyncio.CancelledError:
478 await asyncio.sleep(0.5)
479 raise
480
481 async def runner():
482 t = asyncio.create_task(nested_runner())
483 await t
484
485 r = asyncio.create_task(runner())
486 await asyncio.sleep(0.1)
487
488 self.assertFalse(r.done())
489 r.cancel()
490 with self.assertRaises(ExceptionGroup) as cm:
491 await r
492 self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
493
494 async def test_taskgroup_17(self):
495 NUM = 0
496
497 async def runner():
498 nonlocal NUM
499 async with taskgroups.TaskGroup():
500 try:
501 await asyncio.sleep(10)
502 except asyncio.CancelledError:
503 NUM += 10
504 raise
505
506 r = asyncio.create_task(runner())
507 await asyncio.sleep(0.1)
508
509 self.assertFalse(r.done())
510 r.cancel()
511 with self.assertRaises(asyncio.CancelledError):
512 await r
513
514 self.assertEqual(NUM, 10)
515
516 async def test_taskgroup_18(self):
517 NUM = 0
518
519 async def runner():
520 nonlocal NUM
521 async with taskgroups.TaskGroup():
522 try:
523 await asyncio.sleep(10)
524 except asyncio.CancelledError:
525 NUM += 10
526 # This isn't a good idea, but we have to support
527 # this weird case.
528 raise MyExc
529
530 r = asyncio.create_task(runner())
531 await asyncio.sleep(0.1)
532
533 self.assertFalse(r.done())
534 r.cancel()
535
536 try:
537 await r
538 except ExceptionGroup as t:
539 self.assertEqual(get_error_types(t),{MyExc})
540 else:
541 self.fail('ExceptionGroup was not raised')
542
543 self.assertEqual(NUM, 10)
544
545 async def test_taskgroup_19(self):
546 async def crash_soon():
547 await asyncio.sleep(0.1)
548 1 / 0
549
550 async def nested():
551 try:
552 await asyncio.sleep(10)
553 finally:
554 raise MyExc
555
556 async def runner():
557 async with taskgroups.TaskGroup() as g:
558 g.create_task(crash_soon())
559 await nested()
560
561 r = asyncio.create_task(runner())
562 try:
563 await r
564 except ExceptionGroup as t:
565 self.assertEqual(get_error_types(t), {MyExc, ZeroDivisionError})
566 else:
567 self.fail('TasgGroupError was not raised')
568
569 async def test_taskgroup_20(self):
570 async def crash_soon():
571 await asyncio.sleep(0.1)
572 1 / 0
573
574 async def nested():
575 try:
576 await asyncio.sleep(10)
577 finally:
578 raise KeyboardInterrupt
579
580 async def runner():
581 async with taskgroups.TaskGroup() as g:
582 g.create_task(crash_soon())
583 await nested()
584
585 with self.assertRaises(KeyboardInterrupt):
586 await runner()
587
588 async def test_taskgroup_20a(self):
589 async def crash_soon():
590 await asyncio.sleep(0.1)
591 1 / 0
592
593 async def nested():
594 try:
595 await asyncio.sleep(10)
596 finally:
597 raise MyBaseExc
598
599 async def runner():
600 async with taskgroups.TaskGroup() as g:
601 g.create_task(crash_soon())
602 await nested()
603
604 with self.assertRaises(BaseExceptionGroup) as cm:
605 await runner()
606
607 self.assertEqual(
608 get_error_types(cm.exception), {MyBaseExc, ZeroDivisionError}
609 )
610
611 async def _test_taskgroup_21(self):
612 # This test doesn't work as asyncio, currently, doesn't
613 # correctly propagate KeyboardInterrupt (or SystemExit) --
614 # those cause the event loop itself to crash.
615 # (Compare to the previous (passing) test -- that one raises
616 # a plain exception but raises KeyboardInterrupt in nested();
617 # this test does it the other way around.)
618
619 async def crash_soon():
620 await asyncio.sleep(0.1)
621 raise KeyboardInterrupt
622
623 async def nested():
624 try:
625 await asyncio.sleep(10)
626 finally:
627 raise TypeError
628
629 async def runner():
630 async with taskgroups.TaskGroup() as g:
631 g.create_task(crash_soon())
632 await nested()
633
634 with self.assertRaises(KeyboardInterrupt):
635 await runner()
636
637 async def test_taskgroup_21a(self):
638
639 async def crash_soon():
640 await asyncio.sleep(0.1)
641 raise MyBaseExc
642
643 async def nested():
644 try:
645 await asyncio.sleep(10)
646 finally:
647 raise TypeError
648
649 async def runner():
650 async with taskgroups.TaskGroup() as g:
651 g.create_task(crash_soon())
652 await nested()
653
654 with self.assertRaises(BaseExceptionGroup) as cm:
655 await runner()
656
657 self.assertEqual(get_error_types(cm.exception), {MyBaseExc, TypeError})
658
659 async def test_taskgroup_22(self):
660
661 async def foo1():
662 await asyncio.sleep(1)
663 return 42
664
665 async def foo2():
666 await asyncio.sleep(2)
667 return 11
668
669 async def runner():
670 async with taskgroups.TaskGroup() as g:
671 g.create_task(foo1())
672 g.create_task(foo2())
673
674 r = asyncio.create_task(runner())
675 await asyncio.sleep(0.05)
676 r.cancel()
677
678 with self.assertRaises(asyncio.CancelledError):
679 await r
680
681 async def test_taskgroup_23(self):
682
683 async def do_job(delay):
684 await asyncio.sleep(delay)
685
686 async with taskgroups.TaskGroup() as g:
687 for count in range(10):
688 await asyncio.sleep(0.1)
689 g.create_task(do_job(0.3))
690 if count == 5:
691 self.assertLess(len(g._tasks), 5)
692 await asyncio.sleep(1.35)
693 self.assertEqual(len(g._tasks), 0)
694
695 async def test_taskgroup_24(self):
696
697 async def root(g):
698 await asyncio.sleep(0.1)
699 g.create_task(coro1(0.1))
700 g.create_task(coro1(0.2))
701
702 async def coro1(delay):
703 await asyncio.sleep(delay)
704
705 async def runner():
706 async with taskgroups.TaskGroup() as g:
707 g.create_task(root(g))
708
709 await runner()
710
711 async def test_taskgroup_25(self):
712 nhydras = 0
713
714 async def hydra(g):
715 nonlocal nhydras
716 nhydras += 1
717 await asyncio.sleep(0.01)
718 g.create_task(hydra(g))
719 g.create_task(hydra(g))
720
721 async def hercules():
722 while nhydras < 10:
723 await asyncio.sleep(0.015)
724 1 / 0
725
726 async def runner():
727 async with taskgroups.TaskGroup() as g:
728 g.create_task(hydra(g))
729 g.create_task(hercules())
730
731 with self.assertRaises(ExceptionGroup) as cm:
732 await runner()
733
734 self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
735 self.assertGreaterEqual(nhydras, 10)
736
737 async def test_taskgroup_task_name(self):
738 async def coro():
739 await asyncio.sleep(0)
740 async with taskgroups.TaskGroup() as g:
741 t = g.create_task(coro(), name="yolo")
742 self.assertEqual(t.get_name(), "yolo")
743
744 async def test_taskgroup_task_context(self):
745 cvar = contextvars.ContextVar('cvar')
746
747 async def coro(val):
748 await asyncio.sleep(0)
749 cvar.set(val)
750
751 async with taskgroups.TaskGroup() as g:
752 ctx = contextvars.copy_context()
753 self.assertIsNone(ctx.get(cvar))
754 t1 = g.create_task(coro(1), context=ctx)
755 await t1
756 self.assertEqual(1, ctx.get(cvar))
757 t2 = g.create_task(coro(2), context=ctx)
758 await t2
759 self.assertEqual(2, ctx.get(cvar))
760
761 async def test_taskgroup_no_create_task_after_failure(self):
762 async def coro1():
763 await asyncio.sleep(0.001)
764 1 / 0
765 async def coro2(g):
766 try:
767 await asyncio.sleep(1)
768 except asyncio.CancelledError:
769 with self.assertRaises(RuntimeError):
770 g.create_task(coro1())
771
772 with self.assertRaises(ExceptionGroup) as cm:
773 async with taskgroups.TaskGroup() as g:
774 g.create_task(coro1())
775 g.create_task(coro2(g))
776
777 self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
778
779 async def test_taskgroup_context_manager_exit_raises(self):
780 # See https://github.com/python/cpython/issues/95289
781 class CustomException(Exception):
782 pass
783
784 async def raise_exc():
785 raise CustomException
786
787 @contextlib.asynccontextmanager
788 async def database():
789 try:
790 yield
791 finally:
792 raise CustomException
793
794 async def main():
795 task = asyncio.current_task()
796 try:
797 async with taskgroups.TaskGroup() as tg:
798 async with database():
799 tg.create_task(raise_exc())
800 await asyncio.sleep(1)
801 except* CustomException as err:
802 self.assertEqual(task.cancelling(), 0)
803 self.assertEqual(len(err.exceptions), 2)
804
805 else:
806 self.fail('CustomException not raised')
807
808 await asyncio.create_task(main())
809
810 async def test_taskgroup_already_entered(self):
811 tg = taskgroups.TaskGroup()
812 async with tg:
813 with self.assertRaisesRegex(RuntimeError, "has already been entered"):
814 async with tg:
815 pass
816
817 async def test_taskgroup_double_enter(self):
818 tg = taskgroups.TaskGroup()
819 async with tg:
820 pass
821 with self.assertRaisesRegex(RuntimeError, "has already been entered"):
822 async with tg:
823 pass
824
825 async def test_taskgroup_finished(self):
826 async def create_task_after_tg_finish():
827 tg = taskgroups.TaskGroup()
828 async with tg:
829 pass
830 coro = asyncio.sleep(0)
831 with self.assertRaisesRegex(RuntimeError, "is finished"):
832 tg.create_task(coro)
833
834 # Make sure the coroutine was closed when submitted to the inactive tg
835 # (if not closed, a RuntimeWarning should have been raised)
836 with warnings.catch_warnings(record=True) as w:
837 await create_task_after_tg_finish()
838 self.assertEqual(len(w), 0)
839
840 async def test_taskgroup_not_entered(self):
841 tg = taskgroups.TaskGroup()
842 coro = asyncio.sleep(0)
843 with self.assertRaisesRegex(RuntimeError, "has not been entered"):
844 tg.create_task(coro)
845
846 async def test_taskgroup_without_parent_task(self):
847 tg = taskgroups.TaskGroup()
848 with self.assertRaisesRegex(RuntimeError, "parent task"):
849 await await_without_task(tg.__aenter__())
850 coro = asyncio.sleep(0)
851 with self.assertRaisesRegex(RuntimeError, "has not been entered"):
852 tg.create_task(coro)
853
854 async def test_coro_closed_when_tg_closed(self):
855 async def run_coro_after_tg_closes():
856 async with taskgroups.TaskGroup() as tg:
857 pass
858 coro = asyncio.sleep(0)
859 with self.assertRaisesRegex(RuntimeError, "is finished"):
860 tg.create_task(coro)
861
862 await run_coro_after_tg_closes()
863
864 async def test_cancelling_level_preserved(self):
865 async def raise_after(t, e):
866 await asyncio.sleep(t)
867 raise e()
868
869 try:
870 async with asyncio.TaskGroup() as tg:
871 tg.create_task(raise_after(0.0, RuntimeError))
872 except* RuntimeError:
873 pass
874 self.assertEqual(asyncio.current_task().cancelling(), 0)
875
876 async def test_nested_groups_both_cancelled(self):
877 async def raise_after(t, e):
878 await asyncio.sleep(t)
879 raise e()
880
881 try:
882 async with asyncio.TaskGroup() as outer_tg:
883 try:
884 async with asyncio.TaskGroup() as inner_tg:
885 inner_tg.create_task(raise_after(0, RuntimeError))
886 outer_tg.create_task(raise_after(0, ValueError))
887 except* RuntimeError:
888 pass
889 else:
890 self.fail("RuntimeError not raised")
891 self.assertEqual(asyncio.current_task().cancelling(), 1)
892 except* ValueError:
893 pass
894 else:
895 self.fail("ValueError not raised")
896 self.assertEqual(asyncio.current_task().cancelling(), 0)
897
898 async def test_error_and_cancel(self):
899 event = asyncio.Event()
900
901 async def raise_error():
902 event.set()
903 await asyncio.sleep(0)
904 raise RuntimeError()
905
906 async def inner():
907 try:
908 async with taskgroups.TaskGroup() as tg:
909 tg.create_task(raise_error())
910 await asyncio.sleep(1)
911 self.fail("Sleep in group should have been cancelled")
912 except* RuntimeError:
913 self.assertEqual(asyncio.current_task().cancelling(), 1)
914 self.assertEqual(asyncio.current_task().cancelling(), 1)
915 await asyncio.sleep(1)
916 self.fail("Sleep after group should have been cancelled")
917
918 async def outer():
919 t = asyncio.create_task(inner())
920 await event.wait()
921 self.assertEqual(t.cancelling(), 0)
922 t.cancel()
923 self.assertEqual(t.cancelling(), 1)
924 with self.assertRaises(asyncio.CancelledError):
925 await t
926 self.assertTrue(t.cancelled())
927
928 await outer()
929
930 async def test_exception_refcycles_direct(self):
931 """Test that TaskGroup doesn't keep a reference to the raised ExceptionGroup"""
932 tg = asyncio.TaskGroup()
933 exc = None
934
935 class _Done(Exception):
936 pass
937
938 try:
939 async with tg:
940 raise _Done
941 except ExceptionGroup as e:
942 exc = e
943
944 self.assertIsNotNone(exc)
945 self.assertListEqual(gc.get_referrers(exc), no_other_refs())
946
947
948 async def test_exception_refcycles_errors(self):
949 """Test that TaskGroup deletes self._errors, and __aexit__ args"""
950 tg = asyncio.TaskGroup()
951 exc = None
952
953 class _Done(Exception):
954 pass
955
956 try:
957 async with tg:
958 raise _Done
959 except* _Done as excs:
960 exc = excs.exceptions[0]
961
962 self.assertIsInstance(exc, _Done)
963 self.assertListEqual(gc.get_referrers(exc), no_other_refs())
964
965
966 async def test_exception_refcycles_parent_task(self):
967 """Test that TaskGroup deletes self._parent_task"""
968 tg = asyncio.TaskGroup()
969 exc = None
970
971 class _Done(Exception):
972 pass
973
974 async def coro_fn():
975 async with tg:
976 raise _Done
977
978 try:
979 async with asyncio.TaskGroup() as tg2:
980 tg2.create_task(coro_fn())
981 except* _Done as excs:
982 exc = excs.exceptions[0].exceptions[0]
983
984 self.assertIsInstance(exc, _Done)
985 self.assertListEqual(gc.get_referrers(exc), no_other_refs())
986
987
988 async def test_exception_refcycles_parent_task_wr(self):
989 """Test that TaskGroup deletes self._parent_task and create_task() deletes task"""
990 tg = asyncio.TaskGroup()
991 exc = None
992
993 class _Done(Exception):
994 pass
995
996 async def coro_fn():
997 async with tg:
998 raise _Done
999
1000 with disable_gc():
1001 try:
1002 async with asyncio.TaskGroup() as tg2:
1003 task_wr = weakref.ref(tg2.create_task(coro_fn()))
1004 except* _Done as excs:
1005 exc = excs.exceptions[0].exceptions[0]
1006
1007 self.assertIsNone(task_wr())
1008 self.assertIsInstance(exc, _Done)
1009 self.assertListEqual(gc.get_referrers(exc), no_other_refs())
1010
1011 async def test_exception_refcycles_propagate_cancellation_error(self):
1012 """Test that TaskGroup deletes propagate_cancellation_error"""
1013 tg = asyncio.TaskGroup()
1014 exc = None
1015
1016 try:
1017 async with asyncio.timeout(-1):
1018 async with tg:
1019 await asyncio.sleep(0)
1020 except TimeoutError as e:
1021 exc = e.__cause__
1022
1023 self.assertIsInstance(exc, asyncio.CancelledError)
1024 self.assertListEqual(gc.get_referrers(exc), no_other_refs())
1025
1026 async def test_exception_refcycles_base_error(self):
1027 """Test that TaskGroup deletes self._base_error"""
1028 class MyKeyboardInterrupt(KeyboardInterrupt):
1029 pass
1030
1031 tg = asyncio.TaskGroup()
1032 exc = None
1033
1034 try:
1035 async with tg:
1036 raise MyKeyboardInterrupt
1037 except MyKeyboardInterrupt as e:
1038 exc = e
1039
1040 self.assertIsNotNone(exc)
1041 self.assertListEqual(gc.get_referrers(exc), no_other_refs())
1042
1043 async def test_name(self):
1044 name = None
1045
1046 async def asyncfn():
1047 nonlocal name
1048 name = asyncio.current_task().get_name()
1049
1050 async with asyncio.TaskGroup() as tg:
1051 tg.create_task(asyncfn(), name="example name")
1052
1053 self.assertEqual(name, "example name")
1054
1055
1056 async def test_cancels_task_if_created_during_creation(self):
1057 # regression test for gh-128550
1058 ran = False
1059 class MyError(Exception):
1060 pass
1061
1062 exc = None
1063 try:
1064 async with asyncio.TaskGroup() as tg:
1065 async def third_task():
1066 raise MyError("third task failed")
1067
1068 async def second_task():
1069 nonlocal ran
1070 tg.create_task(third_task())
1071 with self.assertRaises(asyncio.CancelledError):
1072 await asyncio.sleep(0) # eager tasks cancel here
1073 await asyncio.sleep(0) # lazy tasks cancel here
1074 ran = True
1075
1076 tg.create_task(second_task())
1077 except* MyError as excs:
1078 exc = excs.exceptions[0]
1079
1080 self.assertTrue(ran)
1081 self.assertIsInstance(exc, MyError)
1082
1083
1084 async def test_cancellation_does_not_leak_out_of_tg(self):
1085 class MyError(Exception):
1086 pass
1087
1088 async def throw_error():
1089 raise MyError
1090
1091 try:
1092 async with asyncio.TaskGroup() as tg:
1093 tg.create_task(throw_error())
1094 except* MyError:
1095 pass
1096 else:
1097 self.fail("should have raised one MyError in group")
1098
1099 # if this test fails this current task will be cancelled
1100 # outside the task group and inside unittest internals
1101 # we yield to the event loop with sleep(0) so that
1102 # cancellation happens here and error is more understandable
1103 await asyncio.sleep(0)
1104
1105
1106 class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
1107 loop_factory = asyncio.EventLoop
1108
1109 class TestEagerTaskTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
1110 @staticmethod
1111 def loop_factory():
1112 loop = asyncio.EventLoop()
1113 loop.set_task_factory(asyncio.eager_task_factory)
1114 return loop
1115
1116
1117 if __name__ == "__main__":
1118 unittest.main()