コード例 #1
0
    def test_coroutine_exception(self):
        async def coro():
            e = Exception()
            e.sentinel_value = 'Sentinel Exception'
            raise e

        t = Task(coro)
        t()
        self.assertTrue(t.done())
        self.assertEqual('Sentinel Exception', t.exception().sentinel_value)
        with self.assertRaises(Exception):
            t.result()
コード例 #2
0
    def test_exception(self):
        def func():
            e = Exception()
            e.sentinel_value = 'Sentinel Exception'
            raise e

        t = Task(func)
        t()
        self.assertTrue(t.done())
        self.assertEqual('Sentinel Exception', t.exception().sentinel_value)
        with self.assertRaises(Exception):
            t.result()
コード例 #3
0
    def test_task_lambda(self):
        def func():
            return 'Sentinel Result'

        t = Task(lambda: func())
        t()
        self.assertTrue(t.done())
        self.assertEqual('Sentinel Result', t.result())
コード例 #4
0
    def test_task_normal_callable(self):
        def func():
            return 'Sentinel Result'

        t = Task(func)
        t()
        self.assertTrue(t.done())
        self.assertEqual('Sentinel Result', t.result())
コード例 #5
0
    def test_coroutine_kwargs(self):
        arg_in = 'Sentinel Arg'

        async def coro(kwarg=None):
            return kwarg

        t = Task(coro, kwargs={'kwarg': arg_in})
        t()
        self.assertEqual('Sentinel Arg', t.result())
コード例 #6
0
    def test_task_normal_callable_kwargs(self):
        arg_in = 'Sentinel Arg'

        def func(kwarg=None):
            return kwarg

        t = Task(func, kwargs={'kwarg': arg_in})
        t()
        self.assertEqual('Sentinel Arg', t.result())
コード例 #7
0
    def test_coroutine_args(self):
        arg_in = 'Sentinel Arg'

        async def coro(arg):
            return arg

        t = Task(coro, args=(arg_in, ))
        t()
        self.assertEqual('Sentinel Arg', t.result())
コード例 #8
0
    def test_task_normal_callable_args(self):
        arg_in = 'Sentinel Arg'

        def func(arg):
            return arg

        t = Task(func, args=(arg_in, ))
        t()
        self.assertEqual('Sentinel Arg', t.result())
コード例 #9
0
    def test_coroutine(self):
        called1 = False
        called2 = False

        async def coro():
            nonlocal called1
            nonlocal called2
            called1 = True
            await asyncio.sleep(0)
            called2 = True
            return 'Sentinel Result'

        t = Task(coro)
        t()
        self.assertTrue(called1)
        self.assertFalse(called2)

        called1 = False
        t()
        self.assertFalse(called1)
        self.assertTrue(called2)
        self.assertTrue(t.done())
        self.assertEqual('Sentinel Result', t.result())