Skip to content

Commit d5db4e0

Browse files
Fix ConfigManagingActor to handle relative paths (#1005)
The check that compared the event path with the config path was not working as expected when the config path was a relative path.
2 parents 97da4db + 1722a9b commit d5db4e0

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
- Fixed a bug where sending tasks in the data sourcing actor might have not been properly awaited.
3232
- Updated the logical meter documentation to reflect the latest changes.
3333
- Fixed a bug in the code examples in the getting-started tutorial.
34+
- Fixed a bug in `ConfigManagingActor` that was not properly comparing the event path to the config file path when the config file is a relative path.

src/frequenz/sdk/actor/_config_managing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async def _run(self) -> None:
9393
async for event in self._file_watcher:
9494
# Since we are watching the whole parent directory, we need to make sure
9595
# we only react to events related to the configuration file.
96-
if event.path != self._config_path:
96+
if not event.path.samefile(self._config_path):
9797
continue
9898

9999
match event.type:

tests/actor/test_config_manager.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33

44
"""Test for ConfigManager."""
5+
import os
56
import pathlib
67
from typing import Any
78

@@ -48,17 +49,6 @@ class TestActorConfigManager:
4849

4950
@pytest.fixture()
5051
def config_file(self, tmp_path: pathlib.Path) -> pathlib.Path:
51-
"""Create a test config file."""
52-
file_path = tmp_path / TestActorConfigManager.conf_path
53-
file_path.parent.mkdir()
54-
file_path.touch()
55-
file_path.write_text(TestActorConfigManager.conf_content)
56-
return file_path
57-
58-
@pytest.fixture()
59-
def real_config_file(
60-
self, tmp_path: pathlib.Path = pathlib.Path("/tmp/")
61-
) -> pathlib.Path:
6252
"""Create a test config file."""
6353
file_path = tmp_path / TestActorConfigManager.conf_path
6454
if not file_path.exists():
@@ -98,3 +88,26 @@ async def test_update(self, config_file: pathlib.Path) -> None:
9888
assert config.get("var2") == str(number)
9989
assert config.get("var3") is None
10090
assert config_file.read_text() == create_content(number=number)
91+
92+
async def test_update_relative_path(self, config_file: pathlib.Path) -> None:
93+
"""Test ConfigManagingActor with a relative path."""
94+
config_channel: Broadcast[dict[str, Any]] = Broadcast(
95+
name="Config Channel", resend_latest=True
96+
)
97+
config_receiver = config_channel.new_receiver()
98+
99+
current_dir = pathlib.Path.cwd()
100+
relative_path = os.path.relpath(config_file, current_dir)
101+
102+
async with ConfigManagingActor(relative_path, config_channel.new_sender()):
103+
config = await config_receiver.receive()
104+
assert config is not None
105+
assert config.get("var2") is None
106+
107+
number = 8
108+
config_file.write_text(create_content(number=number))
109+
110+
config = await config_receiver.receive()
111+
assert config is not None
112+
assert config.get("var2") == str(number)
113+
assert config_file.read_text() == create_content(number=number)

0 commit comments

Comments
 (0)