Files
mx_bec/tests/unit/test_aaredaq_tasks.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

98 lines
3.6 KiB
Python

from unittest.mock import MagicMock
import pytest
from aarecommon.math.coordinate import Coordinate, SmargonCoordinate
from aarecommon.models.raster_grid import RasterGridRequest
from aarecommon.models.rotation_scan import RotationScanRequest
from mx_bec.decisions import smargon_nudge
from mx_bec.tasks import MxTasks, Server
# where the mock smargon sits
SMARGON_POSITION = {"x": 1.0, "y": 2.0, "z": 3.0, "phi": 0.0, "chi": 0.0}
@pytest.fixture
def client():
client = MagicMock()
for axis, position in SMARGON_POSITION.items():
getattr(client.devices.smargon, axis).get.return_value = position
return client
@pytest.fixture
def tasks(client):
return MxTasks[Server](client)
@pytest.mark.parametrize(
"name", ["aaredaq_mount_sample", "aaredaq_raster_scan", "aaredaq_rotation_scan"]
)
def test_aaredaq_methods_are_tasks(name):
assert MxTasks.is_task(name)
def test_mount_sample(tasks, client):
tasks.aaredaq_mount_sample(42)
client.aaredaq.mount_sample.assert_called_once_with(42, reference=False)
tasks.aaredaq_mount_sample(7, reference=True)
client.aaredaq.mount_sample.assert_called_with(7, reference=True)
def test_raster_scan_builds_request_from_flat_arguments(tasks, client):
tasks.aaredaq_raster_scan(
n_x=4, n_y=3, step_x_mm=0.05, step_y_mm=0.04, exp_time_s=0.1, omega_deg=12.0, dtz=150.0
)
request, kwargs = (
client.aaredaq.raster_scan.call_args.args[0],
client.aaredaq.raster_scan.call_args.kwargs,
)
# without a top left corner, the grid is centred on the current smargon position: the corner
# is half the grid away, in the lab frame at the scan's omega
half_grid = Coordinate(x=4 * 0.05 / 2, y=3 * 0.04 / 2, z=0)
top_left = Coordinate(x=1.0, y=2.0, z=3.0) - smargon_nudge(0.0, 0.0, 12.0, half_grid)
assert request == RasterGridRequest(
n_x=4,
n_y=3,
grid_size_mm=Coordinate(x=0.05, y=0.04),
exp_time_s=0.1,
omega_deg=12.0,
dtz=150.0,
smargon_top_left=SmargonCoordinate(sh_mm=top_left, chi_deg=0.0, phi_deg=0.0),
)
assert kwargs == {"auto_center": False}
def test_raster_scan_uses_a_given_top_left_corner(tasks, client):
top_left = SmargonCoordinate(sh_mm=Coordinate(x=0.1, y=0.2, z=0.3), chi_deg=10.0, phi_deg=20.0)
tasks.aaredaq_raster_scan(1, 1, 0.05, 0.05, 0.1, smargon_top_left=top_left)
assert client.aaredaq.raster_scan.call_args.args[0].smargon_top_left == top_left
client.devices.smargon.x.get.assert_not_called()
def test_raster_scan_passes_auto_center(tasks, client):
tasks.aaredaq_raster_scan(1, 1, 0.05, 0.05, 0.1, auto_center=True)
assert client.aaredaq.raster_scan.call_args.kwargs == {"auto_center": True}
def test_rotation_scan_builds_request_from_flat_arguments(tasks, client):
tasks.aaredaq_rotation_scan(
exp_time_s=0.05, incr_omega_deg=0.1, steps=100, start_omega_deg=90.0, file_prefix="rot"
)
assert client.aaredaq.rotation_scan.call_args.args[0] == RotationScanRequest(
exp_time_s=0.05, incr_omega_deg=0.1, steps=100, start_omega_deg=90.0, file_prefix="rot"
)
def test_screening_scan(tasks, client):
tasks.aaredaq_rotation_scan(0.05, 1.0, 4, screening=True, wedge_omega_deg=90.0)
request = client.aaredaq.rotation_scan.call_args.args[0]
assert request.screening is True and request.wedge_omega_deg == 90.0
def test_failed_aaredaq_call_pauses_queue_and_reraises(tasks, client):
client.aaredaq.mount_sample.side_effect = RuntimeError("sample not found")
with pytest.raises(RuntimeError, match="sample not found"):
tasks.aaredaq_mount_sample(1)
client.task_queue.pause.assert_called_once()