Skip to content

Commit 77f7e4f

Browse files
committed
Refactored tests
1 parent 003853b commit 77f7e4f

File tree

6 files changed

+55
-61
lines changed

6 files changed

+55
-61
lines changed

redis/multidb/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, config: MultiDbConfig):
3434

3535
self._health_check_interval = config.health_check_interval
3636
self._health_check_policy: HealthCheckPolicy = config.health_check_policy.value(
37-
config.health_check_probes, config.health_check_delay
37+
config.health_check_probes, config.health_check_probes_delay
3838
)
3939
self._failure_detectors = config.default_failure_detectors()
4040

redis/multidb/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class MultiDbConfig:
100100
health_checks: Optional list of additional health checks performed on databases.
101101
health_check_interval: Time interval for executing health checks.
102102
health_check_probes: Number of attempts to evaluate the health of a database.
103-
health_check_delay: Delay between health check attempts.
103+
health_check_probes_delay: Delay between health check attempts.
104104
health_check_policy: Policy for determining database health based on health checks.
105105
failover_strategy: Optional strategy for handling database failover scenarios.
106106
failover_attempts: Number of retries allowed for failover operations.
@@ -138,7 +138,7 @@ class MultiDbConfig:
138138
health_checks: Optional[List[HealthCheck]] = None
139139
health_check_interval: float = DEFAULT_HEALTH_CHECK_INTERVAL
140140
health_check_probes: int = DEFAULT_HEALTH_CHECK_PROBES
141-
health_check_delay: float = DEFAULT_HEALTH_CHECK_DELAY
141+
health_check_probes_delay: float = DEFAULT_HEALTH_CHECK_DELAY
142142
health_check_policy: HealthCheckPolicies = DEFAULT_HEALTH_CHECK_POLICY
143143
failover_strategy: Optional[FailoverStrategy] = None
144144
failover_attempts: int = DEFAULT_FAILOVER_ATTEMPTS

tests/test_asyncio/test_multidb/test_client.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -188,29 +188,24 @@ async def test_execute_command_auto_fallback_to_highest_weight_db(
188188
self, mock_multi_db_config, mock_db, mock_db1, mock_db2, mock_hc
189189
):
190190
databases = create_weighted_list(mock_db, mock_db1, mock_db2)
191-
mock_hc.check_health.side_effect = [
192-
True,
193-
True,
194-
True,
195-
False,
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-
True,
210-
True,
211-
True,
212-
True,
213-
]
191+
db1_counter = 0
192+
error_event = asyncio.Event()
193+
check = False
194+
195+
async 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
214209

215210
with (
216211
patch.object(mock_multi_db_config, "databases", return_value=databases),
@@ -224,15 +219,15 @@ async def test_execute_command_auto_fallback_to_highest_weight_db(
224219
mock_db1.client.execute_command.return_value = "OK1"
225220
mock_db2.client.execute_command.return_value = "OK2"
226221
mock_multi_db_config.health_check_interval = 0.1
227-
mock_multi_db_config.auto_fallback_interval = 0.5
222+
mock_multi_db_config.auto_fallback_interval = 0.2
228223
mock_multi_db_config.failover_strategy = WeightBasedFailoverStrategy()
229224

230-
client = MultiDBClient(mock_multi_db_config)
231-
assert await client.set("key", "value") == "OK1"
232-
await asyncio.sleep(0.15)
233-
assert await client.set("key", "value") == "OK2"
234-
await asyncio.sleep(0.5)
235-
assert await client.set("key", "value") == "OK1"
225+
async with MultiDBClient(mock_multi_db_config) as client:
226+
assert await client.set("key", "value") == "OK1"
227+
await error_event.wait()
228+
assert await client.set("key", "value") == "OK2"
229+
await asyncio.sleep(0.2)
230+
assert await client.set("key", "value") == "OK1"
236231

237232
@pytest.mark.asyncio
238233
@pytest.mark.parametrize(

tests/test_multidb/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def mock_multi_db_config(request, mock_fd, mock_fs, mock_hc, mock_ed) -> MultiDb
111111
databases_config=[Mock(spec=DatabaseConfig)],
112112
failure_detectors=[mock_fd],
113113
health_check_interval=hc_interval,
114-
health_check_delay=0.05,
114+
health_check_probes_delay=0.05,
115115
health_check_policy=health_check_policy,
116116
health_check_probes=health_check_probes,
117117
failover_strategy=mock_fs,

tests/test_multidb/test_client.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
import threading
12
from time import sleep
23
from unittest.mock import patch, Mock
34

45
import pybreaker
56
import pytest
67

8+
from redis.backoff import NoBackoff
79
from redis.event import EventDispatcher, OnCommandsFailEvent
10+
from redis.exceptions import ConnectionError
811
from redis.multidb.circuit import State as CBState, PBCircuitBreakerAdapter
912
from redis.multidb.database import SyncDatabase
1013
from redis.multidb.client import MultiDBClient
1114
from redis.multidb.exception import NoValidDatabaseException
1215
from redis.multidb.failover import WeightBasedFailoverStrategy
1316
from redis.multidb.failure_detector import FailureDetector
1417
from redis.multidb.healthcheck import HealthCheck, EchoHealthCheck
18+
from redis.retry import Retry
1519
from 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(

tests/test_scenario/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def r_multi_db(
140140
health_check_probes=3,
141141
health_check_interval=health_check_interval,
142142
event_dispatcher=event_dispatcher,
143-
health_check_delay=health_check_delay,
143+
health_check_probes_delay=health_check_delay,
144144
)
145145

146146
return MultiDBClient(config), listener, endpoint_config

0 commit comments

Comments
 (0)