async def test_as_completed_timeout(self): calls = [(1.0, ), (0.1, )] futures = [asyncio.sleep(*args) for args in calls] with self.assertRaises(asyncio.TimeoutError): await ait.list(aio.as_completed(futures, timeout=0.5)) futures = [asyncio.sleep(*args) for args in calls] results = 0 with self.assertRaises(asyncio.TimeoutError): async for _ in aio.as_completed(futures, timeout=0.5): results += 1 self.assertEqual(results, 1)
async def test_as_completed(self): async def sleepy(number, duration): await asyncio.sleep(duration) return number pairs = [(1, 0.3), (2, 0.1), (3, 0.5)] expected = [2, 1, 3] futures = [sleepy(*pair) for pair in pairs] results = await ait.list(aio.as_completed(futures)) self.assertEqual(results, expected) futures = [sleepy(*pair) for pair in pairs] results = [] async for value in aio.as_completed(futures): results.append(value) self.assertEqual(results, expected)