test(conftest): #160 reset module-level caches between tests (xdist hardening)

CI failures on PR #168 after rebasing onto main + PR #169/#170:
gw2 worker bucket reproducibly fails test_admin_can_list_registry +
test_three_sources_catalog_count with `assert "X" in set()` — the
register-table POST landed but list/catalog endpoints returned empty.

Root cause: pre-existing module-level cache leak across tests on the
same xdist worker process. `app.instance_config._instance_config`,
`connectors.bigquery.access.get_bq_access` (functools.cache), and
`app.api.v2_quota._quota_singleton` all survive across function-scoped
fixtures, so a prior test that read instance.yaml against an old
DATA_DIR poisons the next test's env even after `monkeypatch.setenv`
resets DATA_DIR.

Pre-existing on main — surfaced now because #160's new tests changed
the xdist test bucket distribution and dropped a different mix of
tests onto gw2 that hit the leak. Direct cause is unchanged; my T1a
fix in test_main_exits_when_project_missing addressed one symptom of
the same pollution but didn't generalize.

Add an autouse fixture in conftest.py that resets all three caches
before every test. Generic fix; helps any future test that reads
instance.yaml or BqAccess and would otherwise be order-dependent on
the worker.
This commit is contained in:
ZdenekSrotyr 2026-05-04 12:17:45 +02:00
parent b2d54126dc
commit 9ecbfd2a21

View file

@ -47,6 +47,42 @@ def _disable_auth_rate_limit_in_tests():
limiter.enabled = was_enabled
@pytest.fixture(autouse=True)
def _reset_module_caches():
"""Reset module-level caches that survive across tests on the same
pytest-xdist worker process. Without this, a test that populates
`app.instance_config._instance_config` (e.g. via `runpy.run_module`
in test_bigquery_extractor's __main__ tests, or via any path that
calls `app.instance_config.get_value`) leaves stale config visible
to the next test on that worker including config that points at
a different DATA_DIR than the next test's e2e_env set.
Caches reset:
- app.instance_config._instance_config instance.yaml deep-merge cache
- get_bq_access (functools.cache) BqAccess(BqProjects(...)) lru
- app.api.v2_quota._quota_singleton per-user quota tracker
Pre-existing flakiness; surfaced by issue #160 PR #168 shifting the
test bucket distribution on xdist worker gw2.
"""
try:
import app.instance_config as _ic
_ic._instance_config = None
try:
from connectors.bigquery.access import get_bq_access
get_bq_access.cache_clear()
except (ImportError, AttributeError):
pass
except ImportError:
pass
try:
import app.api.v2_quota as _q
_q._quota_singleton = None
except ImportError:
pass
yield
@pytest.fixture
def e2e_env(tmp_path, monkeypatch):
"""Set up complete E2E environment with DATA_DIR, create dirs."""