Exemplo n.º 1
0
    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)
Exemplo n.º 2
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)