def test_done_task_called(self): called = False def func(): nonlocal called called = True t = Task(func) t() self.assertTrue(called) self.assertTrue(t.done()) called = False t() self.assertFalse(called) self.assertTrue(t.done())
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())
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())
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) self.assertEqual(None, t.result())
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) self.assertEqual(None, t.result())
def test_done_task_done_callback_scheduled(self): executor = DummyExecutor() t = Task(lambda: None, executor=executor) t() self.assertTrue(t.done()) t.add_done_callback('Sentinel Value') self.assertEqual(1, len(executor.done_callbacks)) self.assertEqual('Sentinel Value', executor.done_callbacks[0][0]) args = executor.done_callbacks[0][1] self.assertEqual(1, len(args)) self.assertEqual(t, args[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())