Simplify to form submitted to njs

aio_explicit_task_cancels
Tyler Goodlet 2022-02-25 12:41:35 -05:00
parent faf751acac
commit e45251db56
1 changed files with 33 additions and 38 deletions

View File

@ -10,6 +10,7 @@ from contextlib import asynccontextmanager as acm
import pytest import pytest
import trio import trio
from trio_typing import TaskStatus
import tractor import tractor
from tractor import RemoteActorError from tractor import RemoteActorError
from async_generator import aclosing from async_generator import aclosing
@ -172,64 +173,58 @@ def test_actor_managed_trio_nursery_task_error_cancels_aio(
assert isinstance(err.type(), NameError) assert isinstance(err.type(), NameError)
from trio_typing import TaskStatus def test_stashed_child_nursery():
_child_nursery = None
def test_nursery():
_cn = None
async def task_name_errors_and_never_startedz(
task_status: TaskStatus = trio.TASK_STATUS_IGNORED,
):
await trio.sleep(0.5)
# pet pet
doggy()
# we never get here due to name error
task_status.started()
async def waits_on_signal( async def waits_on_signal(
ev: trio.Event(), ev: trio.Event(),
task_status: TaskStatus[trio.Nursery] = trio.TASK_STATUS_IGNORED, task_status: TaskStatus[trio.Nursery] = trio.TASK_STATUS_IGNORED,
): ):
'''
Do some stuf, then signal other tasks, then yield back to "starter".
'''
await ev.wait() await ev.wait()
task_status.started() task_status.started()
async def start_cn_tasks( async def mk_child_nursery(
# pn: trio.Nursery,
task_status: TaskStatus[trio.Nursery] = trio.TASK_STATUS_IGNORED,
):
nonlocal _cn
assert _cn
ev = trio.Event()
_cn.start_soon(task_never_starteds)
await _cn.start(waits_on_signal, ev)
task_status.started()
async def mk_cn(
task_status: TaskStatus = trio.TASK_STATUS_IGNORED, task_status: TaskStatus = trio.TASK_STATUS_IGNORED,
): ):
nonlocal _cn '''
Allocate a child sub-nursery and stash it as a global.
'''
nonlocal _child_nursery
async with trio.open_nursery() as cn: async with trio.open_nursery() as cn:
_cn = cn _child_nursery = cn
task_status.started(cn) task_status.started(cn)
# block until cancelled by parent.
await trio.sleep_forever() await trio.sleep_forever()
async def sleep_and_err(ev: trio.Event):
await trio.sleep(0.5)
doggy() # noqa
ev.set()
async def main(): async def main():
async with ( async with (
trio.open_nursery() as pn, trio.open_nursery() as pn,
): ):
cn = await pn.start(mk_cn) cn = await pn.start(mk_child_nursery)
assert cn assert cn
# start a parent level task which starts a task ev = trio.Event()
pn.start_soon(start_cn_tasks) cn.start_soon(sleep_and_err, ev)
await trio.sleep_forever()
# this causes inf hang
await cn.start(waits_on_signal, ev)
# this does not.
# cn.start_soon(waits_on_signal, ev)
with pytest.raises(NameError):
trio.run(main) trio.run(main)