@@ -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"
@@ -213,7 +208,7 @@ async def mock_check_health(database):
213208
214209 # Wait for circuit breaker state to actually reflect the unhealthy status
215210 # (instead of just sleeping)
216- max_retries = 10
211+ max_retries = 20
217212 for _ in range (max_retries ):
218213 if cb2 .state == CBState .OPEN : # Circuit is open (unhealthy)
219214 break
@@ -266,9 +261,7 @@ async def mock_check_health(database):
266261 mock_hc .check_health .side_effect = mock_check_health
267262 mock_multi_db_config .health_checks = [mock_hc ]
268263
269- with (
270- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
271- ):
264+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
272265 mock_db .client .execute_command .return_value = "OK"
273266 mock_db1 .client .execute_command .return_value = "OK1"
274267 mock_db2 .client .execute_command .return_value = "OK2"
@@ -280,7 +273,7 @@ async def mock_check_health(database):
280273 assert await client .set ("key" , "value" ) == "OK1"
281274 await error_event .wait ()
282275 # Wait for circuit breaker to actually open (not just the event)
283- max_retries = 10
276+ max_retries = 20
284277 for _ in range (max_retries ):
285278 if mock_db1 .circuit .state == CBState .OPEN : # Circuit is open
286279 break
@@ -328,9 +321,7 @@ async def mock_check_health(database):
328321 mock_hc .check_health .side_effect = mock_check_health
329322 mock_multi_db_config .health_checks = [mock_hc ]
330323
331- with (
332- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
333- ):
324+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
334325 mock_db .client .execute_command .return_value = "OK"
335326 mock_db1 .client .execute_command .return_value = "OK1"
336327 mock_db2 .client .execute_command .return_value = "OK2"
@@ -341,6 +332,15 @@ async def mock_check_health(database):
341332 async with MultiDBClient (mock_multi_db_config ) as client :
342333 assert await client .set ("key" , "value" ) == "OK1"
343334 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 )
344344 assert await client .set ("key" , "value" ) == "OK2"
345345 await asyncio .sleep (0.5 )
346346 assert await client .set ("key" , "value" ) == "OK2"
@@ -364,9 +364,7 @@ async def test_execute_command_throws_exception_on_failed_initialization(
364364 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
365365 mock_multi_db_config .health_checks = [mock_hc ]
366366
367- with (
368- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
369- ):
367+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
370368 mock_hc .check_health .return_value = False
371369
372370 client = MultiDBClient (mock_multi_db_config )
@@ -398,9 +396,7 @@ async def test_add_database_throws_exception_on_same_database(
398396 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
399397 mock_multi_db_config .health_checks = [mock_hc ]
400398
401- with (
402- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
403- ):
399+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
404400 mock_hc .check_health .return_value = False
405401
406402 client = MultiDBClient (mock_multi_db_config )
@@ -429,9 +425,7 @@ async def test_add_database_makes_new_database_active(
429425 databases = create_weighted_list (mock_db , mock_db2 )
430426 mock_multi_db_config .health_checks = [mock_hc ]
431427
432- with (
433- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
434- ):
428+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
435429 mock_db1 .client .execute_command .return_value = "OK1"
436430 mock_db2 .client .execute_command .return_value = "OK2"
437431
@@ -467,9 +461,7 @@ async def test_remove_highest_weighted_database(
467461 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
468462 mock_multi_db_config .health_checks = [mock_hc ]
469463
470- with (
471- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
472- ):
464+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
473465 mock_db1 .client .execute_command .return_value = "OK1"
474466 mock_db2 .client .execute_command .return_value = "OK2"
475467
@@ -503,9 +495,7 @@ async def test_update_database_weight_to_be_highest(
503495 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
504496 mock_multi_db_config .health_checks = [mock_hc ]
505497
506- with (
507- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
508- ):
498+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
509499 mock_db1 .client .execute_command .return_value = "OK1"
510500 mock_db2 .client .execute_command .return_value = "OK2"
511501
@@ -541,9 +531,7 @@ async def test_add_new_failure_detector(
541531 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
542532 mock_multi_db_config .health_checks = [mock_hc ]
543533
544- with (
545- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
546- ):
534+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
547535 mock_db1 .client .execute_command .return_value = "OK1"
548536 mock_multi_db_config .event_dispatcher = EventDispatcher ()
549537 mock_fd = mock_multi_db_config .failure_detectors [0 ]
@@ -562,7 +550,7 @@ async def test_add_new_failure_detector(
562550 assert mock_hc .check_health .call_count == 9
563551
564552 # Simulate failing command events that lead to a failure detection
565- for i in range (5 ):
553+ for _ in range (5 ):
566554 await mock_multi_db_config .event_dispatcher .dispatch_async (
567555 command_fail_event
568556 )
@@ -573,7 +561,7 @@ async def test_add_new_failure_detector(
573561 client .add_failure_detector (another_fd )
574562
575563 # Simulate failing command events that lead to a failure detection
576- for i in range (5 ):
564+ for _ in range (5 ):
577565 await mock_multi_db_config .event_dispatcher .dispatch_async (
578566 command_fail_event
579567 )
@@ -600,9 +588,7 @@ async def test_add_new_health_check(
600588 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
601589 mock_multi_db_config .health_checks = [mock_hc ]
602590
603- with (
604- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
605- ):
591+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
606592 mock_db1 .client .execute_command .return_value = "OK1"
607593
608594 mock_hc .check_health .return_value = True
@@ -640,9 +626,7 @@ async def test_set_active_database(
640626 databases = create_weighted_list (mock_db , mock_db1 , mock_db2 )
641627 mock_multi_db_config .health_checks = [mock_hc ]
642628
643- with (
644- patch .object (mock_multi_db_config , "databases" , return_value = databases ),
645- ):
629+ with patch .object (mock_multi_db_config , "databases" , return_value = databases ):
646630 mock_db1 .client .execute_command .return_value = "OK1"
647631 mock_db .client .execute_command .return_value = "OK"
648632
0 commit comments