From 9ecbfd2a21f077c0e3e15c40a38f40aec3792e91 Mon Sep 17 00:00:00 2001 From: ZdenekSrotyr Date: Mon, 4 May 2026 12:17:45 +0200 Subject: [PATCH] test(conftest): #160 reset module-level caches between tests (xdist hardening) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- tests/conftest.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index ca3a93f..945b061 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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."""