summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorVivanov98 <66319645+Vivanov98@users.noreply.github.com>2023-01-29 13:48:50 +0000
committerGitHub <noreply@github.com>2023-01-29 15:48:50 +0200
commit428d60940f386d3680a413aa327889308f82c5de (patch)
tree9c49ab0693e0af183e084cf8b1930521fc315c41 /tests
parent9e6a9b52e5aab021d239ca56e27f06bca871cbf0 (diff)
downloadredis-py-428d60940f386d3680a413aa327889308f82c5de.tar.gz
Fix issue 2540: Synchronise concurrent command calls to single-client mode. (#2568)
Co-authored-by: Viktor Ivanov <viktor@infogrid.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_asyncio/test_connection.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/test_asyncio/test_connection.py b/tests/test_asyncio/test_connection.py
index bf59dbe..8e4fdac 100644
--- a/tests/test_asyncio/test_connection.py
+++ b/tests/test_asyncio/test_connection.py
@@ -6,6 +6,7 @@ from unittest.mock import patch
import pytest
import redis
+from redis.asyncio import Redis
from redis.asyncio.connection import (
BaseParser,
Connection,
@@ -41,6 +42,50 @@ async def test_invalid_response(create_redis):
await r.connection.disconnect()
+@pytest.mark.onlynoncluster
+async def test_single_connection():
+ """Test that concurrent requests on a single client are synchronised."""
+ r = Redis(single_connection_client=True)
+
+ init_call_count = 0
+ command_call_count = 0
+ in_use = False
+
+ class Retry_:
+ async def call_with_retry(self, _, __):
+ # If we remove the single-client lock, this error gets raised as two
+ # coroutines will be vying for the `in_use` flag due to the two
+ # asymmetric sleep calls
+ nonlocal command_call_count
+ nonlocal in_use
+ if in_use is True:
+ raise ValueError("Commands should be executed one at a time.")
+ in_use = True
+ await asyncio.sleep(0.01)
+ command_call_count += 1
+ await asyncio.sleep(0.03)
+ in_use = False
+ return "foo"
+
+ mock_conn = mock.MagicMock()
+ mock_conn.retry = Retry_()
+
+ async def get_conn(_):
+ # Validate only one client is created in single-client mode when
+ # concurrent requests are made
+ nonlocal init_call_count
+ await asyncio.sleep(0.01)
+ init_call_count += 1
+ return mock_conn
+
+ with mock.patch.object(r.connection_pool, "get_connection", get_conn):
+ with mock.patch.object(r.connection_pool, "release"):
+ await asyncio.gather(r.set("a", "b"), r.set("c", "d"))
+
+ assert init_call_count == 1
+ assert command_call_count == 2
+
+
@skip_if_server_version_lt("4.0.0")
@pytest.mark.redismod
@pytest.mark.onlynoncluster