From f0292c569070bb29bfcea4fa973b15597434a2e6 Mon Sep 17 00:00:00 2001 From: David Perl Date: Wed, 28 Feb 2024 18:24:49 +0000 Subject: [PATCH] DiamondLightSource/hyperion#1192 add test for passing on emit --- .../callbacks/ispyb_callback_base.py | 1 - .../callbacks/plan_reactive_callback.py | 3 +- .../callbacks/xray_centre/ispyb_callback.py | 2 +- .../callbacks/test_plan_reactive_callback.py | 46 ++++++++++++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/hyperion/external_interaction/callbacks/ispyb_callback_base.py b/src/hyperion/external_interaction/callbacks/ispyb_callback_base.py index 882912817..febbae832 100644 --- a/src/hyperion/external_interaction/callbacks/ispyb_callback_base.py +++ b/src/hyperion/external_interaction/callbacks/ispyb_callback_base.py @@ -39,7 +39,6 @@ def __init__( for self.ispyb_ids.""" ISPYB_LOGGER.debug("Initialising ISPyB callback") super().__init__(log=ISPYB_LOGGER, emit=emit) - self.emit_cb = emit # to avoid GC; base class only holds a WeakRef self.params: GridscanInternalParameters | RotationInternalParameters | None = ( None ) diff --git a/src/hyperion/external_interaction/callbacks/plan_reactive_callback.py b/src/hyperion/external_interaction/callbacks/plan_reactive_callback.py index 2b20b323f..267757b68 100644 --- a/src/hyperion/external_interaction/callbacks/plan_reactive_callback.py +++ b/src/hyperion/external_interaction/callbacks/plan_reactive_callback.py @@ -33,6 +33,7 @@ class will be activated, and on recieving the stop document for the in the future (https://github.com/DiamondLightSource/hyperion/issues/964).""" super().__init__(emit=emit) + self.emit_cb = emit # to avoid GC; base class only holds a WeakRef self.active = False self.activity_uid = 0 self.log = log @@ -96,4 +97,4 @@ def activity_gated_stop(self, doc: RunStop) -> RunStop | None: return doc def __repr__(self) -> str: - return f"<{self.__class__.__name__} with id: {hex(id(self))}>" + return f"<{self.__class__.__name__} with id: {hex(id(self))} - active: {self.active}>" diff --git a/src/hyperion/external_interaction/callbacks/xray_centre/ispyb_callback.py b/src/hyperion/external_interaction/callbacks/xray_centre/ispyb_callback.py index 95e6ed660..998b45f6c 100644 --- a/src/hyperion/external_interaction/callbacks/xray_centre/ispyb_callback.py +++ b/src/hyperion/external_interaction/callbacks/xray_centre/ispyb_callback.py @@ -54,7 +54,7 @@ def __init__( *, emit: Callable[..., Any] | None = None, ) -> None: - super().__init__() + super().__init__(emit=emit) self.params: GridscanInternalParameters self.ispyb: StoreGridscanInIspyb self.ispyb_ids: IspybIds = IspybIds() diff --git a/tests/unit_tests/external_interaction/callbacks/test_plan_reactive_callback.py b/tests/unit_tests/external_interaction/callbacks/test_plan_reactive_callback.py index 5aa924f78..abc704eac 100644 --- a/tests/unit_tests/external_interaction/callbacks/test_plan_reactive_callback.py +++ b/tests/unit_tests/external_interaction/callbacks/test_plan_reactive_callback.py @@ -2,6 +2,11 @@ import pytest from bluesky.run_engine import RunEngine +from event_model.documents import Event, EventDescriptor, RunStart, RunStop + +from hyperion.external_interaction.callbacks.plan_reactive_callback import ( + PlanReactiveCallback, +) from ..conftest import MockReactiveCallback, get_test_plan @@ -112,6 +117,45 @@ def mock_excepting_func(_): cb.log = MagicMock() with pytest.raises(MockTestException): - cb._run_activity_gated(mock_excepting_func, {"start": "test"}) + cb._run_activity_gated("start", mock_excepting_func, {"start": "test"}) cb.log.exception.assert_called_with(e) + + +def test_emit_called_correctly(): + receiving_cb = MockReactiveCallback() + test_cb = PlanReactiveCallback(emit=receiving_cb, log=MagicMock()) + + start_doc: RunStart = {"uid": "123", "time": 0} + desc_doc: EventDescriptor = { + "data_keys": {}, + "run_start": "123", + "uid": "987", + "time": 0, + } + event_doc: Event = { + "data": {}, + "time": 0, + "descriptor": "987", + "timestamps": {}, + "uid": "999", + "seq_num": 0, + } + stop_doc: RunStop = { + "exit_status": "success", + "run_start": "123", + "uid": "456", + "time": 0, + } + + test_cb.active = True + receiving_cb.active = True + + test_cb.start(start_doc) + receiving_cb.activity_gated_start.assert_called_with(start_doc) + test_cb.descriptor(desc_doc) + receiving_cb.activity_gated_descriptor.assert_called_with(desc_doc) + test_cb.event(event_doc) + receiving_cb.activity_gated_event.assert_called_with(event_doc) + test_cb.stop(stop_doc) + receiving_cb.activity_gated_stop.assert_called_with(stop_doc)