Add redis "barrier" in cleanup_keys
To prevent pytest-xdist workers stomping each other with _delete_test_keys
This commit is contained in:
parent
878490abb7
commit
675722b221
1 changed files with 13 additions and 6 deletions
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue