Add a basic `tractor.run()` test

kivy_mainline_and_py3.8
Tyler Goodlet 2018-06-12 15:23:58 -04:00
parent bf2f6769fc
commit acb5dc398e
1 changed files with 35 additions and 6 deletions

View File

@ -1,7 +1,10 @@
""" """
Actor model API testing Actor model API testing
""" """
import time
import pytest import pytest
import trio
from piker import tractor from piker import tractor
@ -10,14 +13,39 @@ def us_symbols():
return ['TSLA', 'AAPL', 'CGC', 'CRON'] return ['TSLA', 'AAPL', 'CGC', 'CRON']
@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):
with tractor.open_nursery() as nursery:
pass
def test_local_actor_async_gen():
"""Verify a simple async function in-process.
"""
async def print_loop(actor):
for i in range(10):
await trio.sleep(0.1)
start = time.time()
tractor.run(print_loop)
# ensure the sleeps were actually awaited
assert time.time() - start >= 1
@pytest.mark.trio @pytest.mark.trio
async def test_rx_price_quotes_from_brokerd(us_symbols): async def test_rx_price_quotes_from_brokerd(us_symbols):
"""Verify we can spawn a daemon actor and retrieve streamed price data. """Verify we can spawn a daemon actor and retrieve streamed price data.
""" """
async with tractor.find_actor('brokerd') as portals:
async with tractor.open_nursery() as nursery: async with tractor.open_nursery() as nursery:
# only one per host address, spawns an actor if None # only one per host address, spawns an actor if None
async with tractor.find_actors('brokerd') as portals:
if not portals: if not portals:
# no brokerd actor found # no brokerd actor found
portal = await nursery.start_actor( portal = await nursery.start_actor(
@ -28,7 +56,7 @@ async def test_rx_price_quotes_from_brokerd(us_symbols):
'clients': {}, 'clients': {},
'dtasks': set() 'dtasks': set()
}, },
main=None, main=None, # don't start a main func - use rpc
) )
# gotta expose in a broker agnostic way... # gotta expose in a broker agnostic way...
@ -53,5 +81,6 @@ async def test_rx_price_quotes_from_brokerd(us_symbols):
# await gen.asend(None) # await gen.asend(None)
# break # break
await nursery.cancel()
# arbitter is cancelled here due to `find_actors()` internals # arbitter is cancelled here due to `find_actors()` internals
# (which internally uses `get_arbiter` which kills the root
# nursery on __exit__)