def test_gather_shield(self): child1 = futures.Future(loop=self.loop) child2 = futures.Future(loop=self.loop) inner1 = tasks.shield(child1, loop=self.loop) inner2 = tasks.shield(child2, loop=self.loop) parent = tasks.gather(inner1, inner2, loop=self.loop) test_utils.run_briefly(self.loop) parent.cancel() # This should cancel inner1 and inner2 but bot child1 and child2. test_utils.run_briefly(self.loop) self.assertIsInstance(parent.exception(), futures.CancelledError) self.assertTrue(inner1.cancelled()) self.assertTrue(inner2.cancelled()) child1.set_result(1) child2.set_result(2) test_utils.run_briefly(self.loop)
def test_shield_cancel(self): inner = futures.Future(loop=self.loop) outer = tasks.shield(inner) test_utils.run_briefly(self.loop) inner.cancel() test_utils.run_briefly(self.loop) self.assertTrue(outer.cancelled())
def test_shield_exception(self): inner = futures.Future(loop=self.loop) outer = tasks.shield(inner) test_utils.run_briefly(self.loop) exc = RuntimeError('expected') inner.set_exception(exc) test_utils.run_briefly(self.loop) self.assertIs(outer.exception(), exc)
def test_shield_gather(self): child1 = futures.Future(loop=self.loop) child2 = futures.Future(loop=self.loop) parent = tasks.gather(child1, child2, loop=self.loop) outer = tasks.shield(parent, loop=self.loop) test_utils.run_briefly(self.loop) outer.cancel() test_utils.run_briefly(self.loop) self.assertTrue(outer.cancelled()) child1.set_result(1) child2.set_result(2) test_utils.run_briefly(self.loop) self.assertEqual(parent.result(), [1, 2])
def outer(): nonlocal proof yield from tasks.shield(inner(), loop=self.loop) proof += 100
def test_shield_shortcut(self): fut = futures.Future(loop=self.loop) fut.set_result(42) res = self.loop.run_until_complete(tasks.shield(fut)) self.assertEqual(res, 42)
def test_shield_result(self): inner = futures.Future(loop=self.loop) outer = tasks.shield(inner) inner.set_result(42) res = self.loop.run_until_complete(outer) self.assertEqual(res, 42)