Skip to content

Commit

Permalink
fixed persistent tasks (#730)
Browse files Browse the repository at this point in the history
* fixed persistent tasks

* fixed persistent tango callback task

* skipped tango tests for windows

* tweaked approx on motor tests

Windows is running slow.
  • Loading branch information
evalott100 authored Jan 14, 2025
1 parent 16b794a commit 4658c48
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/ophyd_async/epics/adaravis/_aravis_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def prepare(self, trigger_info: TriggerInfo):
else:
image_mode = adcore.ImageMode.MULTIPLE
if (exposure := trigger_info.livetime) is not None:
asyncio.gather(self.driver.acquire_time.set(exposure))
await self.driver.acquire_time.set(exposure)

trigger_mode, trigger_source = self._get_trigger_info(trigger_info.trigger)
# trigger mode must be set first and on it's own!
Expand Down
14 changes: 12 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def _error_and_kill_pending_tasks(
task
for task in asyncio.all_tasks(loop)
if (coro := task.get_coro()) is not None
and hasattr(coro, "__name__")
and coro.__name__ not in _ALLOWED_PYTEST_TASKS
and not task.done()
}
Expand All @@ -112,7 +113,7 @@ def _error_and_kill_pending_tasks(


@pytest.fixture(autouse=True, scope="function")
def fail_test_on_unclosed_tasks(request: FixtureRequest):
async def fail_test_on_unclosed_tasks(request: FixtureRequest):
"""
Used on every test to ensure failure if there are pending tasks
by the end of the test.
Expand Down Expand Up @@ -276,7 +277,7 @@ async def sim_detector(request: FixtureRequest):


def pytest_collection_modifyitems(config, items):
tango_dir = "tests/tango"
tango_dir = os.path.join("tests", "tango")

for item in items:
if tango_dir in str(item.fspath):
Expand All @@ -286,5 +287,14 @@ def pytest_collection_modifyitems(config, items):
reason="Tango is currently not supported on Python 3.12: https://github.com/bluesky/ophyd-async/issues/681"
)
)
elif "win" in sys.platform:
item.add_marker(
pytest.mark.skip(
reason=(
"Tango tests are currently not supported on Windows: "
"https://github.com/bluesky/ophyd-async/issues/733"
)
)
)
else:
item.add_marker(pytest.mark.forked)
34 changes: 21 additions & 13 deletions tests/epics/adaravis/test_aravis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import cast
from unittest.mock import AsyncMock, patch

import pytest

Expand Down Expand Up @@ -133,17 +134,24 @@ async def test_can_decribe_collect(


async def test_unsupported_trigger_excepts(test_adaravis: adaravis.AravisDetector):
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=r"AravisController only supports the following trigger types: .* but",
):
await test_adaravis.prepare(
TriggerInfo(
number_of_triggers=0,
trigger=DetectorTrigger.VARIABLE_GATE,
deadtime=1,
livetime=1,
frame_timeout=3,
with patch(
"ophyd_async.epics.adcore._hdf_writer.ADHDFWriter.open", new_callable=AsyncMock
) as mock_open:
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=(
"AravisController only supports the following trigger types: .* but"
),
):
await test_adaravis.prepare(
TriggerInfo(
number_of_triggers=0,
trigger=DetectorTrigger.VARIABLE_GATE,
deadtime=1,
livetime=1,
frame_timeout=3,
)
)
)

mock_open.assert_called_once()
31 changes: 18 additions & 13 deletions tests/epics/adpilatus/test_pilatus.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from collections.abc import Awaitable, Callable
from typing import cast
from unittest.mock import patch
from unittest.mock import AsyncMock, patch

import pytest

Expand Down Expand Up @@ -103,19 +103,24 @@ async def test_hints_from_hdf_writer(test_adpilatus: adpilatus.PilatusDetector):


async def test_unsupported_trigger_excepts(test_adpilatus: adpilatus.PilatusDetector):
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=r"PilatusController only supports the following trigger types: .* but",
):
await test_adpilatus.prepare(
TriggerInfo(
number_of_triggers=1,
trigger=DetectorTrigger.EDGE_TRIGGER,
deadtime=1.0,
livetime=1.0,
open = "ophyd_async.epics.adcore._hdf_writer.ADHDFWriter.open"
with patch(open, new_callable=AsyncMock) as mock_open:
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=(
"PilatusController only supports the following trigger types: .* but"
),
):
await test_adpilatus.prepare(
TriggerInfo(
number_of_triggers=1,
trigger=DetectorTrigger.EDGE_TRIGGER,
deadtime=1.0,
livetime=1.0,
)
)
)
mock_open.assert_called_once()


async def test_exposure_time_and_acquire_period_set(
Expand Down
4 changes: 2 additions & 2 deletions tests/epics/sim/test_epics_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async def test_mover_moving_well(mock_mover: sim.Mover) -> None:
target=0.55,
unit="mm",
precision=3,
time_elapsed=pytest.approx(0.0, abs=0.05),
time_elapsed=pytest.approx(0.0, abs=0.08),
)

await assert_value(mock_mover.setpoint, 0.55)
Expand All @@ -137,7 +137,7 @@ async def test_mover_moving_well(mock_mover: sim.Mover) -> None:
target=0.55,
unit="mm",
precision=3,
time_elapsed=pytest.approx(0.1, abs=0.05),
time_elapsed=pytest.approx(0.1, abs=0.08),
)
set_mock_value(mock_mover.readback, 0.5499999)
await wait_for_pending_wakeups()
Expand Down
2 changes: 1 addition & 1 deletion tests/epics/test_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def test_motor_moving_well(sim_motor: motor.Motor) -> None:
target=0.55,
unit="mm",
precision=3,
time_elapsed=pytest.approx(0.1, abs=0.05),
time_elapsed=pytest.approx(0.1, abs=0.08),
)
set_mock_value(sim_motor.motor_done_move, True)
set_mock_value(sim_motor.user_readback, 0.55)
Expand Down
2 changes: 1 addition & 1 deletion tests/sim/test_sim_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def test_negative_move():
"initial": 0.0,
"name": "M1",
"target": -0.19,
"time_elapsed": pytest.approx(0.19, abs=0.05),
"time_elapsed": pytest.approx(0.19, abs=0.08),
"unit": "mm",
},
]
Expand Down
4 changes: 4 additions & 0 deletions tests/tango/test_tango_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ def callback(reading):
await asyncio.sleep(0.5)
assert val == "new label"

assert attr_proxy._poll_task
attr_proxy.unsubscribe_callback()


# --------------------------------------------------------------------
@pytest.mark.asyncio
Expand All @@ -479,6 +482,7 @@ def callback(reading, value):
attr_proxy.subscribe_callback(callback)
await asyncio.sleep(0.2)
assert "Could not poll the attribute" in str(attr_proxy.exception)
attr_proxy.unsubscribe_callback()


# --------------------------------------------------------------------
Expand Down

0 comments on commit 4658c48

Please sign in to comment.