diff --git a/stackvox/daemon.py b/stackvox/daemon.py index 16db445..b323176 100644 --- a/stackvox/daemon.py +++ b/stackvox/daemon.py @@ -272,7 +272,8 @@ def __init__( self.deadline: float | None = None self.worker = threading.Thread(target=self._worker, daemon=True) self.worker.start() - threading.Thread(target=self._watchdog, daemon=True, name="stackvox-watchdog").start() + self.watchdog = threading.Thread(target=self._watchdog, daemon=True, name="stackvox-watchdog") + self.watchdog.start() _start_device_watcher() def _worker(self) -> None: diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..7097a20 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,16 @@ +from __future__ import annotations + +import pytest + +from stackvox import daemon + + +@pytest.fixture(autouse=True) +def isolated_runtime_files(mocker, tmp_path): + """Point the daemon's socket and pid files into the test's temp directory. + + The defaults are the live daemon's own files, and a stray real + `_exit_wedged` or startup during a test run would delete or replace them, + leaving a running daemon that no client can reach.""" + mocker.patch.object(daemon, "SOCKET_PATH", tmp_path / "daemon.sock") + mocker.patch.object(daemon, "PID_PATH", tmp_path / "daemon.pid") diff --git a/tests/test_daemon.py b/tests/test_daemon.py index 5ee0fc0..7f45edc 100644 --- a/tests/test_daemon.py +++ b/tests/test_daemon.py @@ -337,7 +337,11 @@ def exit_wedged(mocker): def state(mocker, exit_wedged): """A _DaemonState with the engine and the audio side mocked out, and a watchdog that polls fast enough to test. The poll interval is patched before - the watchdog starts, because its first wait reads it once.""" + the watchdog starts, because its first wait reads it once. + + Teardown waits for the watchdog to stop before the mocks are undone. A + watchdog paused between spotting an overrun and calling _exit_wedged would + otherwise wake in the next test and call its mock, or the real exit.""" mocker.patch.object(daemon, "WATCHDOG_POLL_SECONDS", 0.01) mocker.patch.object(daemon, "Stackvox") mocker.patch.object(daemon, "_refresh_audio_devices") @@ -347,6 +351,7 @@ def state(mocker, exit_wedged): yield daemon_state finally: daemon_state.shutdown() + daemon_state.watchdog.join() def _wait_until(condition, timeout: float = 1.0) -> None: