Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions helm/blueapi/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
"description": "Name of the device manager in the module",
"title": "Name",
"type": "string"
},
"check_connected": {
"default": false,
"description": "If true, all devices must be successfully connected at startup.",
"title": "Check Connected",
"type": "boolean"
}
},
"required": [
Expand Down
6 changes: 6 additions & 0 deletions helm/blueapi/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,12 @@
"module"
],
"properties": {
"check_connected": {
"title": "Check Connected",
"description": "If true, all devices must be successfully connected at startup.",
"default": false,
"type": "boolean"
},
"kind": {
"title": "Kind",
"default": "deviceManager",
Expand Down
4 changes: 4 additions & 0 deletions src/blueapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class DeviceManagerSource(Source):
name: str = Field(
default="devices", description="Name of the device manager in the module"
)
check_connected: bool = Field(
default=False,
description="If true, all devices must be successfully connected at startup.",
)


class TcpUrl(AnyUrl):
Expand Down
11 changes: 9 additions & 2 deletions src/blueapi/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,20 @@ def with_config(self, config: EnvironmentConfig) -> None:
self.with_device_module(mod)
case DodalSource(mock=mock):
self.with_dodal_module(mod, mock=mock)
case DeviceManagerSource(mock=mock, name=name):
case DeviceManagerSource(
mock=mock, name=name, check_connected=check_connected
):
manager = getattr(mod, name)
if not isinstance(manager, DeviceManager):
raise ValueError(
f"{name} in module {mod} is not a device manager"
)
self.with_device_manager(manager, mock)
_, error_map = self.with_device_manager(manager, mock)
if check_connected and error_map:
raise RuntimeError(
"Errors occurred while building/connecting the following "
f"devices: {', '.join(error_map)}",
)

def with_plan_module(self, module: ModuleType) -> None:
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/unit_tests/core/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from dodal.common import PlanGenerator, inject
from ophyd import Device
from ophyd_async.core import (
NotConnectedError,
PathProvider,
StandardDetector,
StaticPathProvider,
Expand Down Expand Up @@ -153,6 +154,20 @@ def devicey_context(sim_motor: Motor, sim_detector: StandardDetector) -> Bluesky
return ctx


@pytest.fixture
def beamline_with_connection_and_build_errors():
stm = StaticDeviceManager(
devices={},
build_errors={"foo": RuntimeError("Simulated Build Error")},
connection_errors={"bar": NotConnectedError("Simulated Connection Error")},
)
dev_mod = Mock(spec=ModuleType)
dev_mod.devices = stm
with patch("blueapi.core.context.import_module") as imp_mod:
imp_mod.side_effect = lambda mod: dev_mod if mod == "foo.bar" else None
yield


class SomeConfigurable:
def read_configuration(self) -> SyncOrAsync[dict[str, Reading]]:
return {}
Expand Down Expand Up @@ -425,6 +440,27 @@ def test_with_config_passes_mock_to_with_dodal_module(
mock_with_dodal_module.assert_called_once_with(ANY, mock=mock)


def test_with_config_raises_exception_group_on_connection_errors_when_ensure_connected(
empty_context: BlueskyContext, beamline_with_connection_and_build_errors: None
):
with pytest.raises(
RuntimeError, match="Errors occurred while building/connecting.*"
):
empty_context.with_config(
EnvironmentConfig(
sources=[DeviceManagerSource(module="foo.bar", check_connected=True)]
)
)


def test_with_config_ignores_build_connect_exceptions_by_default(
empty_context: BlueskyContext, beamline_with_connection_and_build_errors: None
):
empty_context.with_config(
EnvironmentConfig(sources=[DeviceManagerSource(module="foo.bar")])
)


def test_function_spec(empty_context: BlueskyContext):
spec = empty_context._type_spec_for_function(has_some_params)
assert spec["foo"][0] is int
Expand Down