Skip to content

Commit

Permalink
fixed tests and observer
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Neagu committed Nov 15, 2024
1 parent 06895c1 commit 5e65e8a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()

0 comments on commit 5e65e8a

Please sign in to comment.