diff --git a/services/dy-static-file-server/src/dy_static_file_server/inputs_to_outputs.py b/services/dy-static-file-server/src/dy_static_file_server/inputs_to_outputs.py index b2e3948d..e6b59934 100644 --- a/services/dy-static-file-server/src/dy_static_file_server/inputs_to_outputs.py +++ b/services/dy-static-file-server/src/dy_static_file_server/inputs_to_outputs.py @@ -77,30 +77,34 @@ def remap_input_to_output(input_dir: Path, output_dir: Path) -> None: class PortsMonitor: def __init__( - self, input_dir: Path, output_dir: Path, *, monitor_interval: float = DEFAULT_MONITOR_WAIT_INTERVAL + self, + input_dir: Path, + output_dir: Path, + *, + monitor_interval: float = DEFAULT_MONITOR_WAIT_INTERVAL, ) -> None: self.input_dir: Path = input_dir self.output_dir: Path = output_dir - self.paths: set[Path] = {input_dir, output_dir} + self.to_observe: set[Path] = {input_dir} self.monitor_interval: float = monitor_interval self._monitor_task: asyncio.Task | None = None self._keep_running: bool = False - def _get_state(self) -> Dict[Path, Dict[Path, Tuple[str, float]]]: - """return aggravated state for all monitored paths""" - return {p: _get_directory_state(p) for p in self.paths} + def _get_observed_state(self) -> Dict[Path, Dict[Path, Tuple[str, float]]]: + """return aggravated state for all observed paths""" + return {p: _get_directory_state(p) for p in self.to_observe} async def _monitor(self) -> None: _logger.info("Started monitor") - previous_state = self._get_state() + previous_state = self._get_observed_state() while self._keep_running: await asyncio.sleep(self.monitor_interval) _logger.info("Checking") - current_state = self._get_state() + current_state = self._get_observed_state() if previous_state != current_state: _logger.info("Change detected!") diff --git a/services/dy-static-file-server/tests/unit/test_dy_static_file_server_inputs_to_outputs.py b/services/dy-static-file-server/tests/unit/test_dy_static_file_server_inputs_to_outputs.py index 81ea6e24..2318e00b 100644 --- a/services/dy-static-file-server/tests/unit/test_dy_static_file_server_inputs_to_outputs.py +++ b/services/dy-static-file-server/tests/unit/test_dy_static_file_server_inputs_to_outputs.py @@ -209,6 +209,19 @@ async def test_folder_mirror( await ports_monitor.stop() +async def _assert_on_change_completed( + caplog: pytest.LogCaptureFixture, *, expected_count: int +): + async for attempt in AsyncRetrying( + wait=wait_fixed(0.1), + stop=stop_after_delay(2), + reraise=True, + retry=retry_if_exception_type(AssertionError), + ): + with attempt: + assert caplog.text.count("on_change completed") == 1 + + @pytest.mark.asyncio async def test_folder_mirror_main( caplog: pytest.LogCaptureFixture, @@ -233,13 +246,12 @@ async def test_folder_mirror_main( create_files_in_input(input_dir) - async for attempt in AsyncRetrying( - wait=wait_fixed(0.1), - stop=stop_after_delay(10), - reraise=True, - retry=retry_if_exception_type(AssertionError), - ): - with attempt: - assert "on_change completed" in caplog.text + # wait a bit to trigger the check a few times + await asyncio.sleep(1) + await _assert_on_change_completed(caplog, expected_count=1) + + # touch file in inputs await for another event + (input_dir / "file_input" / "test_file").touch() + await _assert_on_change_completed(caplog, expected_count=2) await ports_monitor.stop()