async def test_cancellation(self): async with TaskGroup() as tasks: t1 = tasks.add_task(asyncio.sleep(0.01)) t2 = tasks.add_task(asyncio.sleep(60)) await t1 self.assertTrue(t2.cancelled())
async def test_enumeration(self): async with TaskGroup() as tasks: tasks.add_task(asyncio.sleep(0.01)) for t in tasks: self.assertIn(t, tasks) await t self.assertNotIn(t, tasks)
async def test_await_first_return(self): async with TaskGroup() as tasks: e1 = asyncio.Event() e2 = asyncio.Event() e1_task = tasks.add_task(e1.wait()) e2_task = tasks.add_task(e2.wait()) wait_one_task = asyncio.ensure_future(wait_one(e1_task, e2_task)) e1.set() await wait_one_task self.assertTrue(e1_task.done()) self.assertFalse(e2_task.done())
async def test_await_first_exception(self): async with TaskGroup() as tasks: f1 = asyncio.Future() f2 = asyncio.Future() f1_task = tasks.add_task(f1) f2_task = tasks.add_task(f2) wait_one_task = asyncio.ensure_future(wait_one(f1_task, f2_task)) await asyncio.sleep(0) # give loop a chance to schedule futures self.assertFalse(f1_task.done()) self.assertFalse(f2_task.done()) f1_task.set_exception(RuntimeError()) with self.assertRaises(RuntimeError): await wait_one_task self.assertIsInstance(wait_one_task.exception(), RuntimeError) self.assertTrue(f1_task.done()) self.assertFalse(f2_task.done())
async def test_len(self): async with TaskGroup() as tasks: t = tasks.add_task(asyncio.sleep(0.01)) self.assertEqual(len(tasks), 1) await t self.assertEqual(len(tasks), 0)
async def test_collection_integrity(self): async with TaskGroup() as tasks: t = tasks.add_task(asyncio.sleep(0.01)) self.assertIn(t, tasks) await t self.assertNotIn(t, tasks)
async def test_removal_of_completed_tasks(self): async with TaskGroup() as tasks: tasks.remove_task = unittest.mock.MagicMock(wraps=tasks.remove_task) t = tasks.add_task(asyncio.sleep(0.01)) await t tasks.remove_task.assert_called_once_with(t)