Skip to content

Commit 0d81b94

Browse files
committed
add test cases for new strategy
1 parent 99a3ae3 commit 0d81b94

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

tests/test_asyncio/test_cluster.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,11 @@ def cmd_init_mock(self, r: ClusterNode) -> None:
770770
(True, None, [7001, 7002, 7001]),
771771
(True, LoadBalancingStrategy.ROUND_ROBIN, [7001, 7002, 7001]),
772772
(True, LoadBalancingStrategy.ROUND_ROBIN_REPLICAS, [7002, 7002, 7002]),
773+
(True, LoadBalancingStrategy.RANDOM, [7002, 7001, 7002]),
773774
(True, LoadBalancingStrategy.RANDOM_REPLICA, [7002, 7002, 7002]),
774775
(False, LoadBalancingStrategy.ROUND_ROBIN, [7001, 7002, 7001]),
775776
(False, LoadBalancingStrategy.ROUND_ROBIN_REPLICAS, [7002, 7002, 7002]),
777+
(False, LoadBalancingStrategy.RANDOM, [7002, 7001, 7002]),
776778
(False, LoadBalancingStrategy.RANDOM_REPLICA, [7002, 7002, 7002]),
777779
],
778780
)
@@ -782,6 +784,25 @@ async def test_reading_with_load_balancing_strategies(
782784
load_balancing_strategy: LoadBalancingStrategy,
783785
mocks_srv_ports: List[int],
784786
) -> None:
787+
def _make_mock_randint():
788+
_state = 1 # Start with 1 so we have clearly different results from round robin
789+
790+
def _mock_randint(lower: int, upper: int) -> int:
791+
"""
792+
Return a controlled sequence of numbers when called repeatedly
793+
"""
794+
if lower == upper:
795+
return lower
796+
797+
nonlocal _state
798+
res = _state + lower
799+
_state ^= 1
800+
return res
801+
802+
return _mock_randint
803+
804+
mock_randint = _make_mock_randint()
805+
785806
with mock.patch.multiple(
786807
Connection,
787808
send_command=mock.DEFAULT,
@@ -792,7 +813,9 @@ async def test_reading_with_load_balancing_strategies(
792813
) as mocks:
793814
with mock.patch.object(
794815
ClusterNode, "execute_command", autospec=True
795-
) as execute_command:
816+
) as execute_command, patch(
817+
"random.randint", wraps=mock_randint
818+
):
796819

797820
async def execute_command_mock_first(self, *args, **options):
798821
await self.connection_class(**self.connection_kwargs).connect()
@@ -1092,6 +1115,7 @@ async def test_get_and_set(self, r: RedisCluster) -> None:
10921115
[
10931116
LoadBalancingStrategy.ROUND_ROBIN,
10941117
LoadBalancingStrategy.ROUND_ROBIN_REPLICAS,
1118+
LoadBalancingStrategy.RANDOM,
10951119
LoadBalancingStrategy.RANDOM_REPLICA,
10961120
],
10971121
)

tests/test_cluster.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,11 @@ def cmd_init_mock(self, r):
660660
(True, None, [7001, 7002, 7001]),
661661
(True, LoadBalancingStrategy.ROUND_ROBIN, [7001, 7002, 7001]),
662662
(True, LoadBalancingStrategy.ROUND_ROBIN_REPLICAS, [7002, 7002, 7002]),
663+
(True, LoadBalancingStrategy.RANDOM, [7002, 7001, 7002]),
663664
(True, LoadBalancingStrategy.RANDOM_REPLICA, [7002, 7002, 7002]),
664665
(False, LoadBalancingStrategy.ROUND_ROBIN, [7001, 7002, 7001]),
665666
(False, LoadBalancingStrategy.ROUND_ROBIN_REPLICAS, [7002, 7002, 7002]),
667+
(False, LoadBalancingStrategy.RANDOM, [7002, 7001, 7002]),
666668
(False, LoadBalancingStrategy.RANDOM_REPLICA, [7002, 7002, 7002]),
667669
],
668670
)
@@ -672,6 +674,25 @@ def test_reading_with_load_balancing_strategies(
672674
load_balancing_strategy: LoadBalancingStrategy,
673675
mocks_srv_ports: List[int],
674676
):
677+
def _make_mock_randint():
678+
_state = 1 # Start with 1 so we have clearly different results from round robin
679+
680+
def _mock_randint(lower: int, upper: int) -> int:
681+
"""
682+
Return a controlled sequence of numbers when called repeatedly
683+
"""
684+
if lower == upper:
685+
return lower
686+
687+
nonlocal _state
688+
res = _state + lower
689+
_state ^= 1
690+
return res
691+
692+
return _mock_randint
693+
694+
mock_randint = _make_mock_randint()
695+
675696
with patch.multiple(
676697
Connection,
677698
send_command=DEFAULT,
@@ -680,7 +701,9 @@ def test_reading_with_load_balancing_strategies(
680701
can_read=DEFAULT,
681702
on_connect=DEFAULT,
682703
) as mocks:
683-
with patch.object(Redis, "parse_response") as parse_response:
704+
with patch.object(Redis, "parse_response") as parse_response, patch(
705+
"random.randint", wraps=mock_randint
706+
):
684707

685708
def parse_response_mock_first(connection, *args, **options):
686709
# Primary
@@ -732,8 +755,9 @@ def parse_response_mock_third(connection, *args, **options):
732755
if (
733756
load_balancing_strategy is None
734757
or load_balancing_strategy == LoadBalancingStrategy.ROUND_ROBIN
758+
or load_balancing_strategy == LoadBalancingStrategy.RANDOM
735759
):
736-
# in the round robin strategy the primary node can also receive read
760+
# in the round robin and random strategies, the primary node can also receive read
737761
# requests and this means that there will be second node connected
738762
expected_calls_list.append(call("READONLY"))
739763

@@ -1092,6 +1116,7 @@ def test_get_and_set(self, r):
10921116
[
10931117
LoadBalancingStrategy.ROUND_ROBIN,
10941118
LoadBalancingStrategy.ROUND_ROBIN_REPLICAS,
1119+
LoadBalancingStrategy.RANDOM,
10951120
LoadBalancingStrategy.RANDOM_REPLICA,
10961121
],
10971122
)

0 commit comments

Comments
 (0)