Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file watcher integration tests #326

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions tests/test_file_watcher_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest

from frequenz.channels import ReceiverStoppedError, select, selected_from
from frequenz.channels.file_watcher import Event, EventType, FileWatcher
from frequenz.channels.file_watcher import EventType, FileWatcher
from frequenz.channels.timer import SkipMissedAndDrift, Timer


Expand All @@ -26,19 +26,28 @@ async def test_file_watcher(tmp_path: pathlib.Path) -> None:
number_of_writes = 0
expected_number_of_writes = 3

file_watcher = FileWatcher(paths=[str(tmp_path)])
file_watcher = FileWatcher(
paths=[str(tmp_path)], polling_interval=timedelta(seconds=0.05)
)
timer = Timer(timedelta(seconds=0.1), SkipMissedAndDrift())

async for selected in select(file_watcher, timer):
if selected_from(selected, timer):
filename.write_text(f"{selected.message}")
elif selected_from(selected, file_watcher):
event_type = EventType.CREATE if number_of_writes == 0 else EventType.MODIFY
assert selected.message == Event(type=event_type, path=filename)
number_of_writes += 1
# After receiving a write 3 times, unsubscribe from the writes channel
if number_of_writes == expected_number_of_writes:
break
event = selected.message
# If we receive updates for the directory itself, they should be only
# modifications, we only check that because we can have ordering issues if
# we try check also the order compared to events in the file.
if event.path == tmp_path:
assert event.type == EventType.MODIFY
elif event.path == filename:
assert event.type == event_type
number_of_writes += 1
# After receiving a write 3 times, unsubscribe from the writes channel
if number_of_writes == expected_number_of_writes:
break

assert number_of_writes == expected_number_of_writes

Expand All @@ -58,6 +67,7 @@ async def test_file_watcher_deletes(tmp_path: pathlib.Path) -> None:
paths=[str(tmp_path)],
event_types={EventType.DELETE},
force_polling=False,
polling_interval=timedelta(seconds=0.05),
)
write_timer = Timer(timedelta(seconds=0.1), SkipMissedAndDrift())
deletion_timer = Timer(timedelta(seconds=0.25), SkipMissedAndDrift())
Expand Down
Loading