Skip to content

Commit 18bb37d

Browse files
committed
Remove deprecated uses of pytest-asyncio
The `event_loop` fixture is deprecated and `event_loop_policy` should be used instead. The option `asyncio_default_fixture_loop_scope = "function"` is also added to `pyproject.toml`, as it is also deprecated to rely on a default. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 1a91c6a commit 18bb37d

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ disable = [
148148
[tool.pytest.ini_options]
149149
testpaths = ["tests", "src"]
150150
asyncio_mode = "auto"
151+
asyncio_default_fixture_loop_scope = "function"
151152
required_plugins = ["pytest-asyncio", "pytest-mock"]
152153
markers = [
153154
"integration: integration tests (deselect with '-m \"not integration\"')",

tests/test_timer.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import asyncio
88
import enum
9-
from collections.abc import Iterator
109
from datetime import timedelta
1110

1211
import async_solipsism
@@ -22,13 +21,10 @@
2221
)
2322

2423

25-
# Setting 'autouse' has no effect as this method replaces the event loop for all tests in the file.
26-
@pytest.fixture()
27-
def event_loop() -> Iterator[async_solipsism.EventLoop]:
28-
"""Replace the loop with one that doesn't interact with the outside world."""
29-
loop = async_solipsism.EventLoop()
30-
yield loop
31-
loop.close()
24+
@pytest.fixture(autouse=True)
25+
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
26+
"""Return an event loop policy that uses the async solipsism event loop."""
27+
return async_solipsism.EventLoopPolicy()
3228

3329

3430
# We give some extra room (dividing by 10) to the max and min to avoid flaky errors
@@ -297,10 +293,10 @@ async def test_timer_construction_wrong_args() -> None:
297293
)
298294

299295

300-
async def test_timer_autostart(
301-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
302-
) -> None:
296+
async def test_timer_autostart() -> None:
303297
"""Test the autostart of a periodic timer."""
298+
event_loop = asyncio.get_running_loop()
299+
304300
timer = Timer(timedelta(seconds=1.0), TriggerAllMissed())
305301

306302
# We sleep some time, less than the interval, and then receive from the
@@ -312,10 +308,10 @@ async def test_timer_autostart(
312308
assert event_loop.time() == pytest.approx(1.0)
313309

314310

315-
async def test_timer_autostart_with_delay(
316-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
317-
) -> None:
311+
async def test_timer_autostart_with_delay() -> None:
318312
"""Test the autostart of a periodic timer with a delay."""
313+
event_loop = asyncio.get_running_loop()
314+
319315
timer = Timer(
320316
timedelta(seconds=1.0), TriggerAllMissed(), start_delay=timedelta(seconds=0.5)
321317
)
@@ -344,9 +340,10 @@ class _StartMethod(enum.Enum):
344340
@pytest.mark.parametrize("start_method", list(_StartMethod))
345341
async def test_timer_no_autostart(
346342
start_method: _StartMethod,
347-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
348343
) -> None:
349344
"""Test a periodic timer when it is not automatically started."""
345+
event_loop = asyncio.get_running_loop()
346+
350347
timer = Timer(
351348
timedelta(seconds=1.0),
352349
TriggerAllMissed(),
@@ -377,10 +374,10 @@ async def test_timer_no_autostart(
377374
assert event_loop.time() == pytest.approx(1.5)
378375

379376

380-
async def test_timer_trigger_all_missed(
381-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
382-
) -> None:
377+
async def test_timer_trigger_all_missed() -> None:
383378
"""Test a timer using the TriggerAllMissed policy."""
379+
event_loop = asyncio.get_running_loop()
380+
384381
interval = 1.0
385382
timer = Timer(timedelta(seconds=interval), TriggerAllMissed())
386383

@@ -438,10 +435,10 @@ async def test_timer_trigger_all_missed(
438435
assert drift == pytest.approx(timedelta(seconds=0.0))
439436

440437

441-
async def test_timer_skip_missed_and_resync(
442-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
443-
) -> None:
438+
async def test_timer_skip_missed_and_resync() -> None:
444439
"""Test a timer using the SkipMissedAndResync policy."""
440+
event_loop = asyncio.get_running_loop()
441+
445442
interval = 1.0
446443
timer = Timer(timedelta(seconds=interval), SkipMissedAndResync())
447444

@@ -489,10 +486,10 @@ async def test_timer_skip_missed_and_resync(
489486
assert drift == pytest.approx(timedelta(seconds=0.0))
490487

491488

492-
async def test_timer_skip_missed_and_drift(
493-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
494-
) -> None:
489+
async def test_timer_skip_missed_and_drift() -> None:
495490
"""Test a timer using the SkipMissedAndDrift policy."""
491+
event_loop = asyncio.get_running_loop()
492+
496493
interval = 1.0
497494
tolerance = 0.1
498495
timer = Timer(
@@ -553,10 +550,10 @@ async def test_timer_skip_missed_and_drift(
553550
assert drift == pytest.approx(timedelta(seconds=0.0))
554551

555552

556-
async def test_timer_reset_with_new_interval(
557-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
558-
) -> None:
553+
async def test_timer_reset_with_new_interval() -> None:
559554
"""Test resetting the timer with a new interval."""
555+
event_loop = asyncio.get_running_loop()
556+
560557
initial_interval = timedelta(seconds=1.0)
561558
new_interval = timedelta(seconds=2.0)
562559
timer = Timer(initial_interval, TriggerAllMissed())

0 commit comments

Comments
 (0)