コード例 #1
0
 def test_async_context_manager_success(self, anyio_backend_name,
                                        anyio_backend_options):
     with start_blocking_portal(anyio_backend_name,
                                anyio_backend_options) as portal:
         with portal.wrap_async_context_manager(
                 TestBlockingPortal.AsyncCM(False)) as cm:
             assert cm == 'test'
コード例 #2
0
    def test_call_stopped_portal(self, anyio_backend_name,
                                 anyio_backend_options):
        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            pass

        pytest.raises(RuntimeError, portal.call, threading.get_ident).\
            match('This portal is not running')
コード例 #3
0
 def test_async_context_manager_error_ignore(self, anyio_backend_name,
                                             anyio_backend_options):
     with start_blocking_portal(anyio_backend_name,
                                anyio_backend_options) as portal:
         with portal.wrap_async_context_manager(
                 TestBlockingPortal.AsyncCM(True)) as cm:
             assert cm == 'test'
             raise Exception('should be ignored')
コード例 #4
0
    def test_start_with_name(self, anyio_backend_name, anyio_backend_options):
        def taskfunc(*, task_status):
            task_status.started(get_current_task().name)

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future, start_value = portal.start_task(taskfunc, name='testname')
            assert start_value == 'testname'
コード例 #5
0
    def test_start_crash_before_started_call(self, anyio_backend_name,
                                             anyio_backend_options):
        def taskfunc(*, task_status):
            raise Exception('foo')

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            with pytest.raises(Exception, match='foo'):
                portal.start_task(taskfunc)
コード例 #6
0
    def test_start_with_value(self, anyio_backend_name, anyio_backend_options):
        def taskfunc(*, task_status):
            task_status.started('foo')

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future, value = portal.start_task(taskfunc)
            assert value == 'foo'
            assert future.result() is None
