Skip to content

Commit 031c2fe

Browse files
committed
Add FakeClient option to use randomized dispatch list
Some tests require extra checks to not rely on the order. Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent a70fec3 commit 031c2fe

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/frequenz/client/dispatch/test/_service.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import dataclasses
99
from dataclasses import dataclass, replace
1010
from datetime import datetime, timezone
11+
from random import shuffle
1112

1213
import grpc
1314
import grpc.aio
@@ -45,6 +46,9 @@ class FakeService:
4546
_last_id: int = 0
4647
"""Last used dispatch id."""
4748

49+
_shuffle_after_create: bool = False
50+
"""Whether to shuffle the dispatches after creating them."""
51+
4852
# pylint: disable=invalid-name
4953
async def ListMicrogridDispatches(
5054
self, request: PBDispatchListRequest
@@ -116,6 +120,9 @@ async def CreateMicrogridDispatch(
116120
update_time=datetime.now(tz=timezone.utc),
117121
)
118122
)
123+
if self._shuffle_after_create:
124+
shuffle(self.dispatches)
125+
119126
return Empty()
120127

121128
async def UpdateMicrogridDispatch(

src/frequenz/client/dispatch/test/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ class FakeClient(Client):
1717
This client uses a fake service to simulate the dispatch api.
1818
"""
1919

20-
def __init__(self) -> None:
21-
"""Initialize the mock client."""
20+
def __init__(self, shuffle_after_create: bool = False) -> None:
21+
"""Initialize the mock client.
22+
23+
Args:
24+
shuffle_after_create: Whether to shuffle the dispatches after creating them.
25+
"""
2226
super().__init__(MagicMock(), "mock")
2327
self._stub = FakeService() # type: ignore
28+
self._service._shuffle_after_create = shuffle_after_create
2429

2530
@property
2631
def dispatches(self) -> list[Dispatch]:

0 commit comments

Comments
 (0)