Port `test_spawning` off `run_in_actor`
Migrate all 4 sites to blocking `tractor.to_actor.run()` (#477 removal), - rename the two API-named tests to `test_to_actor_run_*` (`same_func_in_child`, `can_skip_parent_main_inheritance`) — they exercise the same spawn / `inherit_parent_main` path via the successor API. - the recursive `spawn()` helper drops its white-box `an._children` / portal-`_peers` asserts (which probed `run_in_actor`'s portal + nursery-tracking internals); `to_actor.run()` returns the result and reaps internally, so keep the user-facing `result == 10` check. - `test_most_beautiful_word` drops the 2nd `wait_for_result()` (the legacy result-cache re-fetch) — `to_actor.run()` delivers the value once, no cache. Suite: 9 passed. (this patch was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-codedrop_ria_nursery
parent
8253183b79
commit
dfa2be7078
|
|
@ -48,9 +48,11 @@ async def spawn(
|
||||||
actor: tractor.Actor = tractor.current_actor()
|
actor: tractor.Actor = tractor.current_actor()
|
||||||
assert actor.is_registrar == should_be_root
|
assert actor.is_registrar == should_be_root
|
||||||
|
|
||||||
# spawns subproc here
|
# recursively spawn this same `spawn()` fn as the lone
|
||||||
portal: tractor.Portal = await an.run_in_actor(
|
# task of a one-shot child subactor and get its result.
|
||||||
fn=spawn,
|
result = await tractor.to_actor.run(
|
||||||
|
spawn,
|
||||||
|
an=an,
|
||||||
|
|
||||||
# spawning args
|
# spawning args
|
||||||
name='sub-actor',
|
name='sub-actor',
|
||||||
|
|
@ -62,16 +64,6 @@ async def spawn(
|
||||||
data=data_to_pass_down,
|
data=data_to_pass_down,
|
||||||
reg_addr=reg_addr,
|
reg_addr=reg_addr,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(an._children) == 1
|
|
||||||
assert (
|
|
||||||
portal.channel.aid.uid
|
|
||||||
in
|
|
||||||
tractor.current_actor().ipc_server._peers
|
|
||||||
)
|
|
||||||
|
|
||||||
# get result from child subactor
|
|
||||||
result = await portal.result()
|
|
||||||
assert result == 10
|
assert result == 10
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
|
|
@ -79,7 +71,7 @@ async def spawn(
|
||||||
return 10
|
return 10
|
||||||
|
|
||||||
|
|
||||||
def test_run_in_actor_same_func_in_child(
|
def test_to_actor_run_same_func_in_child(
|
||||||
reg_addr: tuple,
|
reg_addr: tuple,
|
||||||
debug_mode: bool,
|
debug_mode: bool,
|
||||||
):
|
):
|
||||||
|
|
@ -159,21 +151,16 @@ async def test_most_beautiful_word(
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
debug_mode=debug_mode,
|
debug_mode=debug_mode,
|
||||||
) as an:
|
) as an:
|
||||||
portal = await an.run_in_actor(
|
res: Any = await tractor.to_actor.run(
|
||||||
cellar_door,
|
cellar_door,
|
||||||
|
an=an,
|
||||||
return_value=return_value,
|
return_value=return_value,
|
||||||
name='some_linguist',
|
name='some_linguist',
|
||||||
)
|
)
|
||||||
|
|
||||||
res: Any = await portal.wait_for_result()
|
|
||||||
assert res == return_value
|
assert res == return_value
|
||||||
# The ``async with`` will unblock here since the 'some_linguist'
|
# The ``async with`` unblocks here — the 'some_linguist'
|
||||||
# actor has completed its main task ``cellar_door``.
|
# one-shot actor completed its lone task ``cellar_door`` and
|
||||||
|
# was reaped by `to_actor.run()`.
|
||||||
# this should pull the cached final result already captured during
|
|
||||||
# the nursery block exit.
|
|
||||||
res: Any = await portal.wait_for_result()
|
|
||||||
assert res == return_value
|
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -216,8 +203,9 @@ def test_loglevel_propagated_to_subactor(
|
||||||
registry_addrs=[reg_addr],
|
registry_addrs=[reg_addr],
|
||||||
|
|
||||||
) as tn:
|
) as tn:
|
||||||
await tn.run_in_actor(
|
await tractor.to_actor.run(
|
||||||
check_loglevel,
|
check_loglevel,
|
||||||
|
an=tn,
|
||||||
loglevel=level,
|
loglevel=level,
|
||||||
level=level,
|
level=level,
|
||||||
)
|
)
|
||||||
|
|
@ -267,11 +255,11 @@ async def check_parent_main_inheritance(
|
||||||
return has_data
|
return has_data
|
||||||
|
|
||||||
|
|
||||||
def test_run_in_actor_can_skip_parent_main_inheritance(
|
def test_to_actor_run_can_skip_parent_main_inheritance(
|
||||||
start_method: str, # <- only support on `trio` backend rn.
|
start_method: str, # <- only support on `trio` backend rn.
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Verify ``inherit_parent_main=False`` on ``run_in_actor()``
|
Verify ``inherit_parent_main=False`` on ``to_actor.run()``
|
||||||
prevents parent ``__main__`` data from reaching the child.
|
prevents parent ``__main__`` data from reaching the child.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
@ -284,21 +272,21 @@ def test_run_in_actor_can_skip_parent_main_inheritance(
|
||||||
async with tractor.open_nursery(start_method='trio') as an:
|
async with tractor.open_nursery(start_method='trio') as an:
|
||||||
|
|
||||||
# Default: child receives parent __main__ bootstrap data
|
# Default: child receives parent __main__ bootstrap data
|
||||||
replaying = await an.run_in_actor(
|
await tractor.to_actor.run(
|
||||||
check_parent_main_inheritance,
|
check_parent_main_inheritance,
|
||||||
|
an=an,
|
||||||
name='replaying-parent-main',
|
name='replaying-parent-main',
|
||||||
expect_inherited=True,
|
expect_inherited=True,
|
||||||
)
|
)
|
||||||
await replaying.result()
|
|
||||||
|
|
||||||
# Opt-out: child gets no parent __main__ data
|
# Opt-out: child gets no parent __main__ data
|
||||||
isolated = await an.run_in_actor(
|
await tractor.to_actor.run(
|
||||||
check_parent_main_inheritance,
|
check_parent_main_inheritance,
|
||||||
|
an=an,
|
||||||
name='isolated-parent-main',
|
name='isolated-parent-main',
|
||||||
inherit_parent_main=False,
|
inherit_parent_main=False,
|
||||||
expect_inherited=False,
|
expect_inherited=False,
|
||||||
)
|
)
|
||||||
await isolated.result()
|
|
||||||
|
|
||||||
trio.run(main)
|
trio.run(main)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue