From 8f1d53d016321af476d869efb5ed254906a806cd Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Fri, 25 Oct 2024 22:05:02 +0200 Subject: [PATCH] Tests: Add separation of database lock indices by pytest worker (#4553) * Add separation of database lock indices by pytest worker * Fix ID derivation * Update conftest.py --- src/tests/conftest.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/tests/conftest.py b/src/tests/conftest.py index 07bd446bee..3c3556749f 100644 --- a/src/tests/conftest.py +++ b/src/tests/conftest.py @@ -84,7 +84,7 @@ def reset_locale(): @pytest.fixture def fakeredis_client(monkeypatch): worker_id = os.environ.get("PYTEST_XDIST_WORKER") - if worker_id.startswith("gw"): + if worker_id and worker_id.startswith("gw"): redis_port = 1000 + int(worker_id.replace("gw", "")) else: redis_port = 1000 @@ -119,3 +119,13 @@ def fakeredis_client(monkeypatch): redis.flushall() monkeypatch.setattr('django_redis.get_redis_connection', get_redis_connection, raising=False) yield redis + + +@pytest.fixture(autouse=True) +def set_lock_namespaces(request): + worker_id = os.environ.get("PYTEST_XDIST_WORKER") + if worker_id and worker_id.startswith("gw"): + with override_settings(DATABASE_ADVISORY_LOCK_INDEX=int(worker_id.replace("gw", ""))): + yield + else: + yield