Skip to content

Commit 4c71140

Browse files
authored
Stop using a deprecated way to replace the event loop in tests (#976)
The new version of pytest-async doesn't allow overriding the event loop with a fixture as we used to, so we need to use the `event_loop_policy` fixture instead. Fixes #975.
2 parents bfc8de2 + f7e480e commit 4c71140

File tree

7 files changed

+33
-64
lines changed

7 files changed

+33
-64
lines changed

tests/actor/test_background_service.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Simple test for the BaseActor."""
55
import asyncio
6-
from collections.abc import Iterator
76
from typing import Literal, assert_never
87

98
import async_solipsism
@@ -12,13 +11,10 @@
1211
from frequenz.sdk.actor import BackgroundService
1312

1413

15-
# Setting 'autouse' has no effect as this method replaces the event loop for all tests in the file.
16-
@pytest.fixture()
17-
def event_loop() -> Iterator[async_solipsism.EventLoop]:
18-
"""Replace the loop with one that doesn't interact with the outside world."""
19-
loop = async_solipsism.EventLoop()
20-
yield loop
21-
loop.close()
14+
@pytest.fixture(autouse=True)
15+
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
16+
"""Return an event loop policy that uses the async solipsism event loop."""
17+
return async_solipsism.EventLoopPolicy()
2218

2319

2420
class FakeService(BackgroundService):

tests/actor/test_resampling.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""Frequenz Python SDK resampling example."""
55
import asyncio
66
import dataclasses
7-
from collections.abc import Iterator
87
from datetime import datetime, timedelta, timezone
98

109
import async_solipsism
@@ -22,17 +21,11 @@
2221
from frequenz.sdk.timeseries import Sample
2322
from frequenz.sdk.timeseries._quantities import Quantity
2423

25-
# pylint: disable=too-many-locals,redefined-outer-name
26-
#
2724

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

3730

3831
def _now() -> datetime:

tests/actor/test_run_utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import asyncio
77
import time
8-
from collections.abc import Iterator
98

109
import async_solipsism
1110
import pytest
@@ -14,13 +13,10 @@
1413
from frequenz.sdk.actor import Actor, run
1514

1615

17-
# Setting 'autouse' has no effect as this method replaces the event loop for all tests in the file.
18-
@pytest.fixture()
19-
def event_loop() -> Iterator[async_solipsism.EventLoop]:
20-
"""Replace the loop with one that doesn't interact with the outside world."""
21-
loop = async_solipsism.EventLoop()
22-
yield loop
23-
loop.close()
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()
2420

2521

2622
class FaultyActor(Actor):

tests/microgrid/test_datapipeline.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""Basic tests for the DataPipeline."""
55

66
import asyncio
7-
from collections.abc import Iterator
87
from datetime import timedelta
98

109
import async_solipsism
@@ -24,12 +23,10 @@
2423
from ..utils.mock_microgrid_client import MockMicrogridClient
2524

2625

27-
@pytest.fixture
28-
def event_loop() -> Iterator[async_solipsism.EventLoop]:
29-
"""Replace the loop with one that doesn't interact with the outside world."""
30-
loop = async_solipsism.EventLoop()
31-
yield loop
32-
loop.close()
26+
@pytest.fixture(autouse=True)
27+
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
28+
"""Return an event loop policy that uses the async solipsism event loop."""
29+
return async_solipsism.EventLoopPolicy()
3330

3431

3532
# loop time is advanced but not the system time

tests/timeseries/_battery_pool/test_battery_pool.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import dataclasses
1111
import logging
1212
import math
13-
from collections.abc import AsyncIterator, Iterator
13+
from collections.abc import AsyncIterator
1414
from dataclasses import dataclass, is_dataclass, replace
1515
from datetime import datetime, timedelta, timezone
1616
from typing import Any, Generic, TypeVar
@@ -58,18 +58,11 @@
5858

5959
_logger = logging.getLogger(__name__)
6060

61-
# pylint doesn't understand fixtures. It thinks it is redefined name.
62-
# pylint: disable=redefined-outer-name
6361

64-
# pylint: disable=too-many-lines
65-
66-
67-
@pytest.fixture()
68-
def event_loop() -> Iterator[async_solipsism.EventLoop]:
69-
"""Replace the loop with one that doesn't interact with the outside world."""
70-
loop = async_solipsism.EventLoop()
71-
yield loop
72-
loop.close()
62+
@pytest.fixture(autouse=True)
63+
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
64+
"""Return an event loop policy that uses the async solipsism event loop."""
65+
return async_solipsism.EventLoopPolicy()
7366

7467

7568
def get_components(

tests/timeseries/test_moving_window.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""Tests for the moving window."""
55

66
import asyncio
7-
from collections.abc import Iterator, Sequence
7+
from collections.abc import Sequence
88
from datetime import datetime, timedelta, timezone
99

1010
import async_solipsism
@@ -19,13 +19,10 @@
1919
from frequenz.sdk.timeseries._resampling import ResamplerConfig
2020

2121

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

3027

3128
async def push_logical_meter_data(

tests/timeseries/test_resampling.py

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

77
import asyncio
88
import logging
9-
from collections.abc import AsyncIterator, Iterator
9+
from collections.abc import AsyncIterator
1010
from datetime import datetime, timedelta, timezone
1111
from unittest.mock import AsyncMock, MagicMock
1212

@@ -33,17 +33,14 @@
3333

3434
from ..utils import a_sequence
3535

36-
# We relax some pylint checks as for tests they don't make a lot of sense.
37-
# pylint: disable=too-many-lines,disable=too-many-locals,redefined-outer-name
36+
# We relax some pylint checks as for tests they don't make a lot of sense for this test.
37+
# pylint: disable=too-many-lines,disable=too-many-locals
3838

3939

40-
# Setting 'autouse' has no effect as this method replaces the event loop for all tests in the file.
41-
@pytest.fixture()
42-
def event_loop() -> Iterator[async_solipsism.EventLoop]:
43-
"""Replace the loop with one that doesn't interact with the outside world."""
44-
loop = async_solipsism.EventLoop()
45-
yield loop
46-
loop.close()
40+
@pytest.fixture(autouse=True)
41+
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
42+
"""Return an event loop policy that uses the async solipsism event loop."""
43+
return async_solipsism.EventLoopPolicy()
4744

4845

4946
@pytest.fixture

0 commit comments

Comments
 (0)