@@ -36,9 +36,7 @@ async def test_execute_command_against_correct_db_on_successful_initialization(
3636 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
3737 mock_multi_db_config .health_checks = [mock_hc ]
3838
39- with (
40- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
41- ):
39+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
4240 mock_db1 .client .execute_command = AsyncMock (return_value = "OK1" )
4341
4442 mock_hc .check_health .return_value = True
@@ -71,9 +69,7 @@ async def test_execute_command_against_correct_db_and_closed_circuit(
7169 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
7270 mock_multi_db_config .health_checks = [mock_hc ]
7371
74- with (
75- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
76- ):
72+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
7773 mock_db1 .client .execute_command = AsyncMock (return_value = "OK1" )
7874
7975 mock_hc .check_health .side_effect = [
@@ -88,7 +84,8 @@ async def test_execute_command_against_correct_db_and_closed_circuit(
8884
8985 client = MultiDBClient (mock_multi_db_config )
9086 assert mock_multi_db_config .failover_strategy .set_databases .call_count == 1
91- assert await client .set ("key" , "value" ) == "OK1"
87+ result = await client .set ("key" , "value" )
88+ assert result == "OK1"
9289 assert mock_hc .check_health .call_count == 7
9390
9491 assert mock_db .circuit .state == CBState .CLOSED
@@ -185,9 +182,7 @@ async def mock_check_health(database):
185182 mock_hc .check_health .side_effect = mock_check_health
186183 mock_multi_db_config .health_checks = [mock_hc ]
187184
188- with (
189- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
190- ):
185+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
191186 mock_db .client .execute_command .return_value = "OK"
192187 mock_db1 .client .execute_command .return_value = "OK1"
193188 mock_db2 .client .execute_command .return_value = "OK2"
@@ -201,6 +196,7 @@ async def mock_check_health(database):
201196 assert await db1_became_unhealthy .wait (), (
202197 "Timeout waiting for mock_db1 to become unhealthy"
203198 )
199+
204200 await asyncio .sleep (0.01 )
205201
206202 assert await client .set ("key" , "value" ) == "OK2"
@@ -209,7 +205,14 @@ async def mock_check_health(database):
209205 assert await db2_became_unhealthy .wait (), (
210206 "Timeout waiting for mock_db2 to become unhealthy"
211207 )
212- await asyncio .sleep (0.01 )
208+
209+ # Wait for circuit breaker state to actually reflect the unhealthy status
210+ # (instead of just sleeping)
211+ max_retries = 20
212+ for _ in range (max_retries ):
213+ if cb2 .state == CBState .OPEN : # Circuit is open (unhealthy)
214+ break
215+ await asyncio .sleep (0.01 )
213216
214217 assert await client .set ("key" , "value" ) == "OK"
215218
@@ -258,9 +261,7 @@ async def mock_check_health(database):
258261 mock_hc .check_health .side_effect = mock_check_health
259262 mock_multi_db_config .health_checks = [mock_hc ]
260263
261- with (
262- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
263- ):
264+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
264265 mock_db .client .execute_command .return_value = "OK"
265266 mock_db1 .client .execute_command .return_value = "OK1"
266267 mock_db2 .client .execute_command .return_value = "OK2"
@@ -271,6 +272,14 @@ async def mock_check_health(database):
271272 async with MultiDBClient (mock_multi_db_config ) as client :
272273 assert await client .set ("key" , "value" ) == "OK1"
273274 await error_event .wait ()
275+ # Wait for circuit breaker to actually open (not just the event)
276+ max_retries = 20
277+ for _ in range (max_retries ):
278+ if mock_db1 .circuit .state == CBState .OPEN : # Circuit is open
279+ break
280+ await asyncio .sleep (0.01 )
281+
282+ # Now the failover strategy will select mock_db2
274283 assert await client .set ("key" , "value" ) == "OK2"
275284 await asyncio .sleep (0.5 )
276285 assert await client .set ("key" , "value" ) == "OK1"
@@ -312,9 +321,7 @@ async def mock_check_health(database):
312321 mock_hc .check_health .side_effect = mock_check_health
313322 mock_multi_db_config .health_checks = [mock_hc ]
314323
315- with (
316- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
317- ):
324+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
318325 mock_db .client .execute_command .return_value = "OK"
319326 mock_db1 .client .execute_command .return_value = "OK1"
320327 mock_db2 .client .execute_command .return_value = "OK2"
@@ -325,6 +332,15 @@ async def mock_check_health(database):
325332 async with MultiDBClient (mock_multi_db_config ) as client :
326333 assert await client .set ("key" , "value" ) == "OK1"
327334 await error_event .wait ()
335+ # Wait for circuit breaker state to actually reflect the unhealthy status
336+ # (instead of just sleeping)
337+ max_retries = 20
338+ for _ in range (max_retries ):
339+ if (
340+ mock_db1 .circuit .state == CBState .OPEN
341+ ): # Circuit is open (unhealthy)
342+ break
343+ await asyncio .sleep (0.01 )
328344 assert await client .set ("key" , "value" ) == "OK2"
329345 await asyncio .sleep (0.5 )
330346 assert await client .set ("key" , "value" ) == "OK2"
@@ -348,9 +364,7 @@ async def test_execute_command_throws_exception_on_failed_initialization(
348364 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
349365 mock_multi_db_config .health_checks = [mock_hc ]
350366
351- with (
352- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
353- ):
367+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
354368 mock_hc .check_health .return_value = False
355369
356370 client = MultiDBClient (mock_multi_db_config )
@@ -382,9 +396,7 @@ async def test_add_database_throws_exception_on_same_database(
382396 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
383397 mock_multi_db_config .health_checks = [mock_hc ]
384398
385- with (
386- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
387- ):
399+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
388400 mock_hc .check_health .return_value = False
389401
390402 client = MultiDBClient (mock_multi_db_config )
@@ -413,9 +425,7 @@ async def test_add_database_makes_new_database_active(
413425 databases = create_weighted_list (mock_db , mock_db2 )
414426 mock_multi_db_config .health_checks = [mock_hc ]
415427
416- with (
417- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
418- ):
428+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
419429 mock_db1 .client .execute_command .return_value = "OK1"
420430 mock_db2 .client .execute_command .return_value = "OK2"
421431
@@ -451,9 +461,7 @@ async def test_remove_highest_weighted_database(
451461 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
452462 mock_multi_db_config .health_checks = [mock_hc ]
453463
454- with (
455- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
456- ):
464+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
457465 mock_db1 .client .execute_command .return_value = "OK1"
458466 mock_db2 .client .execute_command .return_value = "OK2"
459467
@@ -487,9 +495,7 @@ async def test_update_database_weight_to_be_highest(
487495 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
488496 mock_multi_db_config .health_checks = [mock_hc ]
489497
490- with (
491- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
492- ):
498+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
493499 mock_db1 .client .execute_command .return_value = "OK1"
494500 mock_db2 .client .execute_command .return_value = "OK2"
495501
@@ -525,9 +531,7 @@ async def test_add_new_failure_detector(
525531 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
526532 mock_multi_db_config .health_checks = [mock_hc ]
527533
528- with (
529- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
530- ):
534+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
531535 mock_db1 .client .execute_command .return_value = "OK1"
532536 mock_multi_db_config .event_dispatcher = EventDispatcher ()
533537 mock_fd = mock_multi_db_config .failure_detectors [0 ]
@@ -546,7 +550,7 @@ async def test_add_new_failure_detector(
546550 assert mock_hc .check_health .call_count == 9
547551
548552 # Simulate failing command events that lead to a failure detection
549- for i in range (5 ):
553+ for _ in range (5 ):
550554 await mock_multi_db_config .event_dispatcher .dispatch_async (
551555 command_fail_event
552556 )
@@ -557,7 +561,7 @@ async def test_add_new_failure_detector(
557561 client .add_failure_detector (another_fd )
558562
559563 # Simulate failing command events that lead to a failure detection
560- for i in range (5 ):
564+ for _ in range (5 ):
561565 await mock_multi_db_config .event_dispatcher .dispatch_async (
562566 command_fail_event
563567 )
@@ -584,9 +588,7 @@ async def test_add_new_health_check(
584588 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
585589 mock_multi_db_config .health_checks = [mock_hc ]
586590
587- with (
588- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
589- ):
591+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
590592 mock_db1 .client .execute_command .return_value = "OK1"
591593
592594 mock_hc .check_health .return_value = True
@@ -624,9 +626,7 @@ async def test_set_active_database(
624626 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
625627 mock_multi_db_config .health_checks = [mock_hc ]
626628
627- with (
628- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
629- ):
629+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
630630 mock_db1 .client .execute_command .return_value = "OK1"
631631 mock_db .client .execute_command .return_value = "OK"
632632
0 commit comments