@@ -1702,6 +1702,46 @@ async def coro(i):
17021702 )
17031703 self .assertCountEqual (results , (0 , 1 , 2 , 3 ))
17041704
1705+ def test_as_completed_async_iterator_cancelled_anext (self ):
1706+ async def main ():
1707+ loop = asyncio .get_running_loop ()
1708+ a = loop .create_future ()
1709+ b = loop .create_future ()
1710+ iterator = asyncio .as_completed ([a , b ])
1711+
1712+ waiter = asyncio .create_task (anext (iterator ))
1713+ await asyncio .sleep (0 )
1714+ waiter .cancel ()
1715+ with self .assertRaises (asyncio .CancelledError ):
1716+ await waiter
1717+
1718+ a .set_result ('a' )
1719+ b .set_result ('b' )
1720+ return [await f async for f in iterator ]
1721+
1722+ results = self .loop .run_until_complete (main ())
1723+ self .assertCountEqual (results , ['a' , 'b' ])
1724+
1725+ def test_as_completed_plain_iterator_cancelled_wait (self ):
1726+ async def main ():
1727+ loop = asyncio .get_running_loop ()
1728+ a = loop .create_future ()
1729+ b = loop .create_future ()
1730+ iterator = asyncio .as_completed ([a , b ])
1731+
1732+ waiter = asyncio .create_task (next (iterator ))
1733+ await asyncio .sleep (0 )
1734+ waiter .cancel ()
1735+ with self .assertRaises (asyncio .CancelledError ):
1736+ await waiter
1737+
1738+ a .set_result ('a' )
1739+ b .set_result ('b' )
1740+ return [await c for c in iterator ]
1741+
1742+ results = self .loop .run_until_complete (main ())
1743+ self .assertCountEqual (results , ['a' , 'b' ])
1744+
17051745 def test_as_completed_reverse_wait (self ):
17061746 # Tests the plain iterator style of as_completed iteration to
17071747 # ensure that the first future awaited resolves to the first
0 commit comments