async def test_internal_state(self): promise = Promise(success_sleep(), loop=self.loop) self.assertEqual(promise.loop, self.loop) self.assertEqual(promise.done(), False) self.assertEqual(promise.cancelled(), False) await promise self.assertEqual(promise.loop, self.loop) self.assertEqual(promise.done(), True) self.assertEqual(promise.cancelled(), False)
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_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)
def test_initialization(self): promise = Promise(self.fut) self.assertEqual(promise.loop, getattr(self.fut, "_loop")) self.assertEqual(promise.done(), self.fut.done()) self.assertEqual(promise.cancelled(), self.fut.cancelled())