def test_add_done_callback_spawns_task(self): fut = utils.LazyTask(self.coro) cb = unittest.mock.Mock(["__call__"]) with unittest.mock.patch("asyncio.ensure_future") as async_: fut.add_done_callback(cb) async_.assert_called_once_with(unittest.mock.ANY)
def test_async_able(self): self.coro.return_value = unittest.mock.sentinel.result fut = utils.LazyTask(self.coro) result = run_coroutine(asyncio.ensure_future(fut)) self.assertEqual(result, unittest.mock.sentinel.result)
def test_run_coroutine_able(self): self.coro.return_value = unittest.mock.sentinel.result fut = utils.LazyTask(self.coro) result = run_coroutine(fut) self.assertEqual(result, unittest.mock.sentinel.result)
def test_add_done_callback_works(self): fut = utils.LazyTask(self.coro) cb = unittest.mock.Mock(["__call__"]) fut.add_done_callback(cb) run_coroutine(fut) cb.assert_called_once_with(fut)
def test_yield_from_able(self): self.coro.return_value = unittest.mock.sentinel.result async def user(fut): return await fut fut = utils.LazyTask(self.coro) result = run_coroutine(user(fut)) self.assertEqual(result, unittest.mock.sentinel.result)
def test_runs_only_once_even_if_awaited_concurrently(self): self.coro.return_value = unittest.mock.sentinel.result fut = utils.LazyTask(self.coro) result2 = run_coroutine(asyncio.ensure_future(fut)) result1 = run_coroutine(fut) self.assertEqual(result1, result2) self.assertEqual(result1, unittest.mock.sentinel.result) self.coro.assert_called_once_with()
def test_passes_args(self): self.coro.return_value = unittest.mock.sentinel.result fut = utils.LazyTask( self.coro, unittest.mock.sentinel.a1, unittest.mock.sentinel.a2, unittest.mock.sentinel.a3, ) result = run_coroutine(fut) self.assertEqual(result, unittest.mock.sentinel.result) self.coro.assert_called_once_with( unittest.mock.sentinel.a1, unittest.mock.sentinel.a2, unittest.mock.sentinel.a3, )