async def task_guard(): loop = asyncio.get_event_loop() async with sakaio.TaskGuard(loop) as guard: guard.create_task( self.waster.waste('D', cycles=200, ignore_cancel=True)) guard.create_task(self.waster.waste('C', cycles=30)) guard.create_task(self.waster.waste('A', cycles=10)) guard.create_task(self.waster.waste('B', cycles=20))
async def test_guard_cancel_some_task(self): loop = asyncio.get_event_loop() async with sakaio.TaskGuard(loop) as guard: guard.create_task(self.waster.waste('C', cycles=30)) guard.create_task(self.waster.waste('A', cycles=10)) t = guard.create_task(self.waster.waste('B', cycles=20)) t.cancel() self.assertEqual(self.waster.completion_order, ['A', 'C'])
async def test_guard_err(self): loop = asyncio.get_event_loop() with self.assertRaises(WasteException): async with sakaio.TaskGuard(loop) as guard: guard.create_task( self.waster.waste('D', cycles=200, ignore_cancel=True)) guard.create_task(self.waster.waste( 'C', cycles=30)) # will get cancelled guard.create_task(self.waster.waste('A', cycles=10)) guard.create_task( self.waster.waste('B', cycles=20, raise_err=True)) self.assertEqual(self.waster.completion_order, ['A', 'D'])
async def test_guard_err_mixed_err(self): loop = asyncio.get_event_loop() with self.assertRaises(SomeOtherException) as cm: async with sakaio.TaskGuard(loop) as guard: guard.create_task( self.waster.waste('D', cycles=200, ignore_cancel=True)) guard.create_task(self.waster.waste( 'C', cycles=30)) # gets cancelled guard.create_task(self.waster.waste('A', cycles=10)) t = guard.create_task( self.waster.waste('B', cycles=20, raise_err=True)) await asyncio.wait([t], loop=loop) raise SomeOtherException() # gets raised as well self.assertIsInstance(cm.exception.__context__, WasteException) self.assertEqual(self.waster.completion_order, ['A', 'D'])
async def test_guard_err_inside_err(self): loop = asyncio.get_event_loop() with self.assertRaises(WasteException) as cm: async with sakaio.TaskGuard(loop) as guard: guard.create_task( self.waster.waste('D', cycles=200, ignore_cancel=True)) guard.create_task(self.waster.waste('C', cycles=30)) guard.create_task(self.waster.waste('A', cycles=10)) t = guard.create_task( self.waster.waste('B', cycles=20, raise_err=True)) await t # raises raise SomeOtherException("should not raise this") self.assertIsNone(cm.exception.__context__) # No SomeOtherException self.assertEqual(self.waster.completion_order, ['A', 'D'])