From 675722b221ea53c2fb96cc64eace25c099391b54 Mon Sep 17 00:00:00 2001 From: Serhii Charykov Date: Wed, 27 Apr 2022 19:41:07 +0300 Subject: [PATCH] Add redis "barrier" in cleanup_keys To prevent pytest-xdist workers stomping each other with _delete_test_keys --- tests/conftest.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index c68768c..f6139d9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -37,11 +37,18 @@ def key_prefix(request, redis): @pytest.fixture(scope="session", autouse=True) def cleanup_keys(request): - def cleanup_keys(): - # Always use the sync Redis connection with finalizer. Setting up an - # async finalizer should work, but I'm not suer how yet! - from redis_om.connections import get_redis_connection as get_sync_redis + # Always use the sync Redis connection with finalizer. Setting up an + # async finalizer should work, but I'm not suer how yet! + from redis_om.connections import get_redis_connection as get_sync_redis - _delete_test_keys(TEST_PREFIX, get_sync_redis()) + # Increment for every pytest-xdist worker + redis = get_sync_redis() + once_key = f"{TEST_PREFIX}:cleanup_keys" + redis.incr(once_key) - request.addfinalizer(cleanup_keys) + yield + + # Delete keys only once + if redis.decr(once_key) == 0: + _delete_test_keys(TEST_PREFIX, redis) + redis.delete(once_key)