Skip to content

Commit 2f62a10

Browse files
authored
Fix some problems that related to flakey tests (#124)
2 parents 4aae6d3 + a1bfc11 commit 2f62a10

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
## Bug Fixes
44

5-
* Fix missing dependency in last release
5+
* Fix missing dependency in last release.
6+
* The `FakeClient.set_dispatches()` method now correctly updates `FakeService._last_id` which is used to generate unique dispatch IDs.
7+
* Fix that streams were globally shared between all clients.
8+

src/frequenz/client/dispatch/_client.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
class Client(BaseApiClient[dispatch_pb2_grpc.MicrogridDispatchServiceStub]):
5656
"""Dispatch API client."""
5757

58-
streams: dict[
59-
int, GrpcStreamBroadcaster[StreamMicrogridDispatchesResponse, DispatchEvent]
60-
] = {}
61-
"""A dictionary of streamers, keyed by microgrid_id."""
62-
6358
def __init__(
6459
self,
6560
*,
@@ -91,6 +86,10 @@ def __init__(
9186
),
9287
)
9388
self._metadata = (("key", key),)
89+
self._streams: dict[
90+
int, GrpcStreamBroadcaster[StreamMicrogridDispatchesResponse, DispatchEvent]
91+
] = {}
92+
"""A dictionary of streamers, keyed by microgrid_id."""
9493

9594
@property
9695
def stub(self) -> dispatch_pb2_grpc.MicrogridDispatchServiceAsyncStub:
@@ -225,11 +224,11 @@ def _get_stream(
225224
self, microgrid_id: int
226225
) -> GrpcStreamBroadcaster[StreamMicrogridDispatchesResponse, DispatchEvent]:
227226
"""Get an instance to the streaming helper."""
228-
broadcaster = self.streams.get(microgrid_id)
227+
broadcaster = self._streams.get(microgrid_id)
229228
# pylint: disable=protected-access
230229
if broadcaster is not None and broadcaster._channel.is_closed:
231230
# pylint: enable=protected-access
232-
del self.streams[microgrid_id]
231+
del self._streams[microgrid_id]
233232
broadcaster = None
234233
if broadcaster is None:
235234
request = StreamMicrogridDispatchesRequest(microgrid_id=microgrid_id)
@@ -244,7 +243,7 @@ def _get_stream(
244243
transform=DispatchEvent.from_protobuf,
245244
retry_strategy=LinearBackoff(interval=1, limit=0),
246245
)
247-
self.streams[microgrid_id] = broadcaster
246+
self._streams[microgrid_id] = broadcaster
248247

249248
return broadcaster
250249

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def set_dispatches(self, microgrid_id: int, value: list[Dispatch]) -> None:
5454
"""
5555
self._service.dispatches[microgrid_id] = value
5656

57+
if len(value) == 0:
58+
return
59+
60+
# Max between last id and the max id in the list
61+
# pylint: disable=protected-access
62+
self._service._last_id = max(
63+
self._service._last_id, max(dispatch.id for dispatch in value)
64+
)
65+
# pylint: enable=protected-access
66+
5767
@property
5868
def _service(self) -> FakeService:
5969
"""The fake service.

0 commit comments

Comments
 (0)