From b3450d1867804afb31b9856c756803101892645b Mon Sep 17 00:00:00 2001 From: Eva Lott Date: Mon, 13 Jan 2025 13:54:47 +0000 Subject: [PATCH] fixed persistent tasks --- .../epics/adaravis/_aravis_controller.py | 2 +- tests/conftest.py | 3 +- tests/epics/adaravis/test_aravis.py | 34 ++++++++++++------- tests/epics/adpilatus/test_pilatus.py | 31 ++++++++++------- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/ophyd_async/epics/adaravis/_aravis_controller.py b/src/ophyd_async/epics/adaravis/_aravis_controller.py index e78fdf21aa..b42463a9a3 100644 --- a/src/ophyd_async/epics/adaravis/_aravis_controller.py +++ b/src/ophyd_async/epics/adaravis/_aravis_controller.py @@ -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! diff --git a/tests/conftest.py b/tests/conftest.py index 69d4cfdf4f..bd90848e06 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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() } @@ -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. diff --git a/tests/epics/adaravis/test_aravis.py b/tests/epics/adaravis/test_aravis.py index 6382306d23..fd83f5d45e 100644 --- a/tests/epics/adaravis/test_aravis.py +++ b/tests/epics/adaravis/test_aravis.py @@ -1,4 +1,5 @@ from typing import cast +from unittest.mock import AsyncMock, patch import pytest @@ -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() diff --git a/tests/epics/adpilatus/test_pilatus.py b/tests/epics/adpilatus/test_pilatus.py index e5c8613e03..a1f3bbc5ef 100644 --- a/tests/epics/adpilatus/test_pilatus.py +++ b/tests/epics/adpilatus/test_pilatus.py @@ -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 @@ -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(