Skip to content

Commit 63fedb5

Browse files
committed
Write flyscanning test
1 parent 50727d8 commit 63fedb5

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/test_tutorials.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import importlib
22
import re
33
import time
4+
from collections import defaultdict
45
from pathlib import Path
56
from unittest.mock import patch
67

8+
import bluesky.plan_stubs as bps
9+
import bluesky.preprocessors as bpp
10+
import h5py
711
import pytest
812
from bluesky.run_engine import RunEngine
913

14+
from ophyd_async.core import TriggerInfo
15+
from ophyd_async.sim import FlySimMotorInfo
16+
from ophyd_async.testing import assert_emitted
17+
1018
# https://regex101.com/r/KvLj7t/1
1119
SCAN_LINE = re.compile(
1220
r"^\| *(\d+) \|[^\|]*\| *(\d*.\d*) \| *(\d*.\d*) \| *(\d*) \| *(\d*) \| *(\d*) \|$",
@@ -45,3 +53,57 @@ def test_implementing_devices(module, capsys, expected_scan_output):
4553
captured = capsys.readouterr()
4654
assert captured.err == ""
4755
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

Comments
 (0)