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'
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')
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')
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'
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)
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
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")
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")
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)
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)
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]
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')
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()
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
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"
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()
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")
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'
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()
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
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()
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'
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"
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"
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
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
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")