@@ -436,11 +436,12 @@ async def main(t):
436436 'T<inner>' ,
437437 ['s capture_test_stack' , 'a inner' ],
438438 [
439- ['T<anon>' ,
440- ['a get' , 'a _wait_for_one' , 'a main' ,
441- 'a test_stack_as_completed' ],
442- []
443- ]
439+ ['F' , [], [
440+ ['T<anon>' ,
441+ ['a get' , 'a _wait_for_one' , 'a main' ,
442+ 'a test_stack_as_completed' ],
443+ []],
444+ ]],
444445 ]
445446 ])
446447
@@ -469,14 +470,87 @@ async def main(t):
469470 'T<inner>' ,
470471 ['s capture_test_stack' , 'a inner' ],
471472 [
472- ['T<anon>' ,
473- ['a get' , 'a _wait_for_one' , 'a main' ,
474- 'a test_stack_as_completed_timeout' ],
475- []
476- ]
473+ ['F' , [], [
474+ ['T<anon>' ,
475+ ['a get' , 'a _wait_for_one' , 'a main' ,
476+ 'a test_stack_as_completed_timeout' ],
477+ []],
478+ ]],
477479 ]
478480 ])
479481
482+ async def test_as_completed_awaited_by_different_task (self ):
483+ for get_next in (anext , next ):
484+ with self .subTest (iterator = get_next .__name__ ):
485+ child = asyncio .get_running_loop ().create_future ()
486+ iterator = asyncio .as_completed ([child ])
487+ consumer = asyncio .create_task (
488+ get_next (iterator ), eager_start = True )
489+ graph = asyncio .capture_call_graph (child , limit = 0 )
490+ child .set_result (None )
491+ await consumer
492+
493+ waiter , = graph .awaited_by
494+ self .assertEqual ([node .future for node in waiter .awaited_by ],
495+ [consumer ])
496+ graph = asyncio .capture_call_graph (waiter .future , limit = 0 )
497+ self .assertFalse (graph .awaited_by )
498+
499+ async def test_as_completed_discards_awaited_by_on_cancel (self ):
500+ for get_next in (anext , next ):
501+ with self .subTest (iterator = get_next .__name__ ):
502+ loop = asyncio .get_running_loop ()
503+ first , second = loop .create_future (), loop .create_future ()
504+ iterator = asyncio .as_completed ([first , second ])
505+ cancelled = asyncio .create_task (
506+ get_next (iterator ), eager_start = True )
507+ remaining = asyncio .create_task (
508+ get_next (iterator ), eager_start = True )
509+
510+ before = asyncio .capture_call_graph (second , limit = 0 )
511+ cancelled .cancel ()
512+ with self .assertRaises (asyncio .CancelledError ):
513+ await cancelled
514+ after = asyncio .capture_call_graph (second , limit = 0 )
515+
516+ first .set_result (None )
517+ second .set_result (None )
518+ await remaining
519+ await get_next (iterator )
520+
521+ before_waiter , = before .awaited_by
522+ after_waiter , = after .awaited_by
523+ self .assertCountEqual (
524+ [node .future for node in before_waiter .awaited_by ],
525+ [cancelled , remaining ])
526+ self .assertEqual (
527+ [node .future for node in after_waiter .awaited_by ],
528+ [remaining ])
529+
530+ async def test_as_completed_discards_awaited_by_on_timeout (self ):
531+ for get_next in (anext , next ):
532+ with self .subTest (iterator = get_next .__name__ ):
533+ loop = asyncio .get_running_loop ()
534+ first , second = loop .create_future (), loop .create_future ()
535+ iterator = asyncio .as_completed ([first , second ], timeout = 0 )
536+
537+ graph = asyncio .capture_call_graph (first , limit = 0 )
538+ self .assertEqual (len (graph .awaited_by ), 1 )
539+ waiter = graph .awaited_by [0 ].future
540+ graph = asyncio .capture_call_graph (second , limit = 0 )
541+ self .assertEqual ([node .future for node in graph .awaited_by ],
542+ [waiter ])
543+
544+ with self .assertRaises (TimeoutError ):
545+ await get_next (iterator )
546+
547+ for child in (first , second ):
548+ self .assertFalse (child .done ())
549+ graph = asyncio .capture_call_graph (child , limit = 0 )
550+ self .assertFalse (graph .awaited_by )
551+ self .assertFalse (
552+ asyncio .capture_call_graph (waiter , limit = 0 ).awaited_by )
553+
480554 async def test_stack_task (self ):
481555
482556 stack_for_inner = None
0 commit comments