Update test to new api

drop_main_kwarg
Tyler Goodlet 2018-08-02 15:27:09 -04:00
parent bb13b79df5
commit dadea87451
1 changed files with 30 additions and 37 deletions

View File

@ -76,19 +76,19 @@ async def spawn(is_arbiter):
await trio.sleep(0.1) await trio.sleep(0.1)
actor = tractor.current_actor() actor = tractor.current_actor()
assert actor.is_arbiter == is_arbiter assert actor.is_arbiter == is_arbiter
# arbiter should always have an empty statespace as it's redundant
assert actor.statespace == statespace assert actor.statespace == statespace
if actor.is_arbiter: if actor.is_arbiter:
async with tractor.open_nursery() as nursery: async with tractor.open_nursery() as nursery:
# forks here # forks here
portal = await nursery.start_actor( portal = await nursery.run_in_actor(
'sub-actor', 'sub-actor',
main=partial(spawn, False), spawn,
is_arbiter=False,
statespace=statespace, statespace=statespace,
rpc_module_paths=namespaces, rpc_module_paths=namespaces,
) )
assert len(nursery._children) == 1 assert len(nursery._children) == 1
assert portal.channel.uid in tractor.current_actor()._peers assert portal.channel.uid in tractor.current_actor()._peers
# be sure we can still get the result # be sure we can still get the result
@ -128,9 +128,6 @@ async def stream_from_single_subactor():
'streamerd', 'streamerd',
rpc_module_paths=[__name__], rpc_module_paths=[__name__],
statespace={'global_dict': {}}, statespace={'global_dict': {}},
# don't start a main func - use rpc
# currently the same as outlive_main=False
main=None,
) )
seq = range(10) seq = range(10)
@ -172,7 +169,7 @@ def test_remote_error():
async def main(): async def main():
async with tractor.open_nursery() as nursery: async with tractor.open_nursery() as nursery:
portal = await nursery.start_actor('errorer', main=assert_err) portal = await nursery.run_in_actor('errorer', assert_err)
# get result(s) from main task # get result(s) from main task
try: try:
@ -204,7 +201,6 @@ async def test_cancel_infinite_streamer():
portal = await n.start_actor( portal = await n.start_actor(
f'donny', f'donny',
rpc_module_paths=[__name__], rpc_module_paths=[__name__],
outlive_main=True
) )
async for letter in await portal.run(__name__, 'stream_forever'): async for letter in await portal.run(__name__, 'stream_forever'):
print(letter) print(letter)
@ -227,17 +223,17 @@ async def test_one_cancels_all():
real_actors.append(await n.start_actor( real_actors.append(await n.start_actor(
f'actor_{i}', f'actor_{i}',
rpc_module_paths=[__name__], rpc_module_paths=[__name__],
outlive_main=True
)) ))
# start one actor that will fail immediately # start one actor that will fail immediately
await n.start_actor('extra', main=assert_err) await n.run_in_actor('extra', assert_err)
# should error here with a ``RemoteActorError`` containing # should error here with a ``RemoteActorError`` containing
# an ``AssertionError` # an ``AssertionError`
except tractor.RemoteActorError: except tractor.RemoteActorError:
assert n.cancelled is True assert n.cancelled is True
assert not n._children
else: else:
pytest.fail("Should have gotten a remote assertion error?") pytest.fail("Should have gotten a remote assertion error?")
@ -263,16 +259,15 @@ async def test_trynamic_trio():
async with tractor.open_nursery() as n: async with tractor.open_nursery() as n:
print("Alright... Action!") print("Alright... Action!")
donny = await n.start_actor( donny = await n.run_in_actor(
'donny', 'donny',
main=partial(say_hello, 'gretchen'), say_hello,
rpc_module_paths=[__name__], other_actor='gretchen',
outlive_main=True
) )
gretchen = await n.start_actor( gretchen = await n.run_in_actor(
'gretchen', 'gretchen',
main=partial(say_hello, 'donny'), say_hello,
rpc_module_paths=[__name__], other_actor='donny',
) )
print(await gretchen.result()) print(await gretchen.result())
print(await donny.result()) print(await donny.result())
@ -296,7 +291,6 @@ async def test_movie_theatre_convo():
'frank', 'frank',
# enable the actor to run funcs from this current module # enable the actor to run funcs from this current module
rpc_module_paths=[__name__], rpc_module_paths=[__name__],
outlive_main=True,
) )
print(await portal.run(__name__, 'movie_theatre_question')) print(await portal.run(__name__, 'movie_theatre_question'))
@ -304,18 +298,18 @@ async def test_movie_theatre_convo():
print(await portal.run(__name__, 'movie_theatre_question')) print(await portal.run(__name__, 'movie_theatre_question'))
# the async with will block here indefinitely waiting # the async with will block here indefinitely waiting
# for our actor "frank" to complete, but since it's an # for our actor "frank" to complete, we cancel 'frank'
# "outlive_main" actor it will never end until cancelled # to avoid blocking indefinitely
await portal.cancel_actor() await portal.cancel_actor()
@tractor_test @tractor_test
async def test_movie_theatre_convo_main_task(): async def test_movie_theatre_convo_main_task():
async with tractor.open_nursery() as n: async with tractor.open_nursery() as n:
portal = await n.start_actor('some_linguist', main=cellar_door) portal = await n.run_in_actor('frank', movie_theatre_question)
# The ``async with`` will unblock here since the 'some_linguist' # The ``async with`` will unblock here since the 'frank'
# actor has completed its main task ``cellar_door``. # actor has completed its main task ``movie_theatre_question()``.
print(await portal.result()) print(await portal.result())
@ -329,7 +323,7 @@ async def test_most_beautiful_word():
"""The main ``tractor`` routine. """The main ``tractor`` routine.
""" """
async with tractor.open_nursery() as n: async with tractor.open_nursery() as n:
portal = await n.start_actor('some_linguist', main=cellar_door) portal = await n.run_in_actor('some_linguist', cellar_door)
# The ``async with`` will unblock here since the 'some_linguist' # The ``async with`` will unblock here since the 'some_linguist'
# actor has completed its main task ``cellar_door``. # actor has completed its main task ``cellar_door``.
@ -375,7 +369,6 @@ async def aggregate(seed):
portal = await nursery.start_actor( portal = await nursery.start_actor(
name=f'streamer_{i}', name=f'streamer_{i}',
rpc_module_paths=[__name__], rpc_module_paths=[__name__],
outlive_main=True, # daemonize these actors
) )
portals.append(portal) portals.append(portal)
@ -416,7 +409,6 @@ async def aggregate(seed):
print("AGGREGATOR COMPLETE!") print("AGGREGATOR COMPLETE!")
# @tractor_test
async def a_quadruple_example(): async def a_quadruple_example():
# a nursery which spawns "actors" # a nursery which spawns "actors"
async with tractor.open_nursery() as nursery: async with tractor.open_nursery() as nursery:
@ -424,10 +416,10 @@ async def a_quadruple_example():
seed = int(1e3) seed = int(1e3)
pre_start = time.time() pre_start = time.time()
portal = await nursery.start_actor( portal = await nursery.run_in_actor(
name='aggregator', 'aggregator',
# executed in the actor's "main task" immediately aggregate,
main=partial(aggregate, seed), seed=seed,
) )
start = time.time() start = time.time()
@ -449,17 +441,18 @@ async def cancel_after(wait):
def test_a_quadruple_example(): def test_a_quadruple_example():
"""Verify the *show me the code* readme example works. """This also serves as a kind of "we'd like to eventually be this
fast test".
""" """
results = tractor.run(cancel_after, 2, arbiter_addr=_arb_addr) results = tractor.run(cancel_after, 2.1, arbiter_addr=_arb_addr)
assert results assert results
def test_not_fast_enough_quad(): @pytest.mark.parametrize('cancel_delay', list(range(1, 8)))
def test_not_fast_enough_quad(cancel_delay):
"""Verify we can cancel midway through the quad example and all actors """Verify we can cancel midway through the quad example and all actors
cancel gracefully. cancel gracefully.
This also serves as a kind of "we'd like to eventually be this fast test".
""" """
results = tractor.run(cancel_after, 1, arbiter_addr=_arb_addr) delay = 1 + cancel_delay/10
results = tractor.run(cancel_after, delay, arbiter_addr=_arb_addr)
assert results is None assert results is None