1+ import threading
12from time import sleep
23from unittest .mock import patch , Mock
34
45import pybreaker
56import pytest
67
8+ from redis .backoff import NoBackoff
79from redis .event import EventDispatcher , OnCommandsFailEvent
10+ from redis .exceptions import ConnectionError
811from redis .multidb .circuit import State as CBState , PBCircuitBreakerAdapter
912from redis .multidb .database import SyncDatabase
1013from redis .multidb .client import MultiDBClient
1114from redis .multidb .exception import NoValidDatabaseException
1215from redis .multidb .failover import WeightBasedFailoverStrategy
1316from redis .multidb .failure_detector import FailureDetector
1417from redis .multidb .healthcheck import HealthCheck , EchoHealthCheck
18+ from redis .retry import Retry
1519from tests .test_multidb .conftest import create_weighted_list
1620
1721
@@ -184,29 +188,24 @@ def test_execute_command_auto_fallback_to_highest_weight_db(
184188 self , mock_multi_db_config , mock_db , mock_db1 , mock_db2 , mock_hc
185189 ):
186190 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
187- mock_hc .check_health .side_effect = [
188- True ,
189- True ,
190- True ,
191- False ,
192- True ,
193- True ,
194- True ,
195- True ,
196- True ,
197- True ,
198- True ,
199- True ,
200- True ,
201- True ,
202- True ,
203- True ,
204- True ,
205- True ,
206- True ,
207- True ,
208- True ,
209- ]
191+ db1_counter = 0
192+ error_event = threading .Event ()
193+ check = False
194+
195+ def mock_check_health (database ):
196+ nonlocal db1_counter , check
197+
198+ if database == mock_db1 and not check :
199+ db1_counter += 1
200+
201+ if db1_counter > 1 :
202+ error_event .set ()
203+ check = True
204+ return False
205+
206+ return True
207+
208+ mock_hc .check_health .side_effect = mock_check_health
210209
211210 with (
212211 patch .object (mock_multi_db_config , "databases" , return_value = databases ),
@@ -220,14 +219,14 @@ def test_execute_command_auto_fallback_to_highest_weight_db(
220219 mock_db1 .client .execute_command .return_value = "OK1"
221220 mock_db2 .client .execute_command .return_value = "OK2"
222221 mock_multi_db_config .health_check_interval = 0.1
223- mock_multi_db_config .auto_fallback_interval = 0.5
222+ mock_multi_db_config .auto_fallback_interval = 0.2
224223 mock_multi_db_config .failover_strategy = WeightBasedFailoverStrategy ()
225224
226225 client = MultiDBClient (mock_multi_db_config )
227226 assert client .set ("key" , "value" ) == "OK1"
228- sleep ( 0.18 )
227+ error_event . wait ( timeout = 0.5 )
229228 assert client .set ("key" , "value" ) == "OK2"
230- sleep (0.5 )
229+ sleep (0.2 )
231230 assert client .set ("key" , "value" ) == "OK1"
232231
233232 @pytest .mark .parametrize (
0 commit comments