2023-01-10 18:16:45 +00:00
|
|
|
'''
|
|
|
|
Actor tree daemon sub-service verifications
|
|
|
|
|
|
|
|
'''
|
|
|
|
from typing import AsyncContextManager
|
2023-01-24 20:14:09 +00:00
|
|
|
from contextlib import asynccontextmanager as acm
|
2023-01-10 18:16:45 +00:00
|
|
|
|
2023-01-24 20:14:09 +00:00
|
|
|
import pytest
|
2023-01-10 18:16:45 +00:00
|
|
|
import trio
|
|
|
|
import tractor
|
|
|
|
|
2023-03-08 20:14:39 +00:00
|
|
|
from piker.service import (
|
2023-01-24 20:14:09 +00:00
|
|
|
find_service,
|
|
|
|
Services,
|
|
|
|
)
|
|
|
|
from piker.data import (
|
|
|
|
open_feed,
|
|
|
|
)
|
|
|
|
from piker.clearing import (
|
|
|
|
open_ems,
|
|
|
|
)
|
|
|
|
from piker.clearing._messages import (
|
|
|
|
BrokerdPosition,
|
|
|
|
Status,
|
|
|
|
)
|
|
|
|
from piker.clearing._client import (
|
|
|
|
OrderBook,
|
|
|
|
)
|
|
|
|
|
2023-01-10 18:16:45 +00:00
|
|
|
|
|
|
|
def test_runtime_boot(
|
|
|
|
open_test_pikerd: AsyncContextManager
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
Verify we can boot the `pikerd` service stack using the
|
|
|
|
`open_test_pikerd` fixture helper and that registry address details
|
|
|
|
match up.
|
|
|
|
|
|
|
|
'''
|
|
|
|
async def main():
|
|
|
|
port = 6666
|
|
|
|
daemon_addr = ('127.0.0.1', port)
|
2023-01-24 20:14:09 +00:00
|
|
|
services: Services
|
2023-01-10 18:16:45 +00:00
|
|
|
|
|
|
|
async with (
|
|
|
|
open_test_pikerd(
|
|
|
|
reg_addr=daemon_addr,
|
2023-01-24 20:14:09 +00:00
|
|
|
) as (_, _, pikerd_portal, services),
|
2023-01-10 18:16:45 +00:00
|
|
|
|
|
|
|
tractor.wait_for_actor(
|
|
|
|
'pikerd',
|
|
|
|
arbiter_sockaddr=daemon_addr,
|
|
|
|
) as portal,
|
|
|
|
):
|
|
|
|
assert pikerd_portal.channel.raddr == daemon_addr
|
|
|
|
assert pikerd_portal.channel.raddr == portal.channel.raddr
|
|
|
|
|
|
|
|
trio.run(main)
|
2023-01-24 20:14:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@acm
|
|
|
|
async def ensure_service(
|
|
|
|
name: str,
|
|
|
|
sockaddr: tuple[str, int] | None = None,
|
|
|
|
) -> None:
|
|
|
|
async with find_service(name) as portal:
|
|
|
|
remote_sockaddr = portal.channel.raddr
|
|
|
|
print(f'FOUND `{name}` @ {remote_sockaddr}')
|
|
|
|
|
|
|
|
if sockaddr:
|
|
|
|
assert remote_sockaddr == sockaddr
|
|
|
|
|
|
|
|
yield portal
|
|
|
|
|
|
|
|
|
|
|
|
def test_ensure_datafeed_actors(
|
2023-02-21 17:02:26 +00:00
|
|
|
open_test_pikerd: AsyncContextManager,
|
|
|
|
loglevel: str,
|
2023-01-24 20:14:09 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
'''
|
|
|
|
Verify that booting a data feed starts a `brokerd`
|
|
|
|
actor and a singleton global `samplerd` and opening
|
|
|
|
an order mode in paper opens the `paperboi` service.
|
|
|
|
|
|
|
|
'''
|
|
|
|
actor_name: str = 'brokerd'
|
|
|
|
backend: str = 'kraken'
|
|
|
|
brokerd_name: str = f'{actor_name}.{backend}'
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with (
|
|
|
|
open_test_pikerd(),
|
|
|
|
open_feed(
|
|
|
|
['xbtusdt.kraken'],
|
2023-02-21 17:02:26 +00:00
|
|
|
loglevel=loglevel,
|
2023-01-24 20:14:09 +00:00
|
|
|
) as feed
|
|
|
|
):
|
|
|
|
# halt rt quote streams since we aren't testing them
|
|
|
|
await feed.pause()
|
|
|
|
|
|
|
|
async with (
|
|
|
|
ensure_service(brokerd_name),
|
|
|
|
ensure_service('samplerd'),
|
|
|
|
):
|
|
|
|
pass
|
|
|
|
|
|
|
|
trio.run(main)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ensure_ems_in_paper_actors(
|
2023-02-21 17:02:26 +00:00
|
|
|
open_test_pikerd: AsyncContextManager,
|
|
|
|
loglevel: str,
|
2023-01-24 20:14:09 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
actor_name: str = 'brokerd'
|
|
|
|
backend: str = 'kraken'
|
|
|
|
brokerd_name: str = f'{actor_name}.{backend}'
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
|
|
|
# type declares
|
|
|
|
book: OrderBook
|
|
|
|
trades_stream: tractor.MsgStream
|
|
|
|
pps: dict[str, list[BrokerdPosition]]
|
|
|
|
accounts: list[str]
|
|
|
|
dialogs: dict[str, Status]
|
|
|
|
|
|
|
|
# ensure we timeout after is startup is too slow.
|
|
|
|
# TODO: something like this should be our start point for
|
|
|
|
# benchmarking end-to-end startup B)
|
2023-01-26 22:59:11 +00:00
|
|
|
with trio.fail_after(9):
|
2023-01-24 20:14:09 +00:00
|
|
|
async with (
|
|
|
|
open_test_pikerd() as (_, _, _, services),
|
|
|
|
|
|
|
|
open_ems(
|
|
|
|
'xbtusdt.kraken',
|
|
|
|
mode='paper',
|
2023-02-21 17:02:26 +00:00
|
|
|
loglevel=loglevel,
|
2023-01-24 20:14:09 +00:00
|
|
|
) as (
|
|
|
|
book,
|
|
|
|
trades_stream,
|
|
|
|
pps,
|
|
|
|
accounts,
|
|
|
|
dialogs,
|
|
|
|
),
|
|
|
|
):
|
|
|
|
# there should be no on-going positions,
|
|
|
|
# TODO: though eventually we'll want to validate against
|
|
|
|
# local ledger and `pps.toml` state ;)
|
|
|
|
assert not pps
|
|
|
|
assert not dialogs
|
|
|
|
|
|
|
|
pikerd_subservices = ['emsd', 'samplerd']
|
|
|
|
|
|
|
|
async with (
|
|
|
|
ensure_service('emsd'),
|
|
|
|
ensure_service(brokerd_name),
|
|
|
|
ensure_service(f'paperboi.{backend}'),
|
|
|
|
):
|
|
|
|
for name in pikerd_subservices:
|
|
|
|
assert name in services.service_tasks
|
|
|
|
|
|
|
|
# brokerd.kraken actor should have been started
|
|
|
|
# implicitly by the ems.
|
|
|
|
assert brokerd_name in services.service_tasks
|
|
|
|
|
|
|
|
print('ALL SERVICES STARTED, terminating..')
|
|
|
|
await services.cancel_service('emsd')
|
|
|
|
|
|
|
|
with pytest.raises(
|
|
|
|
tractor._exceptions.ContextCancelled,
|
|
|
|
) as exc_info:
|
|
|
|
trio.run(main)
|
|
|
|
|
2023-02-15 00:46:09 +00:00
|
|
|
cancel_msg: str = '_emsd_main()` was remotely cancelled by its caller'
|
2023-02-23 20:21:10 +00:00
|
|
|
assert cancel_msg in exc_info.value.args[0]
|