Skip to content

Commit f8ee18e

Browse files
committed
Adding integration tests with the different load balancing strategies for read operation
1 parent e8f5da5 commit f8ee18e

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

tests/test_asyncio/test_cluster.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
ResponseError,
3636
)
3737
from redis.utils import str_if_bytes
38+
from tests.test_asyncio.conftest import (
39+
create_redis,
40+
)
3841
from tests.conftest import (
3942
assert_resp_response,
4043
is_resp2_connection,
@@ -706,7 +709,7 @@ def cmd_init_mock(self, r: ClusterNode) -> None:
706709
(False, LoadBalancingStrategy.RANDOM_REPLICA, [7002, 7002, 7002]),
707710
],
708711
)
709-
async def test_reading_from_replicas_in_round_robin(
712+
async def test_reading_with_load_balancing_strategies(
710713
self,
711714
read_from_replicas: bool,
712715
load_balancing_strategy: LoadBalancingStrategy,
@@ -1008,6 +1011,31 @@ async def test_get_and_set(self, r: RedisCluster) -> None:
10081011
assert await r.get("integer") == str(integer).encode()
10091012
assert (await r.get("unicode_string")).decode("utf-8") == unicode_string
10101013

1014+
@pytest.mark.parametrize(
1015+
"load_balancing_strategy",
1016+
[
1017+
LoadBalancingStrategy.ROUND_ROBIN,
1018+
LoadBalancingStrategy.ROUND_ROBIN_REPLICAS,
1019+
LoadBalancingStrategy.RANDOM_REPLICA,
1020+
],
1021+
)
1022+
async def test_get_and_set_with_load_balanced_client(
1023+
self, create_redis, load_balancing_strategy: LoadBalancingStrategy
1024+
) -> None:
1025+
r = await create_redis(cls=RedisCluster, load_balancing_strategy=load_balancing_strategy)
1026+
1027+
# get and set can't be tested independently of each other
1028+
assert await r.get("a") is None
1029+
1030+
byte_string = b"value"
1031+
assert await r.set("byte_string", byte_string)
1032+
1033+
# run the get command for the same key several times
1034+
# to iterate over the read nodes
1035+
assert await r.get("byte_string") == byte_string
1036+
assert await r.get("byte_string") == byte_string
1037+
assert await r.get("byte_string") == byte_string
1038+
10111039
async def test_mget_nonatomic(self, r: RedisCluster) -> None:
10121040
assert await r.mget_nonatomic([]) == []
10131041
assert await r.mget_nonatomic(["a", "b"]) == [None, None]

tests/test_cluster.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def cmd_init_mock(self, r):
615615
(False, LoadBalancingStrategy.RANDOM_REPLICA, [7002, 7002, 7002]),
616616
],
617617
)
618-
def test_reading_from_replicas_in_round_robin(
618+
def test_reading_with_load_balancing_strategies(
619619
self,
620620
read_from_replicas: bool,
621621
load_balancing_strategy: LoadBalancingStrategy,
@@ -1020,6 +1020,35 @@ def test_get_and_set(self, r):
10201020
assert r.get("integer") == str(integer).encode()
10211021
assert r.get("unicode_string").decode("utf-8") == unicode_string
10221022

1023+
@pytest.mark.parametrize(
1024+
"load_balancing_strategy",
1025+
[
1026+
LoadBalancingStrategy.ROUND_ROBIN,
1027+
LoadBalancingStrategy.ROUND_ROBIN_REPLICAS,
1028+
LoadBalancingStrategy.RANDOM_REPLICA,
1029+
],
1030+
)
1031+
def test_get_and_set_with_load_balanced_client(
1032+
self, request, load_balancing_strategy: LoadBalancingStrategy
1033+
) -> None:
1034+
r = _get_client(
1035+
cls=RedisCluster,
1036+
request=request,
1037+
load_balancing_strategy=load_balancing_strategy
1038+
)
1039+
1040+
# get and set can't be tested independently of each other
1041+
assert r.get("a") is None
1042+
1043+
byte_string = b"value"
1044+
assert r.set("byte_string", byte_string)
1045+
1046+
# run the get command for the same key several times
1047+
# to iterate over the read nodes
1048+
assert r.get("byte_string") == byte_string
1049+
assert r.get("byte_string") == byte_string
1050+
assert r.get("byte_string") == byte_string
1051+
10231052
def test_mget_nonatomic(self, r):
10241053
assert r.mget_nonatomic([]) == []
10251054
assert r.mget_nonatomic(["a", "b"]) == [None, None]

0 commit comments

Comments
 (0)