2018-06-07 04:29:17 +00:00
|
|
|
"""
|
|
|
|
Actor model API testing
|
|
|
|
"""
|
|
|
|
import pytest
|
2018-07-06 21:27:42 +00:00
|
|
|
import tractor
|
2018-06-07 04:29:17 +00:00
|
|
|
|
|
|
|
|
2018-07-05 19:33:02 +00:00
|
|
|
async def rx_price_quotes_from_brokerd(us_symbols):
|
2018-06-07 04:29:17 +00:00
|
|
|
"""Verify we can spawn a daemon actor and retrieve streamed price data.
|
|
|
|
"""
|
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(
|
|
|
|
'brokerd',
|
2018-06-19 15:49:25 +00:00
|
|
|
rpc_module_paths=['piker.brokers.core'],
|
2018-06-07 04:29:17 +00:00
|
|
|
statespace={
|
2018-11-12 02:59:41 +00:00
|
|
|
'broker2tickersubs': {},
|
2018-06-07 04:29:17 +00:00
|
|
|
'clients': {},
|
|
|
|
'dtasks': set()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-06-19 15:49:25 +00:00
|
|
|
# gotta expose in a broker agnostic way...
|
|
|
|
# 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(
|
|
|
|
'piker.brokers.core',
|
2018-11-12 02:59:41 +00:00
|
|
|
'start_quote_stream',
|
2018-06-19 15:49:25 +00:00
|
|
|
broker='robinhood',
|
2018-11-12 02:59:41 +00:00
|
|
|
tickers=us_symbols,
|
2018-06-19 15:49:25 +00:00
|
|
|
)
|
|
|
|
# it'd sure be nice to have an asyncitertools here...
|
|
|
|
async for quotes in gen:
|
|
|
|
assert quotes
|
|
|
|
for key in quotes:
|
|
|
|
assert key in us_symbols
|
|
|
|
break
|
|
|
|
# terminate far-end async-gen
|
|
|
|
# await gen.asend(None)
|
|
|
|
# break
|
|
|
|
|
|
|
|
# stop all spawned subactors
|
|
|
|
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)
|
2018-07-05 19:33:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_rx_price_quotes_from_brokerd(us_symbols):
|
|
|
|
tractor.run(
|
|
|
|
rx_price_quotes_from_brokerd,
|
|
|
|
us_symbols,
|
|
|
|
name='arbiter',
|
|
|
|
)
|