mirror of
https://github.com/debpalash/VoiceStudio.git
synced 2026-12-04 01:18:16 +00:00
* ci: shard Python tests, cache ROCm image layers, mount uv cache in Docker build * build(electron): ship only main-process dependencies in app.asar * docs(changelog): fragments for #4322 * ci: shard hook no longer leaks into nested pytest runs; refresh flake node-deps hash; fix moss rebound fixture
125 lines
5.5 KiB
Python
125 lines
5.5 KiB
Python
"""Test isolation shared by every suite (``tests/`` and ``backend/tests/``).
|
|
|
|
Only fixtures that both suites need live here; each suite's own conftest.py
|
|
still owns its import path and hermetic data dir.
|
|
"""
|
|
import os
|
|
import sys
|
|
import types
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_model_manager_shutdown_state(request):
|
|
"""Start every test with the model manager NOT in shutdown mode (#1269).
|
|
|
|
``model_manager._shutting_down`` is a module-global Event and the GPU pool is
|
|
a module-global executor. Any test that runs the app lifespan flips both on
|
|
the way out — ``begin_shutdown()`` plus ``_reset_gpu_pool()`` — and nothing
|
|
puts them back, because in production that state is correct: the process is
|
|
ending.
|
|
|
|
Across a test session it is not correct, and it is not a cosmetic leak. A
|
|
test that arrives with the flag set finds a shut-down executor, so its very
|
|
first ``run_in_executor`` raises "cannot schedule new futures after
|
|
shutdown" — which the preload path classifies as a benign shutdown and
|
|
swallows. The symptom is a load that silently never starts:
|
|
``test_lifespan_shutdown_mid_load_is_clean_and_clears_sentinel`` failed on
|
|
``assert started.is_set()`` for exactly this reason, while passing alone.
|
|
It is also why the post-generation offload tests failed after the dub tests
|
|
(which exit the app lifespan) in ``tests/``: the offload stands down while
|
|
the process is shutting down.
|
|
|
|
This used to live in ``backend/tests/conftest.py`` only, so ``tests/`` —
|
|
which CI runs in one process — never got it. It is here so every suite does.
|
|
|
|
Reset before AND after: before so an inherited flag cannot decide this test,
|
|
after so a test that legitimately shuts down does not hand the state on.
|
|
|
|
Cleans the module in ``sys.modules``, the ``services`` package attribute,
|
|
AND any module-typed alias the test module holds (``import
|
|
services.model_manager as mm`` at module scope). Test modules bind that
|
|
alias at COLLECTION time; ``tests/backend/**`` purges ``services.*`` from
|
|
``sys.modules`` after every test it owns, so in a combined run the alias and
|
|
the live module can be different objects. Cleaning only one of them means a
|
|
test dirties the alias and the next test reads it still dirty
|
|
(``backend/tests/test_shutdown_state_isolation.py``). A module nobody has
|
|
imported holds no state, so nothing is imported just to reset it.
|
|
"""
|
|
|
|
def _targets():
|
|
package = sys.modules.get("services")
|
|
found = {
|
|
id(m): m
|
|
for m in (
|
|
sys.modules.get("services.model_manager"),
|
|
getattr(package, "model_manager", None),
|
|
)
|
|
if isinstance(m, types.ModuleType)
|
|
}
|
|
test_module = getattr(request, "module", None)
|
|
if test_module is not None:
|
|
for val in vars(test_module).values():
|
|
if (isinstance(val, types.ModuleType)
|
|
and getattr(val, "__name__", "") == "services.model_manager"):
|
|
found[id(val)] = val
|
|
return found.values()
|
|
|
|
def _clean():
|
|
# Deliberately NOT wrapped in try/except. A reset that fails silently
|
|
# leaves the next test with stale shutdown or executor state, which is
|
|
# precisely the order-dependent failure this fixture exists to remove —
|
|
# swallowing the error would defeat the fixture while looking like it
|
|
# worked (CodeRabbit). If either of these can raise, that is a real
|
|
# problem in model_manager and it should be loud.
|
|
for mod in _targets():
|
|
mod.reset_shutdown_flag()
|
|
mod._reset_gpu_pool()
|
|
|
|
_clean()
|
|
yield
|
|
_clean()
|
|
|
|
|
|
_MAX_PARAM_ID = 64
|
|
|
|
|
|
def pytest_make_parametrize_id(config, val, argname):
|
|
"""Keep long string parameters out of the test id.
|
|
|
|
pytest copies the full test id into ``PYTEST_CURRENT_TEST``. A 200 000
|
|
character parameter therefore errors every such test on Windows, whose
|
|
environment variables are capped at 32 767 characters. Long strings get a
|
|
short, stable id instead; everything else keeps pytest's default.
|
|
"""
|
|
if isinstance(val, (str, bytes)) and len(val) > _MAX_PARAM_ID:
|
|
return f"{argname}-len{len(val)}"
|
|
return None
|
|
|
|
|
|
def pytest_collection_modifyitems(config, items):
|
|
"""CI sharding: VOICESTUDIO_TEST_SHARD="i/N" keeps every N-th test module.
|
|
|
|
Whole modules (never single tests) are dealt round-robin over the sorted
|
|
module paths, so module-level state stays together and the N shards are a
|
|
disjoint partition of the full collection. Unset = run everything.
|
|
"""
|
|
spec = os.environ.get("VOICESTUDIO_TEST_SHARD", "").strip()
|
|
if not spec:
|
|
return
|
|
# Consumed here: tests that spawn a nested pytest must see the whole suite,
|
|
# not inherit this session's shard (it would collect nothing and fail).
|
|
os.environ.pop("VOICESTUDIO_TEST_SHARD", None)
|
|
index, _, total = spec.partition("/")
|
|
index, total = int(index), int(total)
|
|
if not 1 <= index <= total:
|
|
raise pytest.UsageError(f"VOICESTUDIO_TEST_SHARD={spec!r} must be i/N with 1 <= i <= N")
|
|
modules = sorted({item.nodeid.split("::", 1)[0] for item in items})
|
|
mine = {m for pos, m in enumerate(modules) if pos % total == index - 1}
|
|
kept = [i for i in items if i.nodeid.split("::", 1)[0] in mine]
|
|
dropped = [i for i in items if i.nodeid.split("::", 1)[0] not in mine]
|
|
if dropped:
|
|
config.hook.pytest_deselected(items=dropped)
|
|
items[:] = kept
|