From f67ca132f32c48f7dbc34781ac51f35ac2d9cf46 Mon Sep 17 00:00:00 2001 From: "Mathias L. Baumann" Date: Thu, 30 Mar 2023 14:43:33 +0200 Subject: [PATCH] Update channels dependency to v0.14.0 Signed-off-by: Mathias L. Baumann --- RELEASE_NOTES.md | 1 + examples/resampling.py | 2 +- pyproject.toml | 2 +- src/frequenz/sdk/actor/_resampling.py | 3 +- .../power_distributing/power_distributing.py | 4 +-- tests/actor/test_battery_pool_status.py | 18 ++++------ tests/actor/test_battery_status.py | 14 ++++---- tests/actor/test_power_distributing.py | 4 +-- tests/timeseries/test_formula_engine.py | 36 ++++++++----------- tests/timeseries/test_resampling.py | 5 +-- tests/utils/mock_microgrid.py | 26 +++++++------- 11 files changed, 53 insertions(+), 62 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index f698c4ca3..29c2bec69 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -7,6 +7,7 @@ ## Upgrading * Update BatteryStatus to mark battery with unknown capacity as not working (#263) +* The channels dependency was updated to v0.14.0 (#292) ## New Features diff --git a/examples/resampling.py b/examples/resampling.py index abf02679d..15edc45f3 100644 --- a/examples/resampling.py +++ b/examples/resampling.py @@ -113,7 +113,7 @@ async def run() -> None: # pylint: disable=too-many-locals # Needed until channels Senders raises exceptions on errors async def sink_adapter(sample: Sample) -> None: - assert await average_sender.send(sample) + await average_sender.send(sample) print("Starting...") diff --git a/pyproject.toml b/pyproject.toml index 6f85c87d2..b333d9cb8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,7 +26,7 @@ classifiers = [ requires-python = ">= 3.8, < 4" dependencies = [ "frequenz-api-microgrid >= 0.11.0, < 0.12.0", - "frequenz-channels >= 0.13.0, < 0.14.0", + "frequenz-channels >= 0.14.0, < 0.15.0", "google-api-python-client >= 2.71, < 3", "grpcio >= 1.51.1, < 2", "grpcio-tools >= 1.51.1, < 2", diff --git a/src/frequenz/sdk/actor/_resampling.py b/src/frequenz/sdk/actor/_resampling.py index 1fbadae1e..935fcb52e 100644 --- a/src/frequenz/sdk/actor/_resampling.py +++ b/src/frequenz/sdk/actor/_resampling.py @@ -81,8 +81,7 @@ async def _subscribe(self, request: ComponentMetricRequest) -> None: sender = self._channel_registry.new_sender(request.get_channel_name()) async def sink_adapter(sample: Sample) -> None: - if not await sender.send(sample): - raise RuntimeError(f"Error while sending with sender {sender}", sender) + await sender.send(sample) self._resampler.add_timeseries(request_channel_name, receiver, sink_adapter) diff --git a/src/frequenz/sdk/actor/power_distributing/power_distributing.py b/src/frequenz/sdk/actor/power_distributing/power_distributing.py index 446aa55cb..e870eb3b2 100644 --- a/src/frequenz/sdk/actor/power_distributing/power_distributing.py +++ b/src/frequenz/sdk/actor/power_distributing/power_distributing.py @@ -397,7 +397,7 @@ def _check_request(self, request: Request) -> Optional[Result]: def _remove_duplicated_requests( self, request: Request, user: _User - ) -> List[asyncio.Task[bool]]: + ) -> List[asyncio.Task[None]]: """Remove duplicated requests from the queue. Remove old requests in which set of batteries are the same as in new request. @@ -414,7 +414,7 @@ def _remove_duplicated_requests( batteries = request.batteries good_requests: List[Tuple[Request, _User]] = [] - to_ignore: List[asyncio.Task[bool]] = [] + to_ignore: List[asyncio.Task[None]] = [] while not self._request_queue.empty(): prev_request, prev_user = self._request_queue.get_nowait() diff --git a/tests/actor/test_battery_pool_status.py b/tests/actor/test_battery_pool_status.py index c35e55883..4da9e488c 100644 --- a/tests/actor/test_battery_pool_status.py +++ b/tests/actor/test_battery_pool_status.py @@ -67,28 +67,22 @@ async def test_batteries_status(self, mock_microgrid: MockMicrogridClient) -> No batteries_list = list(batteries) - assert await mock_microgrid.send(battery_data(component_id=batteries_list[0])) + await mock_microgrid.send(battery_data(component_id=batteries_list[0])) await asyncio.sleep(0.1) assert batteries_status.get_working_batteries(batteries) == expected_working expected_working.add(batteries_list[0]) - assert await mock_microgrid.send( - inverter_data(component_id=batteries_list[0] - 1) - ) + await mock_microgrid.send(inverter_data(component_id=batteries_list[0] - 1)) await asyncio.sleep(0.1) assert batteries_status.get_working_batteries(batteries) == expected_working msg = await asyncio.wait_for(battery_status_recv.receive(), timeout=0.2) assert msg == batteries_status._current_status - assert await mock_microgrid.send( - inverter_data(component_id=batteries_list[1] - 1) - ) - assert await mock_microgrid.send(battery_data(component_id=batteries_list[1])) + await mock_microgrid.send(inverter_data(component_id=batteries_list[1] - 1)) + await mock_microgrid.send(battery_data(component_id=batteries_list[1])) - assert await mock_microgrid.send( - inverter_data(component_id=batteries_list[2] - 1) - ) - assert await mock_microgrid.send(battery_data(component_id=batteries_list[2])) + await mock_microgrid.send(inverter_data(component_id=batteries_list[2] - 1)) + await mock_microgrid.send(battery_data(component_id=batteries_list[2])) expected_working = set(batteries_list) await asyncio.sleep(0.1) diff --git a/tests/actor/test_battery_status.py b/tests/actor/test_battery_status.py index 056fe2ad3..a43f19550 100644 --- a/tests/actor/test_battery_status.py +++ b/tests/actor/test_battery_status.py @@ -665,12 +665,12 @@ async def test_async_battery_status( await asyncio.sleep(0.01) with time_machine.travel("2022-01-01 00:00 UTC", tick=False) as time: - assert await mock_microgrid.send(inverter_data(component_id=INVERTER_ID)) - assert await mock_microgrid.send(battery_data(component_id=BATTERY_ID)) + await mock_microgrid.send(inverter_data(component_id=INVERTER_ID)) + await mock_microgrid.send(battery_data(component_id=BATTERY_ID)) status = await asyncio.wait_for(status_receiver.receive(), timeout=0.1) assert status is Status.WORKING - assert await set_power_result_sender.send( + await set_power_result_sender.send( SetPowerResult(succeed={}, failed={BATTERY_ID}) ) status = await asyncio.wait_for(status_receiver.receive(), timeout=0.1) @@ -678,11 +678,11 @@ async def test_async_battery_status( time.shift(2) - assert await mock_microgrid.send(battery_data(component_id=BATTERY_ID)) + await mock_microgrid.send(battery_data(component_id=BATTERY_ID)) status = await asyncio.wait_for(status_receiver.receive(), timeout=0.1) assert status is Status.WORKING - assert await mock_microgrid.send( + await mock_microgrid.send( inverter_data( component_id=INVERTER_ID, timestamp=datetime.now(tz=timezone.utc) - timedelta(seconds=7), @@ -691,13 +691,13 @@ async def test_async_battery_status( status = await asyncio.wait_for(status_receiver.receive(), timeout=0.1) assert status is Status.NOT_WORKING - assert await set_power_result_sender.send( + await set_power_result_sender.send( SetPowerResult(succeed={}, failed={BATTERY_ID}) ) await asyncio.sleep(0.3) assert len(status_receiver) == 0 - assert await mock_microgrid.send(inverter_data(component_id=INVERTER_ID)) + await mock_microgrid.send(inverter_data(component_id=INVERTER_ID)) status = await asyncio.wait_for(status_receiver.receive(), timeout=0.1) assert status is Status.WORKING diff --git a/tests/actor/test_power_distributing.py b/tests/actor/test_power_distributing.py index ffb924c82..2b12e9fd8 100644 --- a/tests/actor/test_power_distributing.py +++ b/tests/actor/test_power_distributing.py @@ -119,7 +119,7 @@ async def init_mock_microgrid(self, mocker: MockerFixture) -> MockMicrogridClien graph = microgrid.component_graph for battery in graph.components(component_category={ComponentCategory.BATTERY}): - assert await microgrid.send( + await microgrid.send( battery_msg( battery.component_id, capacity=Metric(98000), @@ -130,7 +130,7 @@ async def init_mock_microgrid(self, mocker: MockerFixture) -> MockMicrogridClien inverters = graph.components(component_category={ComponentCategory.INVERTER}) for inverter in inverters: - assert await microgrid.send( + await microgrid.send( inverter_msg( inverter.component_id, power=Bound(-500, 500), diff --git a/tests/timeseries/test_formula_engine.py b/tests/timeseries/test_formula_engine.py index 747295d78..1d14084f0 100644 --- a/tests/timeseries/test_formula_engine.py +++ b/tests/timeseries/test_formula_engine.py @@ -80,13 +80,11 @@ async def run_test( tests_passed = 0 for io_pair in io_pairs: io_input, io_output = io_pair - assert all( - await asyncio.gather( - *[ - chan.new_sender().send(Sample(now, value)) - for chan, value in zip(channels.values(), io_input) - ] - ) + await asyncio.gather( + *[ + chan.new_sender().send(Sample(now, value)) + for chan, value in zip(channels.values(), io_input) + ] ) next_val = ( await engine._evaluator.apply() # pylint: disable=protected-access @@ -339,13 +337,11 @@ async def run_test( # pylint: disable=too-many-locals tests_passed = 0 for io_pair in io_pairs: io_input, io_output = io_pair - assert all( - await asyncio.gather( - *[ - chan.new_sender().send(Sample(now, value)) - for chan, value in zip(channels, io_input) - ] - ) + await asyncio.gather( + *[ + chan.new_sender().send(Sample(now, value)) + for chan, value in zip(channels, io_input) + ] ) next_val = await result_chan.receive() assert next_val.value == io_output @@ -576,13 +572,11 @@ async def run_test( tests_passed = 0 for io_pair in io_pairs: io_input, io_output = io_pair - assert all( - await asyncio.gather( - *[ - chan.new_sender().send(Sample(now, value)) - for chan, value in zip(channels.values(), io_input) - ] - ) + await asyncio.gather( + *[ + chan.new_sender().send(Sample(now, value)) + for chan, value in zip(channels.values(), io_input) + ] ) next_val = ( await engine._evaluator.apply() # pylint: disable=protected-access diff --git a/tests/timeseries/test_resampling.py b/tests/timeseries/test_resampling.py index 26f7b4abd..e6c4884a8 100644 --- a/tests/timeseries/test_resampling.py +++ b/tests/timeseries/test_resampling.py @@ -15,7 +15,7 @@ import async_solipsism import pytest import time_machine -from frequenz.channels import Broadcast +from frequenz.channels import Broadcast, SenderError from frequenz.sdk.timeseries import Sample from frequenz.sdk.timeseries._resampling import ( @@ -901,7 +901,8 @@ async def test_receiving_stopped_resampling_error( # Close channel, try to resample again await source_chan.close() - assert await source_sendr.send(sample0s) is False + with pytest.raises(SenderError): + await source_sendr.send(sample0s) fake_time.shift(resampling_period_s) with pytest.raises(ResamplingError) as excinfo: await resampler.resample(one_shot=True) diff --git a/tests/utils/mock_microgrid.py b/tests/utils/mock_microgrid.py index 2b5ddbb64..286a04317 100644 --- a/tests/utils/mock_microgrid.py +++ b/tests/utils/mock_microgrid.py @@ -112,7 +112,7 @@ def component_graph(self) -> ComponentGraph: """ return self._component_graph - async def send(self, data: ComponentData) -> bool: + async def send(self, data: ComponentData) -> None: """Send component data using channel. This simulates component sending data. Right now only battery and inverter @@ -121,20 +121,22 @@ async def send(self, data: ComponentData) -> bool: Args: data: Data to be send - Returns: - Whether data was send, or not. + Raises: + SenderError: if the underlying channel was closed. + A [ChannelClosedError][frequenz.channels.ChannelClosedError] is + set as the cause. """ cid = data.component_id if isinstance(data, BatteryData): - return await self._battery_data_senders[cid].send(data) - if isinstance(data, InverterData): - return await self._inverter_data_senders[cid].send(data) - if isinstance(data, MeterData): - return await self._meter_data_senders[cid].send(data) - if isinstance(data, EVChargerData): - return await self._ev_charger_data_senders[cid].send(data) - - raise RuntimeError(f"{type(data)} is not supported in MockMicrogridClient.") + await self._battery_data_senders[cid].send(data) + elif isinstance(data, InverterData): + await self._inverter_data_senders[cid].send(data) + elif isinstance(data, MeterData): + await self._meter_data_senders[cid].send(data) + elif isinstance(data, EVChargerData): + await self._ev_charger_data_senders[cid].send(data) + else: + raise RuntimeError(f"{type(data)} is not supported in MockMicrogridClient.") async def close_channel(self, cid: int) -> None: """Close channel for given component id.