Skip to content

Commit 838e37b

Browse files
Stop PowerDistributingActor at the end of each test
This removes some warnings about tasks that is pending when loop was closed. Signed-off-by: ela-kotulska-frequenz <[email protected]>
1 parent 3cc663c commit 838e37b

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/frequenz/sdk/actor/power_distributing/power_distributing.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module
3434

3535
from ... import microgrid
36+
from ..._internal.asyncio import cancel_and_await
3637
from ...actor._decorator import actor
3738
from ...microgrid import ComponentGraph
3839
from ...microgrid.client import MicrogridApiClient
@@ -186,13 +187,21 @@ def __init__(
186187
self._users_channels: Dict[
187188
str, Bidirectional.Handle[Result, Request]
188189
] = users_channels
189-
self._create_users_tasks()
190+
self._users_tasks = self._create_users_tasks()
190191
self._started = asyncio.Event()
191192

192-
def _create_users_tasks(self) -> None:
193-
"""For each user create a task to wait for request."""
193+
def _create_users_tasks(self) -> List[asyncio.Task[Empty]]:
194+
"""For each user create a task to wait for request.
195+
196+
Returns:
197+
List with users tasks.
198+
"""
199+
tasks = []
194200
for user, handler in self._users_channels.items():
195-
asyncio.create_task(self._wait_for_request(_User(user, handler)))
201+
tasks.append(
202+
asyncio.create_task(self._wait_for_request(_User(user, handler)))
203+
)
204+
return tasks
196205

197206
def _get_upper_bound(self, batteries: Set[int]) -> int:
198207
"""Get total upper bound of power to be set for given batteries.
@@ -634,3 +643,8 @@ async def _cancel_tasks(self, tasks: Iterable[asyncio.Task[Any]]) -> None:
634643
aws.cancel()
635644

636645
await asyncio.gather(*tasks, return_exceptions=True)
646+
647+
async def _stop_actor(self) -> None:
648+
"""Stop all running async tasks."""
649+
await asyncio.gather(*[cancel_and_await(t) for t in self._users_tasks])
650+
await self._stop() # type: ignore # pylint: disable=no-member

tests/actor/test_power_distributing.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def test_constructor(self) -> None:
9494

9595
assert distributor._bat_inv_map == {106: 105, 206: 205, 306: 305}
9696
assert distributor._inv_bat_map == {105: 106, 205: 206, 305: 306}
97-
await distributor._stop() # type: ignore # pylint: disable=no-member
97+
await distributor._stop_actor()
9898

9999
async def init_mock_microgrid(self) -> MockMicrogridClient:
100100
"""Create mock microgrid and send initial data from the components.
@@ -158,7 +158,7 @@ async def test_power_distributor_one_user(self, mocker: MockerFixture) -> None:
158158
[asyncio.create_task(client_handle.receive())],
159159
timeout=SAFETY_TIMEOUT,
160160
)
161-
await distributor._stop() # type: ignore # pylint: disable=no-member
161+
await distributor._stop_actor()
162162

163163
assert len(pending) == 0
164164
assert len(done) == 1
@@ -215,7 +215,7 @@ async def test_power_distributor_two_users(self, mocker: MockerFixture) -> None:
215215
],
216216
timeout=SAFETY_TIMEOUT,
217217
)
218-
await distributor._stop() # type: ignore # pylint: disable=no-member
218+
await distributor._stop_actor()
219219

220220
assert len(pending) == 0
221221
assert len(done) == 2
@@ -257,7 +257,7 @@ async def test_power_distributor_invalid_battery_id(
257257
[asyncio.create_task(user1_handle.receive())],
258258
timeout=SAFETY_TIMEOUT,
259259
)
260-
await distributor._stop() # type: ignore # pylint: disable=no-member
260+
await distributor._stop_actor()
261261

262262
assert len(done) == 1
263263
result: Result = done.pop().result()
@@ -328,7 +328,7 @@ async def test_power_distributor_overlapping_batteries(
328328
],
329329
timeout=SAFETY_TIMEOUT,
330330
)
331-
await distributor._stop() # type: ignore # pylint: disable=no-member
331+
await distributor._stop_actor()
332332

333333
assert len(done) == 3
334334
success, ignored = 0, 0
@@ -382,7 +382,7 @@ async def test_power_distributor_one_user_adjust_power_consume(
382382
[asyncio.create_task(user1_handle.receive())],
383383
timeout=SAFETY_TIMEOUT,
384384
)
385-
await distributor._stop() # type: ignore # pylint: disable=no-member
385+
await distributor._stop_actor()
386386

387387
assert len(pending) == 0
388388
assert len(done) == 1
@@ -430,7 +430,7 @@ async def test_power_distributor_one_user_adjust_power_supply(
430430
[asyncio.create_task(user1_handle.receive())],
431431
timeout=SAFETY_TIMEOUT,
432432
)
433-
await distributor._stop() # type: ignore # pylint: disable=no-member
433+
await distributor._stop_actor()
434434

435435
assert len(pending) == 0
436436
assert len(done) == 1
@@ -478,7 +478,7 @@ async def test_power_distributor_one_user_adjust_power_success(
478478
[asyncio.create_task(user1_handle.receive())],
479479
timeout=SAFETY_TIMEOUT,
480480
)
481-
await distributor._stop() # type: ignore # pylint: disable=no-member
481+
await distributor._stop_actor()
482482

483483
assert len(pending) == 0
484484
assert len(done) == 1
@@ -520,7 +520,7 @@ async def test_not_all_batteries_are_working(self, mocker: MockerFixture) -> Non
520520
[asyncio.create_task(client_handle.receive())],
521521
timeout=SAFETY_TIMEOUT,
522522
)
523-
await distributor._stop() # type: ignore # pylint: disable=no-member
523+
await distributor._stop_actor()
524524

525525
assert len(pending) == 0
526526
assert len(done) == 1

0 commit comments

Comments
 (0)