Skip to content

Commit 675722b

Browse files
committed
Add redis "barrier" in cleanup_keys
To prevent pytest-xdist workers stomping each other with _delete_test_keys
1 parent 878490a commit 675722b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

tests/conftest.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ def key_prefix(request, redis):
3737

3838
@pytest.fixture(scope="session", autouse=True)
3939
def cleanup_keys(request):
40-
def cleanup_keys():
41-
# Always use the sync Redis connection with finalizer. Setting up an
42-
# async finalizer should work, but I'm not suer how yet!
43-
from redis_om.connections import get_redis_connection as get_sync_redis
40+
# Always use the sync Redis connection with finalizer. Setting up an
41+
# async finalizer should work, but I'm not suer how yet!
42+
from redis_om.connections import get_redis_connection as get_sync_redis
4443

45-
_delete_test_keys(TEST_PREFIX, get_sync_redis())
44+
# Increment for every pytest-xdist worker
45+
redis = get_sync_redis()
46+
once_key = f"{TEST_PREFIX}:cleanup_keys"
47+
redis.incr(once_key)
4648

47-
request.addfinalizer(cleanup_keys)
49+
yield
50+
51+
# Delete keys only once
52+
if redis.decr(once_key) == 0:
53+
_delete_test_keys(TEST_PREFIX, redis)
54+
redis.delete(once_key)

0 commit comments

Comments
 (0)