2018-06-07 04:29:17 +00:00
|
|
|
"""
|
|
|
|
Actor model API testing
|
|
|
|
"""
|
2018-06-12 19:23:58 +00:00
|
|
|
import time
|
2018-06-19 15:49:25 +00:00
|
|
|
from functools import partial
|
2018-06-12 19:23:58 +00:00
|
|
|
|
2018-06-07 04:29:17 +00:00
|
|
|
import pytest
|
2018-06-12 19:23:58 +00:00
|
|
|
import trio
|
2018-07-05 23:49:21 +00:00
|
|
|
import tractor
|
2018-06-07 04:29:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def us_symbols():
|
|
|
|
return ['TSLA', 'AAPL', 'CGC', 'CRON']
|
|
|
|
|
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
@pytest.mark.trio
|
|
|
|
async def test_no_arbitter():
|
|
|
|
"""An arbitter must be established before any nurseries
|
|
|
|
can be created.
|
|
|
|
|
|
|
|
(In other words ``tractor.run`` must be used instead of ``trio.run`` as is
|
|
|
|
done by the ``pytest-trio`` plugin.)
|
|
|
|
"""
|
|
|
|
with pytest.raises(RuntimeError):
|
2018-06-19 15:49:25 +00:00
|
|
|
with tractor.open_nursery():
|
2018-06-12 19:23:58 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-06-19 15:49:25 +00:00
|
|
|
def test_local_actor_async_func():
|
2018-06-12 19:23:58 +00:00
|
|
|
"""Verify a simple async function in-process.
|
|
|
|
"""
|
2018-06-19 15:49:25 +00:00
|
|
|
nums = []
|
|
|
|
|
|
|
|
async def print_loop():
|
|
|
|
# arbiter is started in-proc if dne
|
|
|
|
assert tractor.current_actor().is_arbiter
|
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
for i in range(10):
|
2018-06-19 15:49:25 +00:00
|
|
|
nums.append(i)
|
2018-06-12 19:23:58 +00:00
|
|
|
await trio.sleep(0.1)
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
tractor.run(print_loop)
|
2018-06-19 15:49:25 +00:00
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
# ensure the sleeps were actually awaited
|
|
|
|
assert time.time() - start >= 1
|
2018-06-19 15:49:25 +00:00
|
|
|
assert nums == list(range(10))
|
|
|
|
|
|
|
|
|
|
|
|
# NOTE: this func must be defined at module level in order for the
|
|
|
|
# interal pickling infra of the forkserver to work
|
|
|
|
async def spawn(is_arbiter):
|
|
|
|
statespace = {'doggy': 10, 'kitty': 4}
|
2018-07-06 06:45:26 +00:00
|
|
|
namespaces = [__name__]
|
2018-06-19 15:49:25 +00:00
|
|
|
|
|
|
|
await trio.sleep(0.1)
|
|
|
|
actor = tractor.current_actor()
|
|
|
|
assert actor.is_arbiter == is_arbiter
|
|
|
|
|
|
|
|
# arbiter should always have an empty statespace as it's redundant
|
2018-06-27 15:45:21 +00:00
|
|
|
assert actor.statespace == statespace
|
2018-06-19 15:49:25 +00:00
|
|
|
|
|
|
|
if actor.is_arbiter:
|
|
|
|
async with tractor.open_nursery() as nursery:
|
|
|
|
# forks here
|
|
|
|
portal = await nursery.start_actor(
|
|
|
|
'sub-actor',
|
|
|
|
main=partial(spawn, False),
|
|
|
|
statespace=statespace,
|
|
|
|
rpc_module_paths=namespaces,
|
|
|
|
)
|
|
|
|
assert len(nursery._children) == 1
|
2018-07-05 19:33:02 +00:00
|
|
|
assert portal.channel.uid in tractor.current_actor()._peers
|
2018-07-06 06:45:26 +00:00
|
|
|
# be sure we can still get the result
|
|
|
|
result = await portal.result()
|
|
|
|
assert result == 10
|
|
|
|
return result
|
2018-06-19 15:49:25 +00:00
|
|
|
else:
|
|
|
|
return 10
|
|
|
|
|
|
|
|
|
|
|
|
def test_local_arbiter_subactor_global_state():
|
|
|
|
statespace = {'doggy': 10, 'kitty': 4}
|
2018-07-06 06:45:26 +00:00
|
|
|
result = tractor.run(
|
2018-06-19 15:49:25 +00:00
|
|
|
spawn,
|
|
|
|
True,
|
|
|
|
name='arbiter',
|
|
|
|
statespace=statespace,
|
|
|
|
)
|
2018-07-06 06:45:26 +00:00
|
|
|
assert result == 10
|
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
|
2018-07-06 06:45:26 +00:00
|
|
|
async def stream_seq(sequence):
|
|
|
|
for i in sequence:
|
|
|
|
yield i
|
|
|
|
await trio.sleep(0.1)
|
2018-06-12 19:23:58 +00:00
|
|
|
|
2018-07-06 06:45:26 +00:00
|
|
|
|
|
|
|
async def stream_from_single_subactor():
|
|
|
|
"""Verify we can spawn a daemon actor and retrieve streamed data.
|
2018-06-07 04:29:17 +00:00
|
|
|
"""
|
2018-06-12 19:23:58 +00:00
|
|
|
async with tractor.find_actor('brokerd') as portals:
|
2018-06-19 15:49:25 +00:00
|
|
|
if not portals:
|
2018-06-12 19:23:58 +00:00
|
|
|
# only one per host address, spawns an actor if None
|
2018-06-19 15:49:25 +00:00
|
|
|
async with tractor.open_nursery() as nursery:
|
2018-06-07 04:29:17 +00:00
|
|
|
# no brokerd actor found
|
|
|
|
portal = await nursery.start_actor(
|
2018-07-06 06:45:26 +00:00
|
|
|
'streamerd',
|
|
|
|
rpc_module_paths=[__name__],
|
|
|
|
statespace={'global_dict': {}},
|
|
|
|
# don't start a main func - use rpc
|
|
|
|
# currently the same as outlive_main=False
|
|
|
|
main=None,
|
2018-06-07 04:29:17 +00:00
|
|
|
)
|
|
|
|
|
2018-07-06 06:45:26 +00:00
|
|
|
seq = range(10)
|
2018-06-19 15:49:25 +00:00
|
|
|
|
|
|
|
gen = await portal.run(
|
2018-07-06 06:45:26 +00:00
|
|
|
__name__,
|
|
|
|
'stream_seq', # the func above
|
|
|
|
sequence=list(seq), # has to be msgpack serializable
|
2018-06-19 15:49:25 +00:00
|
|
|
)
|
|
|
|
# it'd sure be nice to have an asyncitertools here...
|
2018-07-06 06:45:26 +00:00
|
|
|
iseq = iter(seq)
|
|
|
|
async for val in gen:
|
|
|
|
assert val == next(iseq)
|
2018-06-19 15:49:25 +00:00
|
|
|
break
|
|
|
|
# terminate far-end async-gen
|
|
|
|
# await gen.asend(None)
|
|
|
|
# break
|
|
|
|
|
|
|
|
# stop all spawned subactors
|
|
|
|
await nursery.cancel()
|
|
|
|
|
2018-07-05 19:33:02 +00:00
|
|
|
|
2018-07-06 06:45:26 +00:00
|
|
|
def test_stream_from_single_subactor(us_symbols):
|
|
|
|
"""Verify streaming from a spawned async generator.
|
|
|
|
"""
|
2018-07-05 19:33:02 +00:00
|
|
|
tractor.run(
|
2018-07-06 06:45:26 +00:00
|
|
|
stream_from_single_subactor,
|
|
|
|
name='client',
|
2018-07-05 19:33:02 +00:00
|
|
|
)
|
2018-07-06 06:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|