예제 #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())