コード例 #7
0
    def test_call_stopped_portal(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            pass

        pytest.raises(RuntimeError, portal.call,
                      threading.get_ident).match("This portal is not running")
コード例 #8
0
 def test_async_context_manager_error_ignore(
         self, anyio_backend_name: str,
         anyio_backend_options: Dict[str, Any]) -> None:
     with start_blocking_portal(anyio_backend_name,
                                anyio_backend_options) as portal:
         with portal.wrap_async_context_manager(
                 TestBlockingPortal.AsyncCM(True)) as cm:
             assert cm == "test"
             raise Exception("should be ignored")
コード例 #9
0
    def test_start_no_started_call(self, anyio_backend_name,
                                   anyio_backend_options):
        def taskfunc(*, task_status):
            pass

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            with pytest.raises(RuntimeError, match='Task exited'):
                portal.start_task(taskfunc)
コード例 #10
0
    def test_start_crash_before_started_call(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        def taskfunc(*, task_status: object) -> NoReturn:
            raise Exception("foo")

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            with pytest.raises(Exception, match="foo"):
                portal.start_task(taskfunc)
コード例 #11
0
    def test_start_no_started_call(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        def taskfunc(*, task_status: TaskStatus) -> None:
            pass

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            with pytest.raises(RuntimeError, match="Task exited"):
                portal.start_task(taskfunc)  # type: ignore[arg-type]
コード例 #12
0
    def test_async_context_manager_error(self, anyio_backend_name,
                                         anyio_backend_options):
        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            with pytest.raises(Exception) as exc:
                with portal.wrap_async_context_manager(
                        TestBlockingPortal.AsyncCM(False)) as cm:
                    assert cm == 'test'
                    raise Exception('should NOT be ignored')

                exc.match('should NOT be ignored')
コード例 #13
0
    def test_start_with_new_event_loop(self, anyio_backend_name,
                                       anyio_backend_options):
        async def async_get_thread_id():
            return threading.get_ident()

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            thread_id = portal.call(async_get_thread_id)

        assert isinstance(thread_id, int)
        assert thread_id != threading.get_ident()
コード例 #14
0
    def test_start_with_value(self, anyio_backend_name: str,
                              anyio_backend_options: Dict[str, Any]) -> None:
        def taskfunc(*, task_status: TaskStatus) -> None:
            task_status.started("foo")

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future, value = portal.start_task(
                taskfunc)  # type: ignore[arg-type]
            assert value == "foo"
            assert future.result() is None
コード例 #15
0
    def test_start_with_name(self, anyio_backend_name: str,
                             anyio_backend_options: Dict[str, Any]) -> None:
        def taskfunc(*, task_status: TaskStatus) -> None:
            task_status.started(get_current_task().name)

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future, start_value = portal.start_task(
                taskfunc,
                name="testname"  # type: ignore[arg-type]
            )
            assert start_value == "testname"
コード例 #16
0
    def test_start_task_soon_cancel_later(self, anyio_backend_name,
                                          anyio_backend_options):
        async def noop():
            await sleep(2)

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future = portal.start_task_soon(noop)
            portal.call(wait_all_tasks_blocked)
            future.cancel()

        assert future.cancelled()
コード例 #17
0
    def test_async_context_manager_error(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            with pytest.raises(Exception) as exc:
                with portal.wrap_async_context_manager(
                        TestBlockingPortal.AsyncCM(False)) as cm:
                    assert cm == "test"
                    raise Exception("should NOT be ignored")

                exc.match("should NOT be ignored")
コード例 #18
0
    def test_start_task_soon_with_name(self, anyio_backend_name,
                                       anyio_backend_options):
        async def taskfunc():
            nonlocal task_name
            task_name = get_current_task().name

        task_name = None
        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            portal.start_task_soon(taskfunc, name='testname')

        assert task_name == 'testname'
コード例 #19
0
    def test_start_crash_after_started_call(self, anyio_backend_name,
                                            anyio_backend_options):
        def taskfunc(*, task_status):
            task_status.started(2)
            raise Exception('foo')

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future, value = portal.start_task(taskfunc)
            assert value == 2
            with pytest.raises(Exception, match='foo'):
                future.result()
コード例 #20
0
    def test_contextvar_propagation_sync(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        if anyio_backend_name == "asyncio" and sys.version_info < (3, 7):
            pytest.skip("Asyncio does not propagate context before Python 3.7")

        var = ContextVar("var", default=1)
        var.set(6)
        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            propagated_value = portal.call(var.get)

        assert propagated_value == 6
コード例 #21
0
    def test_start_crash_after_started_call(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        def taskfunc(*, task_status: TaskStatus) -> NoReturn:
            task_status.started(2)
            raise Exception("foo")

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future, value = portal.start_task(taskfunc)
            assert value == 2
            with pytest.raises(Exception, match="foo"):
                future.result()
コード例 #22
0
    def test_start_task_soon(self, anyio_backend_name, anyio_backend_options):
        async def event_waiter():
            await event1.wait()
            event2.set()
            return 'test'

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            event1 = portal.call(Event)
            event2 = portal.call(Event)
            future = portal.start_task_soon(event_waiter)
            portal.call(event1.set)
            portal.call(event2.wait)
            assert future.result() == 'test'
コード例 #23
0
    def test_start_task_soon_with_name(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        task_name = None

        async def taskfunc() -> None:
            nonlocal task_name
            task_name = get_current_task().name

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            portal.start_task_soon(taskfunc, name="testname")

        assert task_name == "testname"
コード例 #24
0
    def test_start_task_soon(self, anyio_backend_name: str,
                             anyio_backend_options: Dict[str, Any]) -> None:
        async def event_waiter() -> Literal["test"]:
            await event1.wait()
            event2.set()
            return "test"

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            event1 = portal.call(Event)
            event2 = portal.call(Event)
            future = portal.start_task_soon(event_waiter)
            portal.call(event1.set)
            portal.call(event2.wait)
            assert future.result() == "test"
コード例 #25
0
    def test_start_task_soon_cancel_immediately(self, anyio_backend_name,
                                                anyio_backend_options):
        async def event_waiter():
            nonlocal cancelled
            try:
                await sleep(3)
            except get_cancelled_exc_class():
                cancelled = True

        cancelled = False
        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            future = portal.start_task_soon(event_waiter)
            future.cancel()

        assert cancelled
コード例 #26
0
    def test_async_context_manager_exception_in_task_group(
            self, anyio_backend_name: str,
            anyio_backend_options: Dict[str, Any]) -> None:
        """Regression test for #381."""
        async def failing_func() -> None:
            0 / 0

        @asynccontextmanager
        async def run_in_context() -> AsyncGenerator[None, None]:
            async with create_task_group() as tg:
                tg.start_soon(failing_func)
                yield

        with start_blocking_portal(anyio_backend_name,
                                   anyio_backend_options) as portal:
            with pytest.raises(ZeroDivisionError):
                with portal.wrap_async_context_manager(run_in_context()):
                    pass
コード例 #27
0
    def test_start_with_nonexistent_backend(self) -> None:
        with pytest.raises(LookupError) as exc:
            with start_blocking_portal("foo"):
                pass

        exc.match("No such backend: foo")