Skip to content

Commit

Permalink
Add epics test for busy signals
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Nov 14, 2024
1 parent 2d4b12b commit 559dcc5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ophyd_async/core/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,10 @@ async def observe_value(
signal.subscribe_value(q.put_nowait)
try:
while True:
item = await asyncio.wait_for(q.get(), timeout)
# yield here in case something else is filling the queue
# like in test_observe_value_times_out_with_no_external_task()
await asyncio.sleep(0)
item = await asyncio.wait_for(q.get(), timeout)
if done_status and item is done_status:
if exc := done_status.exception():
raise exc
Expand Down
6 changes: 6 additions & 0 deletions tests/epics/signal/test_records.db
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,9 @@ record(waveform, "$(P)ntndarray:data")
}
})
}

record(calc, "$(P)ticking") {
field(INPA, "$(P)ticking")
field(CALC, "A+1")
field(SCAN, ".1 second")
}
20 changes: 20 additions & 0 deletions tests/epics/signal/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
Array1D,
NotConnected,
SignalBackend,
SignalR,
SignalRW,
StrictEnum,
SubsetEnum,
T,
Table,
load_from_yaml,
observe_value,
save_to_yaml,
)
from ophyd_async.epics.core import (
Expand Down Expand Up @@ -935,3 +937,21 @@ def my_plan():
def test_signal_module_emits_deprecation_warning():
with pytest.deprecated_call():
import ophyd_async.epics.signal # noqa: F401


async def test_observe_ticking_signal_with_busy_loop(ioc: IOC):
sig = SignalR(await ioc.make_backend(int, "ticking"))
recv = []

async def watch():
async for val in observe_value(sig):
time.sleep(0.15)
recv.append(val)

start = time.time()
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(watch(), timeout=0.2)
assert time.time() - start == pytest.approx(0.3, abs=0.05)
assert len(recv) == 2
# Don't check values as CA and PVA have different algorithms for
# dropping updates for slow callbacks

0 comments on commit 559dcc5

Please sign in to comment.