Skip to content

Commit 3cf99d6

Browse files
committed
Fix a few tests warnings
Fix some deprecation warnings while overriding the event loop and for `test_timer_intergration` we also use async-solipsism properly, before we were not replacing the loop policy appropriately. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent f688d1b commit 3cf99d6

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

tests/test_select_integration.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class at a time.
99
"""
1010

1111
import asyncio
12-
from collections.abc import AsyncIterator, Iterator
12+
from collections.abc import AsyncIterator
1313
from typing import Any
1414

1515
import async_solipsism
@@ -33,17 +33,11 @@ class TestSelect:
3333
recv1: Event
3434
recv2: Event
3535
recv3: Event
36-
loop: async_solipsism.EventLoop
3736

3837
@pytest.fixture(autouse=True)
39-
def event_loop(
40-
self, request: pytest.FixtureRequest
41-
) -> Iterator[async_solipsism.EventLoop]:
42-
"""Replace the loop with one that doesn't interact with the outside world."""
43-
loop = async_solipsism.EventLoop()
44-
request.cls.loop = loop
45-
yield loop
46-
loop.close()
38+
def event_loop_policy(self) -> async_solipsism.EventLoopPolicy:
39+
"""Return an event loop policy that uses the async solipsism event loop."""
40+
return async_solipsism.EventLoopPolicy()
4741

4842
@pytest.fixture()
4943
async def start_run_ordered_sequence(self) -> AsyncIterator[asyncio.Task[None]]:
@@ -92,10 +86,16 @@ def assert_received_from(
9286
assert selected.exception is None
9387
assert not selected.was_stopped
9488
if expected_pending_tasks > 0:
95-
assert len(asyncio.all_tasks(self.loop)) == expected_pending_tasks
89+
assert (
90+
len(asyncio.all_tasks(asyncio.get_event_loop()))
91+
== expected_pending_tasks
92+
)
9693
elif expected_pending_tasks < 0:
97-
assert len(asyncio.all_tasks(self.loop)) > expected_pending_tasks
98-
assert self.loop.time() == at_time
94+
assert (
95+
len(asyncio.all_tasks(asyncio.get_event_loop()))
96+
> expected_pending_tasks
97+
)
98+
assert asyncio.get_event_loop().time() == at_time
9999

100100
def assert_receiver_stopped(
101101
self,
@@ -125,10 +125,16 @@ def assert_receiver_stopped(
125125
assert isinstance(selected.exception, ReceiverStoppedError)
126126
assert selected.exception.receiver is receiver
127127
if expected_pending_tasks > 0:
128-
assert len(asyncio.all_tasks(self.loop)) == expected_pending_tasks
128+
assert (
129+
len(asyncio.all_tasks(asyncio.get_event_loop()))
130+
== expected_pending_tasks
131+
)
129132
elif expected_pending_tasks < 0:
130-
assert len(asyncio.all_tasks(self.loop)) > expected_pending_tasks
131-
assert self.loop.time() == at_time
133+
assert (
134+
len(asyncio.all_tasks(asyncio.get_event_loop()))
135+
> expected_pending_tasks
136+
)
137+
assert asyncio.get_event_loop().time() == at_time
132138

133139
# We use the loop time (and the sleeps in the run_ordered_sequence method) mainly to
134140
# ensure we are processing the events in the correct order and we are really
@@ -362,11 +368,11 @@ async def test_multiple_ready(
362368
Also test that the loop waits forever if there are no more receivers ready.
363369
"""
364370
received: set[str] = set()
365-
last_time: float = self.loop.time()
371+
last_time: float = asyncio.get_event_loop().time()
366372
try:
367373
async with asyncio.timeout(15):
368374
async for selected in select(self.recv1, self.recv2, self.recv3):
369-
now = self.loop.time()
375+
now = asyncio.get_event_loop().time()
370376
if now != last_time: # Only check when there was a jump in time
371377
match now:
372378
case 1:
@@ -401,7 +407,7 @@ async def test_multiple_ready(
401407
else:
402408
assert False, "Should not reach this point"
403409
except asyncio.TimeoutError:
404-
assert self.loop.time() == 15
410+
assert asyncio.get_event_loop().time() == 15
405411
# This happened after time == 3, but the loop never resumes because
406412
# there is nothing ready, so we need to check it after the timeout.
407413
assert received == {

tests/test_timer.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,14 @@ async def test_timer_construction_defaults() -> None:
252252
def test_timer_construction_no_async() -> None:
253253
"""Test the construction outside of async (using a custom loop)."""
254254
loop = async_solipsism.EventLoop()
255-
timer = Timer(timedelta(seconds=1.0), TriggerAllMissed(), loop=loop)
256-
assert timer.interval == timedelta(seconds=1.0)
257-
assert isinstance(timer.missed_tick_policy, TriggerAllMissed)
258-
assert timer.loop is loop
259-
assert timer.is_running is True
255+
try:
256+
timer = Timer(timedelta(seconds=1.0), TriggerAllMissed(), loop=loop)
257+
assert timer.interval == timedelta(seconds=1.0)
258+
assert isinstance(timer.missed_tick_policy, TriggerAllMissed)
259+
assert timer.loop is loop
260+
assert timer.is_running is True
261+
finally:
262+
loop.close()
260263

261264

262265
def test_timer_construction_no_event_loop() -> None:

tests/test_timer_integration.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
from frequenz.channels.timer import SkipMissedAndDrift, Timer
1414

1515

16+
@pytest.fixture(autouse=True)
17+
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
18+
"""Return an event loop policy that uses the async solipsism event loop."""
19+
return async_solipsism.EventLoopPolicy()
20+
21+
1622
@pytest.mark.integration
17-
async def test_timer_timeout_reset(
18-
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
19-
) -> None:
23+
@pytest.mark.asyncio(loop_scope="function")
24+
async def test_timer_timeout_reset() -> None:
2025
"""Test that the receiving is properly adjusted after a reset."""
26+
event_loop = asyncio.get_running_loop()
2127

2228
async def timer_wait(timer: Timer) -> None:
2329
await timer.receive()

0 commit comments

Comments
 (0)