Skip to content

Commit

Permalink
Allow passing the mocker in the constructor
Browse files Browse the repository at this point in the history
This is to be able to call `start()` without any arguments, so we can
make the `MockMicrogrid` an async context manager, to make sure it is
properly finalized after it is used.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
  • Loading branch information
llucax committed Jan 4, 2024
1 parent 69e93b7 commit 3d9936f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
72 changes: 46 additions & 26 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,68 @@
# The basic syntax is [label]: [path patterns].
#
# For more details on the configuration please see:
- changed-files:
- any-glob-to-any-file:
# https://github.com/marketplace/actions/labeler

"part:actor":
- "src/frequenz/sdk/actor/**"
- changed-files:
- any-glob-to-any-file:
- "src/frequenz/sdk/actor/**"

"part:config":
- "src/frequenz/sdk/config/**"
- changed-files:
- any-glob-to-any-file:
- "src/frequenz/sdk/config/**"

"part:core":
- "src/frequenz/sdk/_internal/**"
- changed-files:
- any-glob-to-any-file:
- "src/frequenz/sdk/_internal/**"

"part:data-pipeline":
- "src/frequenz/sdk/_data_handling/**"
- "src/frequenz/sdk/_data_ingestion/**"
- "src/frequenz/sdk/timeseries/**"
- changed-files:
- any-glob-to-any-file:
- "src/frequenz/sdk/_data_handling/**"
- "src/frequenz/sdk/_data_ingestion/**"
- "src/frequenz/sdk/timeseries/**"

"part:docs":
- "**/*.md"
- "docs/**"
- "examples/**"
- LICENSE
- changed-files:
- any-glob-to-any-file:
- "**/*.md"
- "docs/**"
- "examples/**"
- LICENSE

"part:microgrid":
- "src/frequenz/sdk/microgrid/**"
- changed-files:
- any-glob-to-any-file:
- "src/frequenz/sdk/microgrid/**"

"part:power-distribution":
- "src/frequenz/sdk/power/**"
- changed-files:
- any-glob-to-any-file:
- "src/frequenz/sdk/power/**"

"part:tests":
- "**/conftest.py"
- "tests/**"
- changed-files:
- any-glob-to-any-file:
- "**/conftest.py"
- "tests/**"

"part:tooling":
- "**/*.ini"
- "**/*.toml"
- "**/*.yaml"
- "**/*.yml"
- "**/conftest.py"
- ".editorconfig"
- ".git*"
- ".git*/**"
- "docs/*.py"
- CODEOWNERS
- MANIFEST.in
- noxfile.py
- changed-files:
- any-glob-to-any-file:
- "**/*.ini"
- "**/*.toml"
- "**/*.yaml"
- "**/*.yml"
- "**/conftest.py"
- ".editorconfig"
- ".git*"
- ".git*/**"
- "docs/*.py"
- CODEOWNERS
- MANIFEST.in
- noxfile.py
20 changes: 18 additions & 2 deletions tests/timeseries/mock_microgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__( # pylint: disable=too-many-arguments
num_namespaces: int = 1,
fuse: Fuse | None = Fuse(Current.from_amperes(10_000.0)),
graph: _MicrogridComponentGraph | None = None,
mocker: MockerFixture | None = None,
):
"""Create a new instance.
Expand All @@ -78,10 +79,12 @@ def __init__( # pylint: disable=too-many-arguments
fuse: optional, the fuse to use for the grid connection.
graph: optional, a graph of components to use instead of the default grid
layout. If specified, grid_meter must be None.
mocker: optional, a mocker to pass to the mock client and mock resampler.
Raises:
ValueError: if both grid_meter and graph are specified.
"""
self.mocker = mocker
if grid_meter is not None and graph is not None:
raise ValueError("grid_meter and graph are mutually exclusive")

Expand Down Expand Up @@ -163,9 +166,22 @@ def inverters(comp_type: InverterType) -> list[int]:
self.meter_ids.append(self._grid_meter_id)
self._start_meter_streaming(self._grid_meter_id)

async def start(self, mocker: MockerFixture) -> None:
async def start(self, mocker: MockerFixture | None = None) -> None:
"""Init the mock microgrid client and start the mock resampler."""
self.init_mock_client(lambda mock_client: mock_client.initialize(mocker))
# Return if it is already started
if hasattr(self, "mock_client") or hasattr(self, "mock_resampler"):
return

if mocker is None:
mocker = self.mocker
assert mocker is not None, "A mocker must be set at init or start time"

# This binding to a local is needed because Python uses late binding for
# closures and `mocker` could be bound to `None` again after the lambda is
# created. See:
# https://mypy.readthedocs.io/en/stable/common_issues.html#narrowing-and-inner-functions
local_mocker = mocker
self.init_mock_client(lambda mock_client: mock_client.initialize(local_mocker))
self.mock_resampler = MockResampler(
mocker,
ResamplerConfig(timedelta(seconds=self._sample_rate_s)),
Expand Down

0 comments on commit 3d9936f

Please sign in to comment.