Update all tests to new streaming API
parent
5a5e6baad1
commit
2498a4963b
|
@ -49,7 +49,9 @@ def test_remote_error(arb_addr, args_err):
|
|||
async def main():
|
||||
async with tractor.open_nursery() as nursery:
|
||||
|
||||
portal = await nursery.run_in_actor(assert_err, name='errorer', **args)
|
||||
portal = await nursery.run_in_actor(
|
||||
assert_err, name='errorer', **args
|
||||
)
|
||||
|
||||
# get result(s) from main task
|
||||
try:
|
||||
|
@ -168,12 +170,13 @@ async def test_cancel_infinite_streamer(start_method):
|
|||
async with tractor.open_nursery() as n:
|
||||
portal = await n.start_actor(
|
||||
'donny',
|
||||
rpc_module_paths=[__name__],
|
||||
enable_modules=[__name__],
|
||||
)
|
||||
|
||||
# this async for loop streams values from the above
|
||||
# async generator running in a separate process
|
||||
async for letter in await portal.run(stream_forever):
|
||||
async with portal.open_stream_from(stream_forever) as stream:
|
||||
async for letter in stream:
|
||||
print(letter)
|
||||
|
||||
# we support trio's cancellation system
|
||||
|
@ -430,7 +433,6 @@ def test_cancel_via_SIGINT_other_task(
|
|||
tractor.run(main)
|
||||
|
||||
|
||||
|
||||
async def spin_for(period=3):
|
||||
"Sync sleep."
|
||||
time.sleep(period)
|
||||
|
@ -438,7 +440,7 @@ async def spin_for(period=3):
|
|||
|
||||
async def spawn():
|
||||
async with tractor.open_nursery() as tn:
|
||||
portal = await tn.run_in_actor(
|
||||
await tn.run_in_actor(
|
||||
spin_for,
|
||||
name='sleeper',
|
||||
)
|
||||
|
@ -460,7 +462,7 @@ def test_cancel_while_childs_child_in_sync_sleep(
|
|||
async def main():
|
||||
with trio.fail_after(2):
|
||||
async with tractor.open_nursery() as tn:
|
||||
portal = await tn.run_in_actor(
|
||||
await tn.run_in_actor(
|
||||
spawn,
|
||||
name='spawn',
|
||||
)
|
||||
|
|
|
@ -108,7 +108,8 @@ async def cancel(use_signal, delay=0):
|
|||
|
||||
|
||||
async def stream_from(portal):
|
||||
async for value in await portal.result():
|
||||
async with portal.open_stream_from(stream_forever) as stream:
|
||||
async for value in stream:
|
||||
print(value)
|
||||
|
||||
|
||||
|
@ -139,18 +140,20 @@ async def spawn_and_check_registry(
|
|||
registry = await get_reg()
|
||||
assert actor.uid in registry
|
||||
|
||||
if with_streaming:
|
||||
to_run = stream_forever
|
||||
else:
|
||||
to_run = trio.sleep_forever
|
||||
|
||||
async with trio.open_nursery() as trion:
|
||||
try:
|
||||
async with tractor.open_nursery() as n:
|
||||
async with trio.open_nursery() as trion:
|
||||
|
||||
portals = {}
|
||||
for i in range(3):
|
||||
name = f'a{i}'
|
||||
portals[name] = await n.run_in_actor(to_run, name=name)
|
||||
if with_streaming:
|
||||
portals[name] = await n.start_actor(
|
||||
name=name, enable_modules=[__name__])
|
||||
|
||||
else: # no streaming
|
||||
portals[name] = await n.run_in_actor(
|
||||
trio.sleep_forever, name=name)
|
||||
|
||||
# wait on last actor to come up
|
||||
async with tractor.wait_for_actor(name):
|
||||
|
@ -171,8 +174,8 @@ async def spawn_and_check_registry(
|
|||
trion.start_soon(cancel, use_signal, 1)
|
||||
|
||||
last_p = pts[-1]
|
||||
async for value in await last_p.result():
|
||||
print(value)
|
||||
await stream_from(last_p)
|
||||
|
||||
else:
|
||||
await cancel(use_signal)
|
||||
|
||||
|
@ -260,15 +263,15 @@ async def close_chans_before_nursery(
|
|||
get_reg = partial(aportal.run_from_ns, 'self', 'get_registry')
|
||||
|
||||
async with tractor.open_nursery() as tn:
|
||||
portal1 = await tn.run_in_actor(
|
||||
stream_forever,
|
||||
name='consumer1',
|
||||
)
|
||||
agen1 = await portal1.result()
|
||||
|
||||
portal2 = await tn.start_actor('consumer2', rpc_module_paths=[__name__])
|
||||
agen2 = await portal2.run(stream_forever)
|
||||
portal1 = await tn.start_actor(
|
||||
name='consumer1', enable_modules=[__name__])
|
||||
portal2 = await tn.start_actor(
|
||||
'consumer2', enable_modules=[__name__])
|
||||
|
||||
async with (
|
||||
portal1.open_stream_from(stream_forever) as agen1,
|
||||
portal2.open_stream_from(stream_forever) as agen2,
|
||||
):
|
||||
async with trio.open_nursery() as n:
|
||||
n.start_soon(streamer, agen1)
|
||||
n.start_soon(cancel, use_signal, .5)
|
||||
|
@ -289,7 +292,7 @@ async def close_chans_before_nursery(
|
|||
await agen2.aclose()
|
||||
finally:
|
||||
with trio.CancelScope(shield=True):
|
||||
await trio.sleep(.5)
|
||||
await trio.sleep(1)
|
||||
|
||||
# all subactors should have de-registered
|
||||
registry = await get_reg()
|
||||
|
|
|
@ -61,11 +61,13 @@ async def subs(
|
|||
return isinstance(i, int)
|
||||
|
||||
async with tractor.find_actor(pub_actor_name) as portal:
|
||||
stream = await portal.run(
|
||||
async with (
|
||||
portal.open_stream_from(
|
||||
pubber,
|
||||
topics=which,
|
||||
seed=seed,
|
||||
)
|
||||
) as stream
|
||||
):
|
||||
task_status.started(stream)
|
||||
times = 10
|
||||
count = 0
|
||||
|
@ -79,12 +81,13 @@ async def subs(
|
|||
|
||||
await stream.aclose()
|
||||
|
||||
stream = await portal.run(
|
||||
async with (
|
||||
portal.open_stream_from(
|
||||
pubber,
|
||||
topics=['odd'],
|
||||
seed=seed,
|
||||
)
|
||||
|
||||
) as stream
|
||||
):
|
||||
await stream.__anext__()
|
||||
count = 0
|
||||
# async with aclosing(stream) as stream:
|
||||
|
@ -128,11 +131,10 @@ async def test_required_args(callwith_expecterror):
|
|||
await func(**kwargs)
|
||||
else:
|
||||
async with tractor.open_nursery() as n:
|
||||
# await func(**kwargs)
|
||||
portal = await n.run_in_actor(
|
||||
multilock_pubber,
|
||||
|
||||
portal = await n.start_actor(
|
||||
name='pubber',
|
||||
**kwargs
|
||||
enable_modules=[__name__],
|
||||
)
|
||||
|
||||
async with tractor.wait_for_actor('pubber'):
|
||||
|
@ -140,9 +142,15 @@ async def test_required_args(callwith_expecterror):
|
|||
|
||||
await trio.sleep(0.5)
|
||||
|
||||
async for val in await portal.result():
|
||||
async with portal.open_stream_from(
|
||||
multilock_pubber,
|
||||
**kwargs
|
||||
) as stream:
|
||||
async for val in stream:
|
||||
assert val == {'doggy': 10}
|
||||
|
||||
await portal.cancel_actor()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'pub_actor',
|
||||
|
|
|
@ -61,15 +61,16 @@ async def stream_from_single_subactor(stream_func):
|
|||
# no brokerd actor found
|
||||
portal = await nursery.start_actor(
|
||||
'streamerd',
|
||||
rpc_module_paths=[__name__],
|
||||
enable_modules=[__name__],
|
||||
)
|
||||
|
||||
seq = range(10)
|
||||
|
||||
stream = await portal.run(
|
||||
stream_func, # one of the funcs above
|
||||
async with portal.open_stream_from(
|
||||
stream_func,
|
||||
sequence=list(seq), # has to be msgpack serializable
|
||||
)
|
||||
) as stream:
|
||||
|
||||
# it'd sure be nice to have an asyncitertools here...
|
||||
iseq = iter(seq)
|
||||
ival = next(iseq)
|
||||
|
@ -132,7 +133,7 @@ async def aggregate(seed):
|
|||
# fork point
|
||||
portal = await nursery.start_actor(
|
||||
name=f'streamer_{i}',
|
||||
rpc_module_paths=[__name__],
|
||||
enable_modules=[__name__],
|
||||
)
|
||||
|
||||
portals.append(portal)
|
||||
|
@ -141,9 +142,12 @@ async def aggregate(seed):
|
|||
|
||||
async def push_to_chan(portal, send_chan):
|
||||
async with send_chan:
|
||||
async for value in await portal.run(
|
||||
__name__, 'stream_data', seed=seed
|
||||
):
|
||||
|
||||
async with portal.open_stream_from(
|
||||
stream_data, seed=seed,
|
||||
) as stream:
|
||||
|
||||
async for value in stream:
|
||||
# leverage trio's built-in backpressure
|
||||
await send_chan.send(value)
|
||||
|
||||
|
@ -183,22 +187,24 @@ async def a_quadruple_example():
|
|||
seed = int(1e3)
|
||||
pre_start = time.time()
|
||||
|
||||
portal = await nursery.run_in_actor(
|
||||
aggregate,
|
||||
seed=seed,
|
||||
portal = await nursery.start_actor(
|
||||
name='aggregator',
|
||||
enable_modules=[__name__],
|
||||
)
|
||||
|
||||
start = time.time()
|
||||
# the portal call returns exactly what you'd expect
|
||||
# as if the remote "aggregate" function was called locally
|
||||
result_stream = []
|
||||
async for value in await portal.result():
|
||||
|
||||
async with portal.open_stream_from(aggregate, seed=seed) as stream:
|
||||
async for value in stream:
|
||||
result_stream.append(value)
|
||||
|
||||
print(f"STREAM TIME = {time.time() - start}")
|
||||
print(f"STREAM + SPAWN TIME = {time.time() - pre_start}")
|
||||
assert result_stream == list(range(seed))
|
||||
await portal.cancel_actor()
|
||||
return result_stream
|
||||
|
||||
|
||||
|
@ -272,11 +278,14 @@ async def test_respawn_consumer_task(
|
|||
|
||||
async with tractor.open_nursery() as n:
|
||||
|
||||
stream = await(await n.run_in_actor(
|
||||
portal = await n.start_actor(
|
||||
name='streamer',
|
||||
enable_modules=[__name__]
|
||||
)
|
||||
async with portal.open_stream_from(
|
||||
stream_data,
|
||||
seed=11,
|
||||
name='streamer',
|
||||
)).result()
|
||||
) as stream:
|
||||
|
||||
expect = set(range(11))
|
||||
received = []
|
||||
|
@ -317,3 +326,7 @@ async def test_respawn_consumer_task(
|
|||
if not expect:
|
||||
print("all values streamed, BREAKING")
|
||||
break
|
||||
|
||||
# TODO: this is justification for a
|
||||
# ``ActorNursery.stream_from_actor()`` helper?
|
||||
await portal.cancel_actor()
|
||||
|
|
Loading…
Reference in New Issue