Skip to content

Commit 078600d

Browse files
committed
Upgrade to pytest-asyncio 1.4.0 and use event loop factory hook
See NGC-1985.
1 parent 0821352 commit 078600d

7 files changed

Lines changed: 19 additions & 21 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ repos:
5252
'prometheus-client==0.24.0',
5353
'pyparsing==3.3.1',
5454
'pytest==9.0.3',
55+
'pytest-asyncio==1.4.0',
5556
'redis==7.1.0', # Indirect dependency of katsdptelstate
5657
'spead2==4.4.1',
5758
'types-decorator==5.2.0.20251101',

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ test = [
6767
"katsdpsigproc[CUDA]",
6868
"matplotlib",
6969
"pytest>=8",
70-
"pytest-asyncio>=0.24",
70+
"pytest-asyncio>=1.4.0",
7171
"pytest-check>=1.3,<2.2.3", # Upper bound due to https://github.com/okken/pytest-check/issues/173
7272
"pytest-custom_exit_code",
7373
"pytest-mock",
@@ -136,7 +136,7 @@ pytester_example_dir = "test/pytest_plugins/demo"
136136
# and only the top-level conftest.py is allowed to load plugins. See
137137
# test/pytest_plugins/demo/README.md for more info.
138138
addopts = "--cov-context=test --cov-report html --import-mode=prepend --ignore=test/pytest_plugins/demo"
139-
markers = ["mask_timestamp", "use_vkgdr", "cmdline_args", "spectra_per_heap", "slow"]
139+
markers = ["mask_timestamp", "use_vkgdr", "cmdline_args", "spectra_per_heap", "slow", "use_async_solipsism"]
140140
asyncio_mode = "auto"
141141
asyncio_default_fixture_loop_scope = "function"
142142

qualification/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pytest==9.0.3
322322
# pytest-check
323323
# pytest-custom-exit-code
324324
# pytest-reportlog
325-
pytest-asyncio==1.3.0
325+
pytest-asyncio==1.4.0
326326
# via
327327
# -c qualification/../requirements-dev.txt
328328
# katgpucbf (pyproject.toml)

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pytest==9.0.3
344344
# pytest-mock
345345
# pytest-reportlog
346346
# pytest-xdist
347-
pytest-asyncio==1.3.0
347+
pytest-asyncio==1.4.0
348348
# via katgpucbf (pyproject.toml)
349349
pytest-check==2.2.2
350350
# via katgpucbf (pyproject.toml)

test/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def my_test(a, b):
5050
from typing import Any
5151

5252
import aiokatcp
53+
import async_solipsism
5354
import katsdpsigproc.cuda
5455
import pycuda.driver
5556
import pytest
@@ -84,6 +85,7 @@ class _CombinationsCandidate:
8485
by_name: dict[str, Any] # Lookup by argument name (for filtering)
8586

8687

88+
@pytest.hookimpl
8789
def pytest_generate_tests(metafunc) -> None:
8890
"""Apply "combinations" marker."""
8991
all_combinations = metafunc.config.option.all_combinations
@@ -144,6 +146,15 @@ def pytest_generate_tests(metafunc) -> None:
144146
metafunc.parametrize(names, [combo.values for combo in combos])
145147

146148

149+
@pytest.hookimpl
150+
def pytest_asyncio_loop_factories(config: pytest.Config, item: pytest.Item) -> dict[str, Callable]:
151+
"""Override the event loop factory for tests with a ``use_async_solipsism`` mark."""
152+
if item.get_closest_marker("use_async_solipsism") is not None:
153+
return {"async_solipsism": async_solipsism.EventLoop}
154+
else:
155+
return {"stdlib": asyncio.new_event_loop}
156+
157+
147158
@pytest.fixture
148159
def mock_send_stream_network() -> IPv4Network:
149160
"""Network mask to filter the queues returned by :func:`mock_send_stream`.

test/test_utils.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from unittest import mock
2626

2727
import aiokatcp
28-
import async_solipsism
2928
import pytest
3029
from aiokatcp import DeviceStatus
3130

@@ -71,14 +70,10 @@ async def discarding_iterator(counter: AsyncIterable[int]) -> SpyDiscardingItera
7170
return SpyDiscardingIterator(counter)
7271

7372

73+
@pytest.mark.use_async_solipsism
7474
class TestDiscardingIterator:
7575
"""Test :class:`.DiscardingIterator`."""
7676

77-
@pytest.fixture
78-
def event_loop_policy(self) -> async_solipsism.EventLoopPolicy:
79-
"""Use async_solipsism event loop."""
80-
return async_solipsism.EventLoopPolicy()
81-
8277
async def test_simple(self, discarding_iterator: SpyDiscardingIterator) -> None:
8378
"""Test basic operations."""
8479
await asyncio.sleep(2.5)
@@ -228,14 +223,10 @@ def test_status_change(self, sensors: aiokatcp.SensorSet, mock_time: mock.Mock)
228223
assert ds.reading == aiokatcp.Reading(3456789012.0, aiokatcp.Sensor.Status.NOMINAL, DeviceStatus.OK)
229224

230225

226+
@pytest.mark.use_async_solipsism
231227
class TestTimeoutSensorStatus:
232228
"""Tests for :func:`katgpucbf.utils.timeout_sensor_status`."""
233229

234-
@pytest.fixture
235-
def event_loop_policy(self) -> async_solipsism.EventLoopPolicy:
236-
"""Use async_solipsism event loop."""
237-
return async_solipsism.EventLoopPolicy()
238-
239230
@pytest.fixture
240231
def sensor(self) -> aiokatcp.Sensor:
241232
"""Create a dummy sensor."""

test/vgpu/test_send.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@
2626
from typing import override
2727
from unittest import mock
2828

29-
import async_solipsism
3029
import numpy as np
3130
import pytest
3231
import pytest_mock
3332

3433
from katgpucbf.vgpu.main import VTP_DEFAULT_PORT
3534
from katgpucbf.vgpu.send import RateLimiter, VDIFFrame, VDIFSender
3635

37-
38-
@pytest.fixture
39-
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
40-
"""Use async_solipsism event loop."""
41-
return async_solipsism.EventLoopPolicy()
36+
pytestmark = pytest.mark.use_async_solipsism
4237

4338

4439
@dataclass(frozen=True)

0 commit comments

Comments
 (0)