Fix flaky test_renaming_top_level_directory on Windows#1179
Open
Arman-Luthra wants to merge 1 commit into
Open
Fix flaky test_renaming_top_level_directory on Windows#1179Arman-Luthra wants to merge 1 commit into
Arman-Luthra wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the Windows failure mode of #1128.
Root cause
On Windows,
mkdir(p("a", "b"))updates the last-write time of directorya, but NTFS flushes that change - and thus delivers the correspondingFILE_ACTION_MODIFIEDnotification fora- lazily. InstrumentingDirectoryChangeReaderdirectly shows the notification is not delivered when the subdirectory is created; on my machine it only shows up when the subsequentmv(p("a"), p("a2"))touches the directory, ~4 seconds later:Two consequences for the test:
aarrives in the events-checker block for the rename, not the block for the mkdir - or, on other machines (apparently the GitHub runners), not at all.ano longer exists on disk anymore, soWindowsApiEmitter.queue_events()classifies it as aFileModifiedEventinstead of aDirModifiedEvent(src/watchdog/observers/read_directory_changes.pyline 89 - theos.path.isdir(src_path)check runs at processing time, not at event time).The events checker requires exactly the expected events, in order, so the stray event makes the test fail with
AssertionError: wrong number of events.Fix
Test-side only. The emitter cannot reliably classify a modified event for a path that no longer exists without the extended info from
ReadDirectoryChangesExW, which is what #1156 is introducing for deletions - I did not want to conflict with that work._EventsChecker.add()acceptsoptional=Truefor events whose delivery is not deterministic on the platform; both the ordered and unordered checks now tolerate absent optional events. The ordered check is rewritten as a two-pointer walk over found/expected events (the previouszip()-based check also silently ignored missing trailing events wheneverallow_extra_events()was used).expected_classmay now be a tuple of classes (passed straight through toisinstance()). This is needed because the class of the delayed event depends on whether the directory still exists when the emitter processes the notification (see point 2 above).test_renaming_top_level_directorydeclares the delayed notification foraas optional in the two affected blocks.Before/after
On this machine (Windows 11, NTFS, Python 3.12.10),
tests/test_emitter.py::test_renaming_top_level_directory:Full test suite apart from
test_0_watchmedo: 102 passed, 12 skipped.test_0_watchmedohad one unrelated flaky failure (test_auto_restart_on_file_change_debounce, thread-count assertion) which also fails on master CI on Linux 3.13t; it passes on rerun and does not use the events checker.ruff checkis clean on the touched files.Limitations
#1128 also shows a Linux/Python 3.14 failure (
DirMovedEventnot delivered within the 2 s timeout, pre-dating the events-checker refactor). I could not reproduce that on this machine and this PR does not address it - only the Windows failure mode, which is deterministic here.