|
1 | 1 | import importlib |
2 | 2 | import re |
3 | 3 | import time |
| 4 | +from collections import defaultdict |
4 | 5 | from pathlib import Path |
5 | 6 | from unittest.mock import patch |
6 | 7 |
|
| 8 | +import bluesky.plan_stubs as bps |
| 9 | +import bluesky.preprocessors as bpp |
| 10 | +import h5py |
7 | 11 | import pytest |
8 | 12 | from bluesky.run_engine import RunEngine |
9 | 13 |
|
| 14 | +from ophyd_async.core import TriggerInfo |
| 15 | +from ophyd_async.sim import FlySimMotorInfo |
| 16 | +from ophyd_async.testing import assert_emitted |
| 17 | + |
10 | 18 | # https://regex101.com/r/KvLj7t/1 |
11 | 19 | SCAN_LINE = re.compile( |
12 | 20 | r"^\| *(\d+) \|[^\|]*\| *(\d*.\d*) \| *(\d*.\d*) \| *(\d*) \| *(\d*) \| *(\d*) \|$", |
@@ -45,3 +53,57 @@ def test_implementing_devices(module, capsys, expected_scan_output): |
45 | 53 | captured = capsys.readouterr() |
46 | 54 | assert captured.err == "" |
47 | 55 | assert SCAN_LINE.findall(captured.out) == expected_scan_output |
| 56 | + |
| 57 | + |
| 58 | +def test_flyscanning_devices(capsys): |
| 59 | + with patch("matplotlib.get_backend"): |
| 60 | + with patch("bluesky.run_engine.autoawait_in_bluesky_event_loop"): |
| 61 | + main = importlib.import_module("ophyd_async.sim.__main__") |
| 62 | + # We want the text output of the best effort callback, but the plotting takes |
| 63 | + # too much time for CI, even if headless, so disable it |
| 64 | + main.bec._set_up_plots = lambda *args, **kwargs: None |
| 65 | + RE: RunEngine = main.RE |
| 66 | + |
| 67 | + @bpp.stage_decorator([main.bdet]) |
| 68 | + @bpp.run_decorator() |
| 69 | + def fly_plan(): |
| 70 | + # Move to the start |
| 71 | + yield from bps.prepare(main.bdet, TriggerInfo(number_of_triggers=7)) |
| 72 | + yield from bps.abs_set(main.stage.y.velocity, 0) |
| 73 | + yield from bps.abs_set(main.stage.y, 0) |
| 74 | + yield from bps.prepare( |
| 75 | + main.stage.x, FlySimMotorInfo(cv_start=1, cv_end=2, cv_time=0.7) |
| 76 | + ) |
| 77 | + yield from bps.wait() |
| 78 | + yield from bps.declare_stream(main.bdet, name="primary") |
| 79 | + # Kickoff stage and wait until at velocity |
| 80 | + yield from bps.kickoff(main.stage.x, wait=True) |
| 81 | + # Kickoff the detector |
| 82 | + yield from bps.kickoff(main.bdet, wait=True) |
| 83 | + # Collect the data |
| 84 | + yield from bps.collect_while_completing( |
| 85 | + flyers=[main.stage.x, main.bdet], dets=[main.bdet], flush_period=0.42 |
| 86 | + ) |
| 87 | + |
| 88 | + docs = defaultdict(list) |
| 89 | + RE.subscribe(lambda name, doc: docs[name].append(doc)) |
| 90 | + start = time.monotonic() |
| 91 | + RE(fly_plan()) |
| 92 | + assert time.monotonic() - start == pytest.approx(1.7, abs=0.2) |
| 93 | + captured = capsys.readouterr() |
| 94 | + assert captured.err == "" |
| 95 | + # assert captured.out == "" |
| 96 | + assert_emitted( |
| 97 | + docs, start=1, descriptor=1, stream_resource=2, stream_datum=4, stop=1 |
| 98 | + ) |
| 99 | + path = docs["stream_resource"][0]["uri"].split("://localhost")[-1] |
| 100 | + h5file = h5py.File(path) |
| 101 | + assert list(h5file["/entry/sum"]) == [ |
| 102 | + 506344, |
| 103 | + 524952, |
| 104 | + 538800, |
| 105 | + 547064, |
| 106 | + 549656, |
| 107 | + 547020, |
| 108 | + 538668, |
| 109 | + ] |
0 commit comments