Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stage synchronous if arming not started #92

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/dodal/devices/eiger.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class ArmingSignal(Signal):
def set(self, value, *, timeout=None, settle_time=None, **kwargs):
return self.parent.async_stage()

class ArmedState(Enum):
UNARMED = "unarmed"
ARMING = "arming"
ARMED = "armed"

armed_state = ArmedState.UNARMED

do_arm: ArmingSignal = Component(ArmingSignal)
cam: EigerDetectorCam = Component(EigerDetectorCam, "CAM:")
odin: EigerOdin = Component(EigerOdin, "")
Expand Down Expand Up @@ -79,8 +86,19 @@ def async_stage(self):
status_ok, error_message = self.odin.check_odin_initialised()
if not status_ok:
raise Exception(f"Odin not initialised: {error_message}")
self.armed_state = self.ArmedState.ARMING
self.arming_status = self.do_arming_chain()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should: You should create a placeholder class variable for this that's initialised to a complete status, this helps autocomplete etc.

return self.arming_status

return self.do_arming_chain()
def stage(self):
if self.armed_state.value == "unarmed":
self.async_stage().wait(60)

elif self.armed_state.value == "arming":
self.arming_status.wait(60)

else:
return None

def unstage(self) -> bool:
assert self.detector_params is not None
Expand All @@ -99,11 +117,15 @@ def unstage(self) -> bool:

LOGGER.info("Disarming detector")
self.disarm_detector()

self.armed_state = self.ArmedState.UNARMED
status_ok = self.odin.check_odin_state()
self.disable_roi_mode()
return status_ok

# This is no longer used by staging
def enable_roi_mode(self):
self.change_roi_mode(True)

def disable_roi_mode(self):
self.change_roi_mode(False)

Expand Down Expand Up @@ -265,6 +287,7 @@ def _wait_fan_ready(self) -> Status:

def _finish_arm(self) -> Status:
LOGGER.info("Eiger staging: Finishing arming")
self.armed_state = self.ArmedState.ARMED
return Status(done=True, success=True)

def forward_bit_depth_to_filewriter(self):
Expand Down
3 changes: 2 additions & 1 deletion src/dodal/devices/utils.py
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't related to #76 but I noticed the the status in the final function in the callback chain was never being checked

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def run_functions_without_blocking(
# intermediate statuses have an exception, the full_status will timeout.
full_status = Status(timeout=timeout)

def closing_func():
def closing_func(old_status):
check_callback_error(old_status)
full_status.set_finished()

# Wrap each function by first checking the previous status and attaching a callback to the next
Expand Down
68 changes: 65 additions & 3 deletions tests/devices/unit_tests/test_eiger.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
TEST_DET_DIST_TO_BEAM_CONVERTER_PATH = "tests/devices/unit_tests/test_lookup_table.txt"


class StatusException(Exception):
pass


def create_new_params() -> DetectorParams:
return DetectorParams(
current_energy=TEST_CURRENT_ENERGY,
Expand Down Expand Up @@ -443,11 +447,24 @@ def test_when_stage_called_then_finish_arm_on_fan_ready(
def test_check_callback_error(fake_eiger: EigerDetector, iteration):
def get_bad_status():
status = Status()
status.set_exception(Exception)
status.set_exception(StatusException)
return status

def get_good_status():
status = Status()
status.set_finished()
return status

LOGGER.error = MagicMock()

# These functions timeout without extra tweaking rather than give us the specific status error for the test
fake_eiger.set_odin_pvs = MagicMock()
fake_eiger.set_odin_pvs.return_value = get_good_status()
fake_eiger._wait_for_odin_status = MagicMock()
fake_eiger._wait_for_odin_status.return_value = get_good_status()
fake_eiger._wait_fan_ready = MagicMock()
fake_eiger._wait_fan_ready.return_value = get_good_status()

unwrapped_funcs = [
(
lambda: fake_eiger.set_detector_threshold(
Expand All @@ -467,8 +484,8 @@ def get_bad_status():

unwrapped_funcs[iteration] = get_bad_status

with pytest.raises(Exception):
run_functions_without_blocking(unwrapped_funcs).wait()
with pytest.raises(StatusException):
run_functions_without_blocking(unwrapped_funcs).wait(timeout=10)
LOGGER.error.assert_called_once()


Expand Down Expand Up @@ -499,3 +516,48 @@ def test_given_in_free_run_mode_when_unstaged_then_waiting_on_file_writer_to_fin

assert fake_eiger.odin.meta.stop_writing.get() == 1
assert fake_eiger.odin.file_writer.capture.get() == 0


def test_if_arming_in_progress_then_stage_waits_for_completion(
fake_eiger: EigerDetector, mock_set_odin_filewriter
):
assert fake_eiger.armed_state.value == "unarmed"
fake_eiger.do_async_staging = MagicMock(return_value=Status(timeout=0))
fake_eiger.async_stage()
assert fake_eiger.armed_state.value == "arming"

# Should do .wait and error on timeout
with pytest.raises(Exception):
fake_eiger.stage()

# State still in arming, but now wait should be done successfully
fake_eiger.arming_status = Status(done=True, success=True)
assert fake_eiger.stage() is None


def test_if_eiger_isnt_armed_then_stage_calls_async_stage(fake_eiger: EigerDetector):
assert fake_eiger.armed_state.value == "unarmed"
fake_eiger.async_stage = MagicMock()
fake_eiger.stage()
fake_eiger.async_stage.assert_called_once()


def test_if_eiger_is_armed_then_stage_does_nothing(fake_eiger: EigerDetector):
fake_eiger.armed_state = fake_eiger.ArmedState.ARMED
fake_eiger.async_stage = MagicMock()
assert fake_eiger.stage() is None
fake_eiger.async_stage.assert_not_called()


def test_armed_state_goes_to_armed_upon_stage_completion(fake_eiger: EigerDetector):
fake_eiger._finish_arm()
assert fake_eiger.armed_state.value == "armed"


def test_armed_state_goes_to_unarmed_after_unstage(
fake_eiger: EigerDetector, mock_set_odin_filewriter
):
fake_eiger.armed_state = fake_eiger.ArmedState.ARMED
fake_eiger.filewriters_finished = MagicMock()
fake_eiger.unstage()
assert fake_eiger.armed_state.value == "unarmed"