Skip to content

Commit 10e3986

Browse files
authored
Fix stream() not working with base-client 0.11 (#180)
2 parents b04df36 + 605757e commit 10e3986

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/frequenz/client/dispatch/_client.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,7 @@ def _get_stream(
242242
) -> GrpcStreamBroadcaster[StreamMicrogridDispatchesResponse, DispatchEvent]:
243243
"""Get an instance to the streaming helper."""
244244
broadcaster = self._streams.get(microgrid_id)
245-
# pylint: disable=protected-access
246-
if broadcaster is not None and broadcaster._channel.is_closed:
247-
# pylint: enable=protected-access
245+
if broadcaster is not None and not broadcaster.is_running:
248246
del self._streams[microgrid_id]
249247
broadcaster = None
250248
if broadcaster is None:

tests/test_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from functools import partial
1111

1212
import grpc
13+
import pytest
1314
from pytest import raises
1415

1516
from frequenz.client.dispatch.test.client import FakeClient, to_create_params
@@ -302,13 +303,20 @@ async def test_delete_dispatch_fail(client: FakeClient) -> None:
302303
await client.delete(microgrid_id=1, dispatch_id=1)
303304

304305

305-
async def test_dispatch_stream(client: FakeClient, sample: Dispatch) -> None:
306+
@pytest.mark.parametrize("call_twice", [True, False])
307+
async def test_dispatch_stream(
308+
client: FakeClient, sample: Dispatch, call_twice: bool
309+
) -> None:
306310
"""Test dispatching a stream of dispatches."""
307311
microgrid_id = random.randint(1, 100)
308312
dispatches = [sample, sample, sample]
309313

310314
stream = client.stream(microgrid_id)
311315

316+
if call_twice:
317+
# Call function again to test behavior if stream already exists
318+
_ = client.stream(microgrid_id)
319+
312320
async def expect(dispatch: Dispatch, event: Event) -> None:
313321
message = await stream.receive()
314322
assert message.dispatch == dispatch

0 commit comments

Comments
 (0)