Skip to content

Commit e3ccfec

Browse files
committed
Change stream_* methods to sync
They just start a background task. There's no async until we're actually waiting for messages. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent ce39eb9 commit e3ccfec

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/frequenz/client/electricity_trading/_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def stub(self) -> electricity_trading_pb2_grpc.ElectricityTradingServiceAsyncStu
218218
# type-checker, so it can only be used for type hints.
219219
return self._stub # type: ignore
220220

221-
async def stream_gridpool_orders(
221+
def stream_gridpool_orders(
222222
# pylint: disable=too-many-arguments, too-many-positional-arguments
223223
self,
224224
gridpool_id: int,
@@ -285,7 +285,7 @@ async def stream_gridpool_orders(
285285
warn_on_overflow=warn_on_overflow, maxsize=max_size
286286
)
287287

288-
async def stream_gridpool_trades(
288+
def stream_gridpool_trades(
289289
# pylint: disable=too-many-arguments, too-many-positional-arguments
290290
self,
291291
gridpool_id: int,
@@ -351,7 +351,7 @@ async def stream_gridpool_trades(
351351
warn_on_overflow=warn_on_overflow, maxsize=max_size
352352
)
353353

354-
async def stream_public_trades(
354+
def stream_public_trades(
355355
# pylint: disable=too-many-arguments, too-many-positional-arguments
356356
self,
357357
states: list[TradeState] | None = None,

src/frequenz/client/electricity_trading/cli/etrading.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async def list_public_trades(url: str, key: str, *, delivery_start: datetime) ->
7373
if delivery_start <= datetime.now(timezone.utc):
7474
return
7575

76-
stream = await client.stream_public_trades(delivery_period=delivery_period)
76+
stream = client.stream_public_trades(delivery_period=delivery_period)
7777
async for trade in stream:
7878
print_public_trade(trade)
7979

@@ -111,7 +111,7 @@ async def list_gridpool_trades(
111111
if delivery_start and delivery_start <= datetime.now(timezone.utc):
112112
return
113113

114-
stream = await client.stream_gridpool_trades(gid, delivery_period=delivery_period)
114+
stream = client.stream_gridpool_trades(gid, delivery_period=delivery_period)
115115
async for trade in stream:
116116
print_trade(trade)
117117

@@ -154,7 +154,7 @@ async def list_gridpool_orders(
154154
if delivery_start and delivery_start <= datetime.now(timezone.utc):
155155
return
156156

157-
stream = await client.stream_gridpool_orders(gid, delivery_period=delivery_period)
157+
stream = client.stream_gridpool_orders(gid, delivery_period=delivery_period)
158158
async for order in stream:
159159
print_order(order)
160160

tests/test_client.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,23 @@ def set_up_order_detail_response(
131131
).to_pb()
132132

133133

134-
def test_stream_gridpool_orders(set_up: SetupParams) -> None:
134+
async def test_stream_gridpool_orders(set_up: SetupParams) -> None:
135135
"""Test the method streaming gridpool orders."""
136-
set_up.loop.run_until_complete(
137-
set_up.client.stream_gridpool_orders(set_up.gridpool_id)
138-
)
136+
set_up.client.stream_gridpool_orders(set_up.gridpool_id)
137+
await asyncio.sleep(0)
139138

140139
set_up.mock_stub.ReceiveGridpoolOrdersStream.assert_called_once()
141140
args, _ = set_up.mock_stub.ReceiveGridpoolOrdersStream.call_args
142141
assert args[0].gridpool_id == set_up.gridpool_id
143142

144143

145-
def test_stream_gridpool_orders_with_optional_inputs(set_up: SetupParams) -> None:
144+
async def test_stream_gridpool_orders_with_optional_inputs(set_up: SetupParams) -> None:
146145
"""Test the method streaming gridpool orders with some fields to filter for."""
147146
# Fields to filter for
148147
order_states = [OrderState.ACTIVE]
149148

150-
set_up.loop.run_until_complete(
151-
set_up.client.stream_gridpool_orders(
152-
set_up.gridpool_id, order_states=order_states
153-
)
154-
)
149+
set_up.client.stream_gridpool_orders(set_up.gridpool_id, order_states=order_states)
150+
await asyncio.sleep(0)
155151

156152
set_up.mock_stub.ReceiveGridpoolOrdersStream.assert_called_once()
157153
args, _ = set_up.mock_stub.ReceiveGridpoolOrdersStream.call_args
@@ -161,32 +157,30 @@ def test_stream_gridpool_orders_with_optional_inputs(set_up: SetupParams) -> Non
161157
]
162158

163159

164-
def test_stream_gridpool_trades(
160+
async def test_stream_gridpool_trades(
165161
set_up: SetupParams,
166162
) -> None:
167163
"""Test the method streaming gridpool trades."""
168-
set_up.loop.run_until_complete(
169-
set_up.client.stream_gridpool_trades(
170-
gridpool_id=set_up.gridpool_id, market_side=set_up.side
171-
)
164+
set_up.client.stream_gridpool_trades(
165+
gridpool_id=set_up.gridpool_id, market_side=set_up.side
172166
)
167+
await asyncio.sleep(0)
173168

174169
set_up.mock_stub.ReceiveGridpoolTradesStream.assert_called_once()
175170
args, _ = set_up.mock_stub.ReceiveGridpoolTradesStream.call_args
176171
assert args[0].gridpool_id == set_up.gridpool_id
177172
assert args[0].filter.side == set_up.side.to_pb()
178173

179174

180-
def test_stream_public_trades(
175+
async def test_stream_public_trades(
181176
set_up: SetupParams,
182177
) -> None:
183178
"""Test the method streaming public trades."""
184179
# Fields to filter for
185180
trade_states = [TradeState.ACTIVE]
186181

187-
set_up.loop.run_until_complete(
188-
set_up.client.stream_public_trades(states=trade_states)
189-
)
182+
set_up.client.stream_public_trades(states=trade_states)
183+
await asyncio.sleep(0)
190184

191185
set_up.mock_stub.ReceivePublicTradesStream.assert_called_once()
192186
args, _ = set_up.mock_stub.ReceivePublicTradesStream.call_args

0 commit comments

Comments
 (0)