Пример #1
0
    async def test_coro_resolve(self):
        p = Promise(success_sleep(), loop=self.loop)

        with self.assertRaisesRegex(RuntimeError, "Task does not support set_result operation"):
            p.resolve(None)

        self.assertEqual(await p, SUCCESS_RESULT)
Пример #2
0
    async def test_resolution(self):
        promise = Promise(self.fut)

        promise.resolve(None)
        result = await promise
        self.assertIsNone(result)
        self.assertEqual(await self.fut, result)
Пример #3
0
    async def test_promise_of_a_promise(self):
        p = Promise()
        p2 = Promise(p)

        p.resolve(0)

        self.assertEqual(await p2.then(lambda x: 1 / x).catch(lambda exc: 0),
                         0)
Пример #4
0
    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())
Пример #5
0
    async def test_promise_cancelled_2(self):
        b = False

        async def a():
            nonlocal b
            b = True

        p = Promise()
        lp = p.lastly(a)

        p.resolve(10)
        lp.cancel()

        with self.assertRaises(CancelledError):
            await lp

        self.assertEqual(await p, 10)
        self.assertFalse(b)
Пример #6
0
    async def test_promise_chain_stack(self):
        p = Promise()

        self.assertEqual(len(p._stack), 1)
        self.assertEqual(p._stack[0].line, "p = Promise()")
        self.assertEqual(p._stack[0].lineno, 14)

        c1_1 = p.lastly(lambda: 1)

        self.assertEqual(len(c1_1._stack), 2)
        self.assertEqual(c1_1._stack[0], p._stack[0])
        self.assertEqual(c1_1._stack[1].line, "c1_1 = p.lastly(lambda: 1)")
        self.assertEqual(c1_1._stack[1].lineno, 20)

        c1_2 = c1_1.then(
            lambda _:
            "LongTextToPurposelyLargerThenTheCharacterLimitOfBlackWhichIsCurrently99"
        )

        self.assertEqual(len(c1_2._stack), 3)
        self.assertEqual(c1_2._stack[0], p._stack[0])
        self.assertEqual(c1_2._stack[1], c1_1._stack[1])
        if sys.version_info >= (3, 8):
            # Python 3.8 supposedly changed this behaviour...
            self.assertEqual(c1_2._stack[2].line, "c1_2 = c1_1.then(")
            self.assertEqual(c1_2._stack[2].lineno, 27)
        else:
            self.assertEqual(
                c1_2._stack[2].line,
                'lambda _: "LongTextToPurposelyLargerThenTheCharacterLimitOfBlackWhichIsCurrently99"',
            )
            self.assertEqual(c1_2._stack[2].lineno, 28)

        c1_3 = c1_2.catch(lambda _: 3)

        self.assertEqual(len(c1_3._stack), 4)
        self.assertEqual(c1_3._stack[0], p._stack[0])
        self.assertEqual(c1_3._stack[1], c1_1._stack[1])
        self.assertEqual(c1_3._stack[2], c1_2._stack[2])
        self.assertEqual(c1_3._stack[3].line, "c1_3 = c1_2.catch(lambda _: 3)")
        self.assertEqual(c1_3._stack[3].lineno, 45)

        c2_1 = p.then(lambda _: 10)

        self.assertEqual(len(c2_1._stack), 2)
        self.assertEqual(c2_1._stack[0], p._stack[0])
        self.assertEqual(c2_1._stack[1].line, "c2_1 = p.then(lambda _: 10)")
        self.assertEqual(c2_1._stack[1].lineno, 54)

        c2_2 = c2_1.lastly(lambda: 20)

        self.assertEqual(len(c2_2._stack), 3)
        self.assertEqual(c2_2._stack[0], p._stack[0])
        self.assertEqual(c2_2._stack[1], c2_1._stack[1])
        self.assertEqual(c2_2._stack[2].line, "c2_2 = c2_1.lastly(lambda: 20)")
        self.assertEqual(c2_2._stack[2].lineno, 61)

        c2_3 = c2_2.then(lambda _: 30)

        self.assertEqual(len(c2_3._stack), 4)
        self.assertEqual(c2_3._stack[0], p._stack[0])
        self.assertEqual(c2_3._stack[1], c2_1._stack[1])
        self.assertEqual(c2_3._stack[2], c2_2._stack[2])
        self.assertEqual(c2_3._stack[3].line, "c2_3 = c2_2.then(lambda _: 30)")
        self.assertEqual(c2_3._stack[3].lineno, 69)

        p.resolve(None)
        self.assertEqual(
            await c1_3,
            "LongTextToPurposelyLargerThenTheCharacterLimitOfBlackWhichIsCurrently99"
        )
        self.assertEqual(await c2_3, 30)