Files
mx_bec/tests/unit/test_aerotech.py
perl_dandx10sa d18c33f3b6
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
AareBec task service (#2)
# 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>
2026-11-24 14:41:31 +01:00

95 lines
3.4 KiB
Python

from unittest.mock import MagicMock, call
import pytest
from mx_bec.devices.aerotech import Aerotech
from mx_bec.devices.http import HttpRestError
PREFIX = "http://aarescan:5234"
def _response(ok=True, status_code=200, reason="OK", url=PREFIX, method="POST"):
resp = MagicMock(ok=ok, status_code=status_code, reason=reason)
resp.request.method, resp.request.url = method, url
return resp
@pytest.fixture
def aerotech():
device = Aerotech(name="aerotech", prefix=PREFIX)
# every REST call goes through the controller's session; never touch the network
device.controller._session = MagicMock()
device.controller._session.post.return_value = _response()
yield device
device.destroy()
def _configure(aerotech, n_x=4, n_y=3, exp_time_s=0.1):
config = aerotech.gridscan_config
config.grid_elem_count_x.put(n_x)
config.grid_elem_count_y.put(n_y)
config.grid_elem_size_x_um.put(50.0)
config.grid_elem_size_y_um.put(40.0)
config.time_per_element_sec.put(exp_time_s)
return config
def test_config_change_validates_with_aarescan(aerotech):
session = aerotech.controller._session
config = _configure(aerotech)
assert session.post.call_args == call(
f"{PREFIX}/validate/grid_scan", json=config.request_body()
)
assert config.valid.get() is True and config.message.get() == ""
session.post.return_value = _response(ok=False, status_code=400, reason="Bad Request")
session.post.return_value.json.return_value = "grid too large"
config.grid_elem_count_x.put(1000)
assert config.valid.get() is False and config.message.get() == "grid too large"
def test_trigger_starts_grid_scan_and_waits_till_done(aerotech):
session = aerotech.controller._session
config = _configure(aerotech, n_x=4, n_y=3, exp_time_s=0.1)
session.post.reset_mock()
status = aerotech.trigger()
status.wait(timeout=5)
expected_timeout = round((0.1 * 4 * 3 + 3 * 0.3) * 1.1 + Aerotech.GRID_SCAN_TIMEOUT_MARGIN_S)
assert session.post.call_args_list == [
call(f"{PREFIX}/grid_scan", params=None, json={**config.request_body(), "run_async": True}),
call(f"{PREFIX}/wait_till_done", params={"timeout": expected_timeout}, json=None),
]
def test_trigger_status_fails_when_aarescan_times_out(aerotech):
session = aerotech.controller._session
_configure(aerotech)
session.post.side_effect = [
_response(), # grid_scan accepted
_response(ok=False, status_code=504, reason="Gateway Timeout"), # wait_till_done
]
status = aerotech.trigger()
with pytest.raises(HttpRestError, match="504"):
status.wait(timeout=5)
def test_trigger_refuses_invalid_config(aerotech):
session = aerotech.controller._session
session.post.return_value = _response(ok=False, status_code=400, reason="Bad Request")
session.post.return_value.json.return_value = "nope"
_configure(aerotech)
session.post.reset_mock()
with pytest.raises(ValueError, match="nope"):
aerotech.trigger()
session.post.assert_not_called()
def test_wait_till_done_timeout_is_clamped(aerotech):
session = aerotech.controller._session
aerotech.controller.wait_till_done(timeout_s=1e6)
assert session.post.call_args.kwargs["params"] == {"timeout": 3600}
aerotech.controller.wait_till_done(timeout_s=-5)
assert session.post.call_args.kwargs["params"] == {"timeout": 0}