1
+ import threading
1
2
from time import sleep
2
3
from unittest .mock import patch , Mock
3
4
4
5
import pybreaker
5
6
import pytest
6
7
8
+ from redis .backoff import NoBackoff
7
9
from redis .event import EventDispatcher , OnCommandsFailEvent
10
+ from redis .exceptions import ConnectionError
8
11
from redis .multidb .circuit import State as CBState , PBCircuitBreakerAdapter
9
12
from redis .multidb .database import SyncDatabase
10
13
from redis .multidb .client import MultiDBClient
11
14
from redis .multidb .exception import NoValidDatabaseException
12
15
from redis .multidb .failover import WeightBasedFailoverStrategy
13
16
from redis .multidb .failure_detector import FailureDetector
14
17
from redis .multidb .healthcheck import HealthCheck , EchoHealthCheck
18
+ from redis .retry import Retry
15
19
from tests .test_multidb .conftest import create_weighted_list
16
20
17
21
@@ -184,29 +188,24 @@ def test_execute_command_auto_fallback_to_highest_weight_db(
184
188
self , mock_multi_db_config , mock_db , mock_db1 , mock_db2 , mock_hc
185
189
):
186
190
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
210
209
211
210
with (
212
211
patch .object (mock_multi_db_config , "databases" , return_value = databases ),
@@ -220,14 +219,14 @@ def test_execute_command_auto_fallback_to_highest_weight_db(
220
219
mock_db1 .client .execute_command .return_value = "OK1"
221
220
mock_db2 .client .execute_command .return_value = "OK2"
222
221
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
224
223
mock_multi_db_config .failover_strategy = WeightBasedFailoverStrategy ()
225
224
226
225
client = MultiDBClient (mock_multi_db_config )
227
226
assert client .set ("key" , "value" ) == "OK1"
228
- sleep ( 0.18 )
227
+ error_event . wait ( timeout = 0.5 )
229
228
assert client .set ("key" , "value" ) == "OK2"
230
- sleep (0.5 )
229
+ sleep (0.2 )
231
230
assert client .set ("key" , "value" ) == "OK1"
232
231
233
232
@pytest .mark .parametrize (
0 commit comments