def _launch_task(self, coro, *args, **kwargs): task_fn = functools.partial(coro, *args, **kwargs) try: self._task = interruptable_task(task_fn) result = yield self._task raise Return(result) finally: self._task = None
def _launch_task(self, coro, *args, **kwargs): """Launch a coroutine as a task, making sure to make it interruptable.""" task_fn = functools.partial(coro, *args, **kwargs) try: self._task = interruptable_task(task_fn) result = yield self._task raise Return(result) finally: self._task = None
async def test_interrupted(self): """Test interrupt future being interrupted""" async def task_fn(cancellable): cancellable.interrupt(RuntimeError('STOP')) task_fut = interruptable_task(task_fn) try: await task_fut except RuntimeError as err: assert str(err) == 'STOP' else: raise AssertionError('ExpectedException not raised')
async def test_future_already_set(self): """Test interrupt future being set before coroutine is done""" async def task_fn(cancellable): fut = asyncio.Future() async def coro(): fut.set_result('I am done') await cancellable.with_interrupt(coro()) cancellable.set_result('NOT ME!!!') return fut.result() task_fut = interruptable_task(task_fn) result = await task_fut assert result == 'NOT ME!!!'
async def test_task(self): """Test coroutine run and succed""" async def task_fn(cancellable): fut = asyncio.Future() async def coro(): fut.set_result('I am done') await cancellable.with_interrupt(coro()) return fut.result() task_fut = interruptable_task(task_fn) result = await task_fut assert isinstance(task_fut, InterruptableFuture) assert task_fut.done() assert result == 'I am done'