Add redis "barrier" in cleanup_keys

To prevent pytest-xdist workers stomping each other with _delete_test_keys
This commit is contained in:
Serhii Charykov 2022-04-27 19:41:07 +03:00
parent 878490abb7
commit 675722b221

View file

@ -37,11 +37,18 @@ def key_prefix(request, redis):
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
def cleanup_keys(request): def cleanup_keys(request):
def cleanup_keys(): # Always use the sync Redis connection with finalizer. Setting up an
# Always use the sync Redis connection with finalizer. Setting up an # async finalizer should work, but I'm not suer how yet!
# async finalizer should work, but I'm not suer how yet! from redis_om.connections import get_redis_connection as get_sync_redis
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)