CI for mx_bec / e2e tests with pxi_bec (push) Failing after 1m4s
CI for mx_bec / e2e tests with pxii_bec (push) Failing after 1m17s
CI for mx_bec / e2e tests with pxiii_bec (push) Failing after 1m17s
CI for mx_bec / unit tests with pxiii_bec (push) Failing after 1m47s
CI for mx_bec / unit tests with pxii_bec (push) Failing after 1m55s
CI for mx_bec / unit tests with pxi_bec (push) Failing after 1m55s
# MX task architecture: AareBec service, tasks, queues and widgets Brings the AareBec task architecture into `mx_bec` ## Components - **AareBec service** (`service.py`, `main.py`): custom BEC client that owns the task queue, the actor manager and the external API clients. - **Task framework** (`tasks/core.py`): `@task` decorator and `TaskManager` with `Server` and `Client` roles. The server runs tasks one at a time and pauses the queue on failure; clients request tasks over Redis. `MxTaskQueue` works off the execution list. - **MX tasks** (`tasks/mx_tasks.py`): sample mount, optical centering, raster and multi-raster scans, X-ray centering, rotation data collection, beamline state change and cleanup. - **Automation actors** (`actors/`, `actor_manager.py`): actors that react to sequence milestones and queue the next task, each switchable through a Redis config value. - **External clients**: `AareDaq` REST client (Kerberos token, baton handling), `JFJoch` raster client, `OpticalPrediction` (AareLC). The `AareDaq` client is to be replaced with native BEC implementations. - **Devices** (`devices/`): Aerotech, Smargon (smargopolo), HTTP device base, simulated stages and sample changer, plus a demo config. - **Widgets** (`bec_widgets/widgets/aare_bec/`): actor config, task queue, task panel, sample queue and sample spreadsheet display - for development and admin use. - **Endpoints and messages** for task requests, the execution list, queue-change notifications and sequence state. ## Notes - BEC-driven optical centering was removed; it will be reimplemented from scratch. - The sample spreadsheet is copied between Redis instances by a hack in the service, to be replaced by the sample database connection module. - CI installs from the MX Gitea package index (`jfjoch_client` is pre-release only). --------- Co-authored-by: x10sa <x10sa@psi.ch> Reviewed-on: #2 Co-authored-by: David Perl <david.perl@psi.ch>
180 lines
6.1 KiB
Python
180 lines
6.1 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from bec_lib.client import BECClient
|
|
from bec_lib.service_config import ServiceConfig
|
|
from bec_lib.tests.utils import ConnectorMock
|
|
|
|
from mx_bec.constants import AareBecWorkflowState
|
|
from mx_bec.endpoints import QueueEndpoints
|
|
from mx_bec.messages import MXQueueChangedMessage, MXTaskExecutionMessage
|
|
from mx_bec.service import AareBec
|
|
from mx_bec.tasks import MxTasks, RoleT, Server, task
|
|
|
|
|
|
@pytest.fixture
|
|
def service(monkeypatch):
|
|
BECClient._reset_singleton()
|
|
monkeypatch.setenv("CONFIG_REDIS_URL", "redis://dummy:6379")
|
|
# the config client connects to the beamline redis on construction
|
|
# HAX: the service reads the sample spreadsheet straight from the beamline redis on
|
|
# construction (mx_bec/service.py lines 57-63), so that connection is patched out too. Remove
|
|
# this patch together with that hack.
|
|
with patch("mx_bec.service.AareConfigClient"), patch("mx_bec.service.Redis"):
|
|
svc = AareBec(ServiceConfig(redis={"host": "dummy", "port": 6379}), ConnectorMock)
|
|
yield svc
|
|
BECClient._reset_singleton()
|
|
|
|
|
|
def test_service_is_idle_before_start(service):
|
|
assert service.workflow_state == AareBecWorkflowState.IDLE
|
|
assert service.actor_manager is None
|
|
|
|
|
|
@pytest.fixture
|
|
def started_service(service):
|
|
"""Run `service.start()` with the BECClient internals and the actor/task machinery mocked."""
|
|
service.connector = MagicMock() # normally created by the patched-out BECClient.start
|
|
with (
|
|
patch.object(BECClient, "start") as base_start,
|
|
patch("mx_bec.service.AareActorManager") as manager_cls,
|
|
patch("mx_bec.service.MxTaskQueue") as queue_cls,
|
|
patch.object(AareBec, "status", create=True),
|
|
):
|
|
service.start()
|
|
yield service, base_start, manager_cls, queue_cls
|
|
|
|
|
|
def test_start_creates_actor_manager(started_service):
|
|
service, base_start, manager_cls, _ = started_service
|
|
base_start.assert_called_once()
|
|
manager_cls.assert_called_once_with(service)
|
|
assert service.actor_manager is manager_cls.return_value
|
|
|
|
|
|
def test_start_creates_and_starts_task_queue(started_service):
|
|
service, _, _, queue_cls = started_service
|
|
queue_cls.assert_called_once_with(service, service.tasks)
|
|
queue_cls.return_value.start.assert_called_once()
|
|
assert service.task_queue is queue_cls.return_value
|
|
|
|
|
|
def test_shutdown_stops_actor_manager_and_task_queue(service):
|
|
service.actor_manager = MagicMock()
|
|
service.task_queue = MagicMock()
|
|
manager, queue = service.actor_manager, service.task_queue
|
|
with patch.object(BECClient, "shutdown") as base_shutdown:
|
|
service.shutdown()
|
|
manager.shutdown.assert_called_once()
|
|
queue.shutdown.assert_called_once()
|
|
base_shutdown.assert_called_once()
|
|
assert service.actor_manager is None
|
|
assert service.task_queue is None
|
|
|
|
|
|
def test_start_registers_task_request_callback(started_service):
|
|
service, *_ = started_service
|
|
service.connector.register.assert_called_once_with(
|
|
QueueEndpoints.task_request(), cb=service._on_task_request
|
|
)
|
|
|
|
|
|
def test_shutdown_unregisters_task_request_callback(service):
|
|
service.started = True
|
|
service.connector = MagicMock()
|
|
with patch.object(BECClient, "shutdown"):
|
|
service.shutdown()
|
|
service.connector.unregister.assert_called_once_with(
|
|
QueueEndpoints.task_request(), cb=service._on_task_request
|
|
)
|
|
|
|
|
|
def test_shutdown_before_start_does_not_touch_connector(service):
|
|
with patch.object(BECClient, "shutdown") as base_shutdown:
|
|
service.shutdown()
|
|
base_shutdown.assert_called_once()
|
|
|
|
|
|
def test_task_request_for_known_method_is_queued_and_announced(service):
|
|
service.connector = MagicMock()
|
|
msg = MXTaskExecutionMessage(name="run_optical_centering", args=(3,), kwargs={})
|
|
service._on_task_request({"data": msg})
|
|
service.connector.rpush.assert_called_once_with(QueueEndpoints.task_execution(), msg)
|
|
service.connector.send.assert_called_once_with(
|
|
QueueEndpoints.task_queue_changed(), MXQueueChangedMessage()
|
|
)
|
|
|
|
|
|
NOT_TASKS = [
|
|
"no_such_task",
|
|
"_publish_optical_centering_done", # private method
|
|
"_client", # private attribute
|
|
"_busy_lock",
|
|
"request", # public, but not a task
|
|
"execute",
|
|
"is_task",
|
|
"planner", # property
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("name", NOT_TASKS)
|
|
def test_task_request_for_non_task_name_is_rejected(service, name):
|
|
service.connector = MagicMock()
|
|
msg = MXTaskExecutionMessage(name=name, args=(), kwargs={})
|
|
with patch("mx_bec.service.logger") as logger:
|
|
service._on_task_request({"data": msg})
|
|
service.connector.rpush.assert_not_called()
|
|
service.connector.send.assert_not_called()
|
|
logger.warning.assert_called_once()
|
|
assert name in logger.warning.call_args.args[0]
|
|
|
|
|
|
def test_tasks_are_exactly_the_decorated_methods():
|
|
class Tasks(MxTasks[RoleT]):
|
|
@task
|
|
def decorated(self): ...
|
|
|
|
def plain(self): ...
|
|
|
|
assert Tasks.is_task("decorated")
|
|
assert not Tasks.is_task("plain")
|
|
assert MxTasks.is_task("run_optical_centering")
|
|
assert all(not MxTasks.is_task(name) for name in NOT_TASKS)
|
|
|
|
|
|
class _FailingTasks(MxTasks[RoleT]):
|
|
@task
|
|
def boom(self):
|
|
raise RuntimeError("boom")
|
|
|
|
@task
|
|
def fine(self):
|
|
return None
|
|
|
|
|
|
def test_failed_task_pauses_the_queue_and_reraises():
|
|
client = MagicMock()
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
_FailingTasks[Server](client).boom()
|
|
client.task_queue.pause.assert_called_once()
|
|
|
|
|
|
def test_successful_task_does_not_pause_the_queue():
|
|
client = MagicMock()
|
|
assert _FailingTasks[Server](client).fine() is None
|
|
client.task_queue.pause.assert_not_called()
|
|
|
|
|
|
def test_failed_task_reraises_without_a_queue():
|
|
client = MagicMock()
|
|
client.task_queue = None # the queue only exists once the service has started
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
_FailingTasks[Server](client).boom()
|
|
|
|
|
|
def test_failed_task_reraises_even_if_pausing_fails():
|
|
client = MagicMock()
|
|
client.task_queue.pause.side_effect = ConnectionError("redis gone")
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
_FailingTasks[Server](client).boom()
|