예제 #1
0
    def test_noncancellable_child_parent_cancel(self):
        def _fake_timeout(t):
            def _tg(f):
                time.sleep(t)
                f()

            fut = Future()
            Thread(target=lambda: _tg(lambda: fut.set_result(None))).start()
            return fut

        timeout = _fake_timeout(2)
        err = None

        async def _func():
            nonlocal err
            try:
                await timeout
            except GeneratorExit as e:
                err = e

        fut = run_coroutine(_func())
        time.sleep(.5)
        fut.cancel()
        self.assertTrue(timeout.cancelled())
        self.assertIsInstance(err, GeneratorExit)
예제 #2
0
def wrap_future(fut: FutureOrCoroutine[T]) -> AbstractFuture[T]:
    """
    Wraps a :class:`asyncio.futures.Future` or any Coroutine into a Future. It will do
    nothing to the :class:`concurrent.futures.Future`

    :param fut: The future to wrap.
    :return: A new future.
    """
    if isinstance(fut, CoroutineType):
        from yakusoku.coroutines import run_coroutine
        return run_coroutine(fut)

    if not isinstance(fut, Future):
        target: AbstractFuture[T] = Future()

        from asyncio import Future as AIOFuture
        if isinstance(fut, AIOFuture):
            if not hasattr(fut, '_loop') and not hasattr(fut, 'get_loop'):
                raise AttributeError("Cannot fetch loop of future.")

            loop = getattr(fut, 'get_loop', lambda: fut._loop)()
            _copy_aiofuture(loop, fut, target)
        else:
            copy(fut, target)
        return target

    fut: AbstractFuture[
        T]  # Tell this thing that this is still an AbstractFuture[T]
    return fut
예제 #3
0
    def test_coro_resolve_exact(self):
        async def _func():
            f = await resolve(obj)
            return f

        fut = run_coroutine(_func())
        self.assertIs(fut.result(), obj)
예제 #4
0
    def test_call_other_coro(self):
        async def coro2():
            return await resolve(obj)

        async def coro():
            return await coro2()

        fut = run_coroutine(coro())
        self.assertIs(fut.result(), obj)
예제 #5
0
    def test_coro_parent_cancel_exit(self):
        timeout = sleep(2)
        err = None

        async def _func():
            nonlocal err
            try:
                await timeout
            except GeneratorExit as e:
                err = e

        fut = run_coroutine(_func())
        time.sleep(.5)
        fut.cancel()
        self.assertTrue(timeout.cancelled())
        self.assertIsInstance(err, GeneratorExit)
예제 #6
0
    def test_coro_child_cancel_bubble(self):
        timeout = sleep(2)
        err = None

        async def _func():
            nonlocal err
            try:
                await timeout
            except CancelledError as e:
                err = e

        fut = run_coroutine(_func())
        time.sleep(.5)
        timeout.cancel()
        fut.result()
        self.assertIsInstance(err, CancelledError)
예제 #7
0
    def test_coro_wrap_raise(self):
        async def _func():
            raise err

        fut = run_coroutine(_func())
        self.assertIs(fut.exception(), err)
예제 #8
0
    def test_coro_reject_exact(self):
        async def _func():
            await reject(err)

        fut = run_coroutine(_func())
        self.assertIs(fut.exception(), err)
예제 #9
0
 def _wrapper(*args, **kwargs) -> Callable[..., AbstractFuture[T]]:
     c = func(*args, **kwargs)
     return run_coroutine(wrapped(c))