Skip to content

Commit

Permalink
WIP: mostly finished
Browse files Browse the repository at this point in the history
Discovered a problem with detectors where controllers are armed on prepare and stage, but disarmed only once.
  • Loading branch information
evalott100 committed Aug 23, 2024
1 parent ff55783 commit 864bb5c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/ophyd_async/core/_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ async def _check_config_sigs(self):
@AsyncStatus.wrap
async def unstage(self) -> None:
# Stop data writing.
await self.writer.close()
await asyncio.gather(self.writer.close(), self.controller.disarm())

async def read_configuration(self) -> Dict[str, Reading]:
return await merge_gathered_dicts(sig.read() for sig in self._config_sigs)
Expand Down
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def error_and_kill_pending_tasks():

@pytest.fixture(scope="function")
def RE(request: FixtureRequest):
loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
loop.set_debug(True)
RE = RunEngine({}, call_returns_result=True, loop=loop)

Expand All @@ -119,13 +119,16 @@ def clean_event_loop():
# with each RE test so we don't need to do this
loop.call_soon_threadsafe(loop.stop)
RE._th.join()
for task in unfinished_tasks:
task.cancel()
loop.close()

# If the tasks are still pending because the test failed
# for other reasons then we don't have to error here
if unfinished_tasks and request.session.testsfailed == fail_count:
import pprint
raise RuntimeError(
f"Not all tasks {unfinished_tasks} "
f"Not all tasks \n{pprint.pformat(unfinished_tasks)}\n"
f"closed during test {request.node.name}."
)

Expand Down
51 changes: 30 additions & 21 deletions tests/epics/adsimdetector/test_sim.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Integration tests for a StandardDetector using a ADHDFWriter and SimController."""

import asyncio
import time
from collections import defaultdict
from pathlib import Path
Expand Down Expand Up @@ -192,22 +193,40 @@ async def test_two_detectors_step(
for det in two_detectors
]

RE(count_sim(two_detectors, times=1))

controller_a = cast(adsimdetector.SimController, two_detectors[0].controller)
writer_a = cast(adcore.ADHDFWriter, two_detectors[0].writer)
writer_b = cast(adcore.ADHDFWriter, two_detectors[1].writer)
info_a = writer_a._path_provider(device_name=writer_a.hdf.name)
info_b = writer_b._path_provider(device_name=writer_b.hdf.name)
file_name_a = None
file_name_b = None

drv = controller_a.driver
assert 1 == await drv.acquire.get_value()
assert adcore.ImageMode.multiple == await drv.image_mode.get_value()
def plan():
nonlocal file_name_a, file_name_b
yield from count_sim(two_detectors, times=1)

drv = controller_a.driver
assert False is (yield from bps.rd(drv.acquire))
assert adcore.ImageMode.multiple == (yield from bps.rd(drv.image_mode))

hdfb = writer_b.hdf
assert True is (yield from bps.rd(hdfb.lazy_open))
assert True is (yield from bps.rd(hdfb.swmr_mode))
assert 0 == (yield from bps.rd(hdfb.num_capture))
assert adcore.FileWriteMode.stream == (yield from bps.rd(hdfb.file_write_mode))

assert (
(yield from bps.rd(writer_a.hdf.file_path)) ==
str(info_a.directory_path)
)
file_name_a = (yield from bps.rd(writer_a.hdf.file_name))
assert file_name_a == info_a.filename

hdfb = writer_b.hdf
assert True is await hdfb.lazy_open.get_value()
assert True is await hdfb.swmr_mode.get_value()
assert 0 == await hdfb.num_capture.get_value()
assert adcore.FileWriteMode.stream == await hdfb.file_write_mode.get_value()
assert (yield from bps.rd(writer_b.hdf.file_path)) == str(info_b.directory_path)
file_name_b = (yield from bps.rd(writer_b.hdf.file_name))
assert file_name_b == info_b.filename

RE(plan())
assert names == [
"start",
"descriptor",
Expand All @@ -218,16 +237,6 @@ async def test_two_detectors_step(
"event",
"stop",
]
info_a = writer_a._path_provider(device_name=writer_a.hdf.name)
info_b = writer_b._path_provider(device_name=writer_b.hdf.name)

assert await writer_a.hdf.file_path.get_value() == str(info_a.directory_path)
file_name_a = await writer_a.hdf.file_name.get_value()
assert file_name_a == info_a.filename

assert await writer_b.hdf.file_path.get_value() == str(info_b.directory_path)
file_name_b = await writer_b.hdf.file_name.get_value()
assert file_name_b == info_b.filename

_, descriptor, sra, sda, srb, sdb, event, _ = docs
assert descriptor["configuration"]["testa"]["data"]["testa-drv-acquire_time"] == 0.8
Expand Down Expand Up @@ -342,7 +351,7 @@ async def test_trigger_logic():
),
],
)
def test_detector_with_unnamed_or_disconnected_config_sigs(
async def test_detector_with_unnamed_or_disconnected_config_sigs(
RE,
static_filename_provider: StaticFilenameProvider,
tmp_path: Path,
Expand Down
5 changes: 4 additions & 1 deletion tests/sim/test_sim_detector.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import asyncio
from collections import defaultdict
from pprint import pprint

import bluesky.plans as bp
import h5py
import numpy as np
from bluesky import RunEngine
from bluesky import plan_stubs as bps

from ophyd_async.core import assert_emitted
from ophyd_async.plan_stubs import ensure_connected
Expand All @@ -25,7 +28,7 @@ async def test_detector_creates_controller_and_writer(
assert sim_pattern_detector.controller


async def test_writes_pattern_to_file(
def test_writes_pattern_to_file(
sim_pattern_detector: PatternDetector,
RE: RunEngine,
):
Expand Down

0 comments on commit 864bb5c

Please sign in to comment.