async def test_with_portal_run(library): for test in (test_simple, test_complex): await greenback.with_portal_run(test, library) await greenback.with_portal_run(greenback.with_portal_run, test, library) with pytest.raises(RuntimeError, match="greenback.ensure_portal"): await_(anyio.sleep(0)) await greenback.with_portal_run_sync(lambda: await_(test(library))) await greenback.with_portal_run_sync(lambda: await_( greenback.with_portal_run_sync(lambda: await_(test(library))))) with pytest.raises(RuntimeError, match="greenback.ensure_portal"): await_(anyio.sleep(0))
async def one_task(*, have_portal=False): if not have_portal: with pytest.raises( RuntimeError, match="you must 'await greenback.ensure_portal" ): await_(anyio.sleep(0)) await ensure_portal() await ensure_portal() for _ in range(100): nonlocal ticks await_(anyio.sleep(0)) await anyio.sleep(0) ticks += 1
async def test_bestow(library): task = None task_started = anyio.create_event() portal_installed = anyio.create_event() async def task_fn(): nonlocal task task = greenback._impl.current_task() await task_started.set() await portal_installed.wait() await_(anyio.sleep(0)) async with anyio.create_task_group() as tg: await tg.spawn(task_fn) await task_started.wait() greenback.bestow_portal(task) greenback.bestow_portal(task) await portal_installed.set() with pytest.raises(RuntimeError): await_(anyio.sleep(0))
def __await__(self): yield from anyio.sleep(0).__await__() if self._fail: raise ValueError("nope") return "it works!"
async def task_fn(): nonlocal task task = greenback._impl.current_task() task_started.set() await portal_installed.wait() await_(anyio.sleep(0))