Skip to content

Commit

Permalink
Try wait_for_wakeups
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Willemsen committed Nov 1, 2024
1 parent 636fae4 commit ec082ae
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions tests/epics/test_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
)
from ophyd_async.epics import motor

# Long enough for multiple asyncio event loop cycles to run so
# all the tasks have a chance to run
A_BIT = 0.001

async def wait_for_wakeups(max_yields=10):
loop = asyncio.get_event_loop()
# If anything has called loop.call_soon or is scheduled a wakeup
# then let it run
for _ in range(max_yields):
if loop._scheduled:
await asyncio.sleep(0)
else:
return
raise RuntimeError(f"Tasks still scheduling wakeups after {max_yields} yields")


@pytest.fixture
Expand All @@ -37,7 +45,7 @@ async def sim_motor():
async def wait_for_eq(item, attribute, comparison, timeout):
timeout_time = time.monotonic() + timeout
while getattr(item, attribute) != comparison:
await asyncio.sleep(A_BIT)
await wait_for_wakeups()
if time.monotonic() > timeout_time:
raise TimeoutError

Expand All @@ -49,6 +57,7 @@ async def test_motor_moving_well(sim_motor: motor.Motor) -> None:
s.watch(watcher)
done = Mock()
s.add_callback(done)
await wait_for_wakeups()
await wait_for_eq(watcher, "call_count", 1, 1)
assert watcher.call_args == call(
name="sim_motor",
Expand Down Expand Up @@ -78,7 +87,7 @@ async def test_motor_moving_well(sim_motor: motor.Motor) -> None:
set_mock_value(sim_motor.motor_done_move, True)
set_mock_value(sim_motor.user_readback, 0.55)
set_mock_put_proceeds(sim_motor.user_setpoint, True)
await asyncio.sleep(A_BIT)
await wait_for_wakeups()
await wait_for_eq(s, "done", True, 1)
done.assert_called_once_with(s)

Expand All @@ -90,7 +99,7 @@ async def test_motor_moving_well_2(sim_motor: motor.Motor) -> None:
s.watch(watcher)
done = Mock()
s.add_callback(done)
await asyncio.sleep(0.1)
await wait_for_wakeups()
assert watcher.call_count == 1
assert watcher.call_args == call(
name="sim_motor",
Expand Down Expand Up @@ -118,7 +127,7 @@ async def test_motor_moving_well_2(sim_motor: motor.Motor) -> None:
time_elapsed=pytest.approx(0.1, abs=0.2),
)
set_mock_put_proceeds(sim_motor.user_setpoint, True)
await asyncio.sleep(0.1)
await wait_for_wakeups()
assert s.done
done.assert_called_once_with(s)

Expand Down Expand Up @@ -157,7 +166,7 @@ async def test_motor_moving_stopped(sim_motor: motor.Motor):
assert not s.done
await sim_motor.stop()
set_mock_put_proceeds(sim_motor.user_setpoint, True)
await asyncio.sleep(0.1)
await wait_for_wakeups()
assert s.done
assert s.success is False

Expand Down

0 comments on commit ec082ae

Please sign in to comment.