88import pytest
99from ophyd_async .core import init_devices
1010from pytest import approx
11+ from redis .asyncio import ConnectionError
1112
1213from dodal .devices .i04 .murko_results import (
1314 RESULTS_COMPLETE_MESSAGE ,
@@ -25,6 +26,7 @@ async def murko_results(mock_strict_redis: MagicMock) -> MurkoResultsDevice:
2526 with init_devices (mock = True ):
2627 murko_results = MurkoResultsDevice (name = "murko_results" )
2728 murko_results .pubsub = AsyncMock ()
29+ murko_results .redis_connected = True
2830 return murko_results
2931
3032
@@ -443,6 +445,7 @@ async def test_trigger_calls_get_message_and_hget(
443445async def test_assert_subscribes_to_queue_and_clears_results_on_stage (
444446 murko_results : MurkoResultsDevice ,
445447):
448+ murko_results ._check_redis_connection = AsyncMock (return_value = True )
446449 murko_results ._x_mm_setter (1 )
447450 murko_results ._y_mm_setter (2 )
448451 murko_results ._z_mm_setter (3 )
@@ -807,3 +810,39 @@ async def test_trigger_stops_if_no_message_in_5_seconds_and_raises_warning(
807810 and record .levelname == "WARNING"
808811 for record in caplog .records
809812 )
813+
814+
815+ async def test_if_redis_connection_failed_then_no_error_is_raised_and_motors_set_to_0 (
816+ murko_results : MurkoResultsDevice ,
817+ ):
818+ murko_results .redis_client .ping = AsyncMock (side_effect = ConnectionError )
819+ await murko_results .stage ()
820+ await murko_results .trigger ()
821+ await murko_results .unstage ()
822+ assert await murko_results .x_mm .get_value () == 0
823+ assert await murko_results .y_mm .get_value () == 0
824+ assert await murko_results .z_mm .get_value () == 0
825+
826+
827+ async def test_if_redis_connection_failed_and_motors_have_values_then_motors_set_to_0 (
828+ murko_results : MurkoResultsDevice ,
829+ ):
830+ murko_results .redis_client .ping = AsyncMock (side_effect = ConnectionError )
831+ murko_results ._x_mm_setter (1 )
832+ murko_results ._y_mm_setter (2 )
833+ murko_results ._z_mm_setter (3 )
834+ assert await murko_results .x_mm .get_value () == 1
835+ assert await murko_results .y_mm .get_value () == 2
836+ assert await murko_results .z_mm .get_value () == 3
837+ await murko_results .stage ()
838+ assert await murko_results .x_mm .get_value () == 0
839+ assert await murko_results .y_mm .get_value () == 0
840+ assert await murko_results .z_mm .get_value () == 0
841+
842+
843+ async def test_if_redis_connects_then_pubsub_is_subscribed_to (
844+ murko_results : MurkoResultsDevice ,
845+ ):
846+ murko_results .redis_client .ping = AsyncMock ()
847+ await murko_results .stage ()
848+ murko_results .pubsub .subscribe .assert_called_once () # type: ignore
0 commit comments