From 5fe3f58ea9757f5453d4f0b28cf35cb0742d79f5 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 20 Feb 2024 08:53:37 -0500 Subject: [PATCH] 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. --- tests/conftest.py | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 8e9a67c..c9159f0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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