async def test_promise_cancellation_after_result(self): p = Promise() counter = Counter() p.cancel() with self.assertRaises(CancelledError): await (p.then(lambda x: counter.update(["then"])).catch( lambda exc: counter.update(["catch"])).lastly( lambda: counter.update(["lastly"]))) self.assertTrue(p.done()) self.assertTrue(p.cancelled()) self.assertEqual(counter["then"], 0) self.assertEqual(counter["catch"], 0) self.assertEqual(counter["lastly"], 0)
async def test_promise_cancelled(self): b = False async def a(): nonlocal b b = True p = Promise() lp = p.lastly(a) p.cancel() with self.assertRaises(CancelledError): await p with self.assertRaises(CancelledError): await lp self.assertFalse(b)
def test_cancel_differences_between_promise_and_future(self): promise = Promise(self.fut) promise.resolve(None) with self.assertRaises(InvalidStateError): self.fut.set_result(None) with self.assertRaises(InvalidStateError): self.fut.set_exception(Exception()) self.assertFalse(self.fut.cancel()) self.assertFalse(self.fut.cancelled()) self.assertTrue(promise.cancel()) self.assertTrue(promise.cancelled())
async def test_cancellation(self): promise = Promise(self.fut) promise.cancel() self.assertAsyncRaises(CancelledError, self.fut) self.assertAsyncRaises(CancelledError, promise)