Add a `debug_mode: bool` fixture via `--tpdb` flag

Allows tests (including any `@tractor_test`s) to subscribe to a CLI flag
`--tpdb` (for "tractor python debugger") which the session can provide
to tests which can then proxy the value to `open_root_actor()` (via
`open_nursery()`) when booting the runtime - thus enabling our debug
mode globally to any subscribers B)

This is real handy if you have some failures but can't determine the
root issue without jumping into a `pdbp` REPL inside a (sub-)actor's
spawned-task.
modden_spawn_from_client_req
Tyler Goodlet 2024-02-20 08:53:37 -05:00
parent 3e1d033708
commit 5fe3f58ea9
1 changed files with 33 additions and 5 deletions

View File

@ -41,11 +41,14 @@ def tractor_test(fn):
*args,
loglevel=None,
reg_addr=None,
start_method=None,
start_method: str|None = None,
debug_mode: bool = False,
**kwargs
):
# __tracebackhide__ = True
# NOTE: inject ant test func declared fixture
# names by manually checking!
if 'reg_addr' in inspect.signature(fn).parameters:
# injects test suite fixture value to test as well
# as `run()`
@ -64,10 +67,14 @@ def tractor_test(fn):
# set of subprocess spawning backends
kwargs['start_method'] = start_method
if 'debug_mode' in inspect.signature(fn).parameters:
# set of subprocess spawning backends
kwargs['debug_mode'] = debug_mode
if kwargs:
# use explicit root actor start
async def _main():
async with tractor.open_root_actor(
# **kwargs,
@ -76,7 +83,7 @@ def tractor_test(fn):
start_method=start_method,
# TODO: only enable when pytest is passed --pdb
# debug_mode=True,
debug_mode=debug_mode,
):
await fn(*args, **kwargs)
@ -130,22 +137,43 @@ def examples_dir() -> pathlib.Path:
def pytest_addoption(parser):
parser.addoption(
"--ll", action="store", dest='loglevel',
"--ll",
action="store",
dest='loglevel',
default='ERROR', help="logging level to set when testing"
)
parser.addoption(
"--spawn-backend", action="store", dest='spawn_backend',
"--spawn-backend",
action="store",
dest='spawn_backend',
default='trio',
help="Processing spawning backend to use for test run",
)
parser.addoption(
"--tpdb", "--debug-mode",
action="store_true",
dest='tractor_debug_mode',
# default=False,
help=(
'Enable a flag that can be used by tests to to set the '
'`debug_mode: bool` for engaging the internal '
'multi-proc debugger sys.'
),
)
def pytest_configure(config):
backend = config.option.spawn_backend
tractor._spawn.try_set_start_method(backend)
@pytest.fixture(scope='session')
def debug_mode(request):
return request.config.option.tractor_debug_mode
@pytest.fixture(scope='session', autouse=True)
def loglevel(request):
orig = tractor.log._default_loglevel