forked from goodboy/tractor
1
0
Fork 0

Get tests working again

Remove all the `piker` stuff and add some further checks including:
- main task result is returned correctly
- remote errors are raised locally
- remote async generator yields values locally
asyncgen_closing_fix
Tyler Goodlet 2018-07-06 02:45:26 -04:00
parent 36fd75e217
commit 10417303aa
1 changed files with 58 additions and 33 deletions

View File

@ -52,7 +52,7 @@ def test_local_actor_async_func():
# interal pickling infra of the forkserver to work # interal pickling infra of the forkserver to work
async def spawn(is_arbiter): async def spawn(is_arbiter):
statespace = {'doggy': 10, 'kitty': 4} statespace = {'doggy': 10, 'kitty': 4}
namespaces = ['piker.brokers.core'] namespaces = [__name__]
await trio.sleep(0.1) await trio.sleep(0.1)
actor = tractor.current_actor() actor = tractor.current_actor()
@ -72,22 +72,33 @@ async def spawn(is_arbiter):
) )
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
result = await portal.result()
assert result == 10
return result
else: else:
return 10 return 10
def test_local_arbiter_subactor_global_state(): def test_local_arbiter_subactor_global_state():
statespace = {'doggy': 10, 'kitty': 4} statespace = {'doggy': 10, 'kitty': 4}
tractor.run( result = tractor.run(
spawn, spawn,
True, True,
name='arbiter', name='arbiter',
statespace=statespace, statespace=statespace,
) )
assert result == 10
async def rx_price_quotes_from_brokerd(us_symbols): async def stream_seq(sequence):
"""Verify we can spawn a daemon actor and retrieve streamed price data. for i in sequence:
yield i
await trio.sleep(0.1)
async def stream_from_single_subactor():
"""Verify we can spawn a daemon actor and retrieve streamed data.
""" """
async with tractor.find_actor('brokerd') as portals: async with tractor.find_actor('brokerd') as portals:
if not portals: if not portals:
@ -95,33 +106,25 @@ async def rx_price_quotes_from_brokerd(us_symbols):
async with tractor.open_nursery() as nursery: async with tractor.open_nursery() as nursery:
# no brokerd actor found # no brokerd actor found
portal = await nursery.start_actor( portal = await nursery.start_actor(
'brokerd', 'streamerd',
rpc_module_paths=['piker.brokers.core'], rpc_module_paths=[__name__],
statespace={ statespace={'global_dict': {}},
'brokers2tickersubs': {}, # don't start a main func - use rpc
'clients': {}, # currently the same as outlive_main=False
'dtasks': set() main=None,
},
main=None, # don't start a main func - use rpc
) )
# gotta expose in a broker agnostic way... seq = range(10)
# retrieve initial symbol data
# sd = await portal.run(
# 'piker.brokers.core', 'symbol_data', symbols=us_symbols)
# assert list(sd.keys()) == us_symbols
gen = await portal.run( gen = await portal.run(
'piker.brokers.core', __name__,
'_test_price_stream', 'stream_seq', # the func above
broker='robinhood', sequence=list(seq), # has to be msgpack serializable
symbols=us_symbols,
) )
# it'd sure be nice to have an asyncitertools here... # it'd sure be nice to have an asyncitertools here...
async for quotes in gen: iseq = iter(seq)
assert quotes async for val in gen:
for key in quotes: assert val == next(iseq)
assert key in us_symbols
break break
# terminate far-end async-gen # terminate far-end async-gen
# await gen.asend(None) # await gen.asend(None)
@ -130,14 +133,36 @@ async def rx_price_quotes_from_brokerd(us_symbols):
# stop all spawned subactors # stop all spawned subactors
await nursery.cancel() await nursery.cancel()
# arbitter is cancelled here due to `find_actors()` internals
# (which internally uses `get_arbiter` which kills its channel
# server scope on exit)
def test_stream_from_single_subactor(us_symbols):
def test_rx_price_quotes_from_brokerd(us_symbols): """Verify streaming from a spawned async generator.
"""
tractor.run( tractor.run(
rx_price_quotes_from_brokerd, stream_from_single_subactor,
us_symbols, name='client',
name='arbiter',
) )
async def assert_err():
assert 0
def test_remote_error():
"""Verify an error raises in a subactor is propagated to the parent.
"""
async def main():
async with tractor.open_nursery() as nursery:
portal = await nursery.start_actor('errorer', main=assert_err)
# get result(s) from main task
try:
return await portal.result()
except tractor.RemoteActorError:
raise
except Exception:
pass
assert 0, "Remote error was not raised?"
with pytest.raises(tractor.RemoteActorError):
tractor.run(main)