Skip to content

Commit

Permalink
DiamondLightSource/hyperion#1192 add test for passing on emit
Browse files Browse the repository at this point in the history
  • Loading branch information
dperl-dls committed Feb 28, 2024
1 parent e1a2b51 commit f0292c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}>"
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit f0292c5

Please sign in to comment.