Pass a config `tmp_dir: Path` to the runtime when testing

service_subpkg
Tyler Goodlet 2023-03-09 14:33:12 -05:00
parent 5aaa7f47dc
commit 79b0db4449
3 changed files with 37 additions and 31 deletions

View File

@ -1 +0,0 @@
TEST_CONFIG_DIR_PATH = '_testing'

View File

@ -2,12 +2,10 @@ from contextlib import asynccontextmanager as acm
from functools import partial
import os
from pathlib import Path
from shutil import rmtree
import pytest
import tractor
from piker import (
# log,
config,
)
from piker.service import (
@ -71,6 +69,7 @@ def ci_env() -> bool:
@acm
async def _open_test_pikerd(
tmpconfdir: str,
reg_addr: tuple[str, int] | None = None,
loglevel: str = 'warning',
**kwargs,
@ -97,6 +96,10 @@ async def _open_test_pikerd(
maybe_open_pikerd(
registry_addr=reg_addr,
loglevel=loglevel,
tractor_runtime_overrides={
'piker_test_dir': tmpconfdir,
},
**kwargs,
) as service_manager,
):
@ -119,18 +122,40 @@ async def _open_test_pikerd(
@pytest.fixture
def open_test_pikerd(
request,
request: pytest.FixtureRequest,
tmp_path: Path,
loglevel: str,
):
tmpconfdir: Path = tmp_path / '_testing'
tmpconfdir.mkdir()
tmpconfdir_str: str = str(tmpconfdir)
# NOTE: on linux the tmp config dir is generally located at:
# /tmp/pytest-of-<username>/pytest-<run#>/test_<current_test_name>/
# the default `pytest` config ensures that only the last 4 test
# suite run's dirs will be persisted, otherwise they are removed:
# https://docs.pytest.org/en/6.2.x/tmpdir.html#the-default-base-temporary-directory
print(f'CURRENT TEST CONF DIR: {tmpconfdir}')
yield partial(
_open_test_pikerd,
# pass in a unique temp dir for this test request
# so that we can have multiple tests running (maybe in parallel)
# bwitout clobbering each other's config state.
tmpconfdir=tmpconfdir_str,
# bind in level from fixture, which is itself set by
# `--ll <value>` cli flag.
loglevel=loglevel,
)
# NOTE: the `tmp_dir` fixture will wipe any files older then 3 test
# sessions by default:
# https://docs.pytest.org/en/6.2.x/tmpdir.html#the-default-base-temporary-directory
# BUT, if we wanted to always wipe conf dir and all contained files,
# rmtree(str(tmp_path))
# TODO: teardown checks such as,
# - no leaked subprocs or shm buffers
# - all requested container service are torn down
@ -169,19 +194,3 @@ def open_test_pikerd_and_ems(
loglevel,
open_test_pikerd
)
@pytest.fixture(scope='module')
def delete_testing_dir():
'''
This fixture removes the temp directory
used for storing all config/ledger/pp data
created during testing sessions. During test runs
this file can be found in .config/piker/_testing
'''
yield
app_dir = Path(config.get_app_dir('piker')).resolve()
if app_dir.is_dir():
rmtree(str(app_dir))
assert not app_dir.is_dir()

View File

@ -17,7 +17,6 @@ from functools import partial
from piker.log import get_logger
from piker.clearing._messages import Order
from piker.pp import (
open_trade_ledger,
open_pps,
)
@ -42,18 +41,19 @@ async def _async_main(
price: int = 30000,
executions: int = 1,
size: float = 0.01,
# Assert options
assert_entries: bool = False,
assert_pps: bool = False,
assert_zeroed_pps: bool = False,
assert_msg: bool = False,
) -> None:
'''
Start piker, place a trade and assert data in
pps stream, ledger and position table.
'''
oid: str = ''
last_msg = {}
@ -136,7 +136,7 @@ def _assert(
def _run_test_and_check(fn):
'''
'''
Close position and assert empty position in pps
'''
@ -150,8 +150,7 @@ def _run_test_and_check(fn):
def test_buy(
open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
open_test_pikerd_and_ems: AsyncContextManager,
):
'''
Enter a trade and assert entries are made in pps and ledger files.
@ -177,8 +176,7 @@ def test_buy(
def test_sell(
open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
open_test_pikerd_and_ems: AsyncContextManager,
):
'''
Sell position and ensure pps are zeroed.
@ -201,13 +199,13 @@ def test_sell(
),
)
def test_multi_sell(
open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
open_test_pikerd_and_ems: AsyncContextManager,
):
'''
Make 5 market limit buy orders and
then sell 5 slots at the same price.
Make 5 market limit buy orders and
then sell 5 slots at the same price.
Finally, assert cleared positions.
'''