Skip to content

Commit 1992372

Browse files
committed
Add Dispatcher.client property for easy client access
When you want to manipulate dispatches, you will need direct access to the client. Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent b77e8d9 commit 1992372

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
## New Features
1212

1313
* Introduced new class `Dispatch` (based on the client class) that contains useful functions and extended information about the received dispatch.
14+
* `Dispatcher.client` was added to provide an easy access to the client for updating, deleting and creating dispatches
1415

1516
## Bug Fixes
1617

src/frequenz/dispatch/_dispatcher.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import grpc.aio
1010
from frequenz.channels import Broadcast, Receiver
11+
from frequenz.client.dispatch import Client
1112

1213
from ._dispatch import Dispatch
1314
from ._event import DispatchEvent
@@ -123,10 +124,10 @@ def __init__(
123124
self._lifecycle_events_channel = Broadcast[DispatchEvent](
124125
name="lifecycle_events"
125126
)
127+
self._client = Client(grpc_channel, svc_addr)
126128
self._actor = DispatchingActor(
127129
microgrid_id,
128-
grpc_channel,
129-
svc_addr,
130+
self._client,
130131
self._lifecycle_events_channel.new_sender(),
131132
self._running_state_channel.new_sender(),
132133
)
@@ -135,6 +136,11 @@ async def start(self) -> None:
135136
"""Start the actor."""
136137
self._actor.start()
137138

139+
@property
140+
def client(self) -> Client:
141+
"""Return the client."""
142+
return self._client
143+
138144
@property
139145
def lifecycle_events(self) -> ReceiverFetcher[DispatchEvent]:
140146
"""Return new, updated or deleted dispatches receiver fetcher.

src/frequenz/dispatch/actor.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class DispatchingActor(Actor):
4747
def __init__(
4848
self,
4949
microgrid_id: int,
50-
grpc_channel: grpc.aio.Channel,
51-
svc_addr: str,
50+
client: Client,
5251
lifecycle_updates_sender: Sender[DispatchEvent],
5352
running_state_change_sender: Sender[Dispatch],
5453
poll_interval: timedelta = _DEFAULT_POLL_INTERVAL,
@@ -57,15 +56,14 @@ def __init__(
5756
5857
Args:
5958
microgrid_id: The microgrid ID to handle dispatches for.
60-
grpc_channel: The gRPC channel to use for communication with the API.
61-
svc_addr: Address of the service to connect to.
59+
client: The client to use for fetching dispatches.
6260
lifecycle_updates_sender: A sender for dispatch lifecycle events.
6361
running_state_change_sender: A sender for dispatch running state changes.
6462
poll_interval: The interval to poll the API for dispatche changes.
6563
"""
6664
super().__init__(name="dispatch")
6765

68-
self._client = Client(grpc_channel, svc_addr)
66+
self._client = client
6967
self._dispatches: dict[int, Dispatch] = {}
7068
self._scheduled: dict[int, asyncio.Task[None]] = {}
7169
self._microgrid_id = microgrid_id

tests/test_frequenz_dispatch.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from datetime import datetime, timedelta, timezone
99
from random import randint
1010
from typing import AsyncIterator, Iterator
11-
from unittest.mock import MagicMock
1211

1312
import async_solipsism
1413
import time_machine
@@ -66,18 +65,15 @@ async def actor_env() -> AsyncIterator[ActorTestEnv]:
6665
lifecycle_updates_dispatches = Broadcast[DispatchEvent](name="lifecycle_updates")
6766
running_state_change_dispatches = Broadcast[Dispatch](name="running_state_change")
6867
microgrid_id = randint(1, 100)
68+
client = FakeClient()
6969

7070
actor = DispatchingActor(
7171
microgrid_id=microgrid_id,
72-
grpc_channel=MagicMock(),
73-
svc_addr="localhost",
7472
lifecycle_updates_sender=lifecycle_updates_dispatches.new_sender(),
7573
running_state_change_sender=running_state_change_dispatches.new_sender(),
74+
client=client,
7675
)
7776

78-
client = FakeClient()
79-
actor._client = client # pylint: disable=protected-access
80-
8177
actor.start()
8278

8379
yield ActorTestEnv(

0 commit comments

Comments
 (0)