From 675722b221ea53c2fb96cc64eace25c099391b54 Mon Sep 17 00:00:00 2001 From: Serhii Charykov Date: Wed, 27 Apr 2022 19:41:07 +0300 Subject: [PATCH 1/5] 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) From 5016f739ddd427e077cf94fdc1fbdb410fd0019d Mon Sep 17 00:00:00 2001 From: Serhii Charykov Date: Wed, 27 Apr 2022 19:42:18 +0300 Subject: [PATCH 2/5] Fix ordering in test_full_text_search_queries --- tests/test_hash_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index a35409d..f2ac0ef 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -137,7 +137,7 @@ async def test_full_text_search_queries(members, m): assert actual == [member1] - actual = await (m.Member.find(~(m.Member.bio % "anxious")).all()) + actual = await (m.Member.find(~(m.Member.bio % "anxious")).sort_by('age').all()) assert actual == [member1, member3] @@ -433,7 +433,7 @@ async def test_all_pks(m): bio="This is a test user to be deleted.", ) - await member1.save() + await member1.save() pk_list = [] async for pk in await m.Member.all_pks(): From 56f1d35de97846036e79a5289dd2f9939efd70f4 Mon Sep 17 00:00:00 2001 From: Serhii Charykov Date: Wed, 27 Apr 2022 19:47:54 +0300 Subject: [PATCH 3/5] lint & format --- tests/test_hash_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index f2ac0ef..b7c9b3f 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -137,7 +137,7 @@ async def test_full_text_search_queries(members, m): assert actual == [member1] - actual = await (m.Member.find(~(m.Member.bio % "anxious")).sort_by('age').all()) + actual = await (m.Member.find(~(m.Member.bio % "anxious")).sort_by("age").all()) assert actual == [member1, member3] From 0ae8ef882e8c04f544c2b069573d9ac1a93d36b2 Mon Sep 17 00:00:00 2001 From: Serhii Charykov Date: Wed, 27 Apr 2022 20:01:17 +0300 Subject: [PATCH 4/5] Refactoring --- tests/conftest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index f6139d9..d14f0a5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,13 +42,13 @@ def cleanup_keys(request): from redis_om.connections import get_redis_connection as get_sync_redis # Increment for every pytest-xdist worker - redis = get_sync_redis() + conn = get_sync_redis() once_key = f"{TEST_PREFIX}:cleanup_keys" - redis.incr(once_key) + conn.incr(once_key) yield # Delete keys only once - if redis.decr(once_key) == 0: - _delete_test_keys(TEST_PREFIX, redis) - redis.delete(once_key) + if conn.decr(once_key) == 0: + _delete_test_keys(TEST_PREFIX, conn) + conn.delete(once_key) From 838a712d57ac4bd9f83f68a469df008c8b5d4485 Mon Sep 17 00:00:00 2001 From: Serhii Charykov Date: Wed, 27 Apr 2022 20:11:44 +0300 Subject: [PATCH 5/5] Remove deletion logic duplication --- tests/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index d14f0a5..bd530bb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -51,4 +51,3 @@ def cleanup_keys(request): # Delete keys only once if conn.decr(once_key) == 0: _delete_test_keys(TEST_PREFIX, conn) - conn.delete(once_key)