@@ -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