Skip to content

Commit defeb7f

Browse files
authored
Don't watch the whole directory in the file watcher test (#326)
When watching the whole test directory we'll also get some events for the directory, and those events can come with different order depending on the OS, so we can't rely on them. We check that if a directory event comes, it is a modification. Also when testing for events in a file watcher, we need to set the `polling_interval` to a value that is small enough to detect the changes we are expecting, otherwise some events could be "merged" if the changes happen faster than the polling interval. Fixes #324.
2 parents 127e542 + 12229a4 commit defeb7f

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

tests/test_file_watcher_integration.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pytest
1111

1212
from frequenz.channels import ReceiverStoppedError, select, selected_from
13-
from frequenz.channels.file_watcher import Event, EventType, FileWatcher
13+
from frequenz.channels.file_watcher import EventType, FileWatcher
1414
from frequenz.channels.timer import SkipMissedAndDrift, Timer
1515

1616

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

29-
file_watcher = FileWatcher(paths=[str(tmp_path)])
29+
file_watcher = FileWatcher(
30+
paths=[str(tmp_path)], polling_interval=timedelta(seconds=0.05)
31+
)
3032
timer = Timer(timedelta(seconds=0.1), SkipMissedAndDrift())
3133

3234
async for selected in select(file_watcher, timer):
3335
if selected_from(selected, timer):
3436
filename.write_text(f"{selected.message}")
3537
elif selected_from(selected, file_watcher):
3638
event_type = EventType.CREATE if number_of_writes == 0 else EventType.MODIFY
37-
assert selected.message == Event(type=event_type, path=filename)
38-
number_of_writes += 1
39-
# After receiving a write 3 times, unsubscribe from the writes channel
40-
if number_of_writes == expected_number_of_writes:
41-
break
39+
event = selected.message
40+
# If we receive updates for the directory itself, they should be only
41+
# modifications, we only check that because we can have ordering issues if
42+
# we try check also the order compared to events in the file.
43+
if event.path == tmp_path:
44+
assert event.type == EventType.MODIFY
45+
elif event.path == filename:
46+
assert event.type == event_type
47+
number_of_writes += 1
48+
# After receiving a write 3 times, unsubscribe from the writes channel
49+
if number_of_writes == expected_number_of_writes:
50+
break
4251

4352
assert number_of_writes == expected_number_of_writes
4453

@@ -58,6 +67,7 @@ async def test_file_watcher_deletes(tmp_path: pathlib.Path) -> None:
5867
paths=[str(tmp_path)],
5968
event_types={EventType.DELETE},
6069
force_polling=False,
70+
polling_interval=timedelta(seconds=0.05),
6171
)
6272
write_timer = Timer(timedelta(seconds=0.1), SkipMissedAndDrift())
6373
deletion_timer = Timer(timedelta(seconds=0.25), SkipMissedAndDrift())

0 commit comments

Comments
 (0)