Skip to content

Commit 3f8a12f

Browse files
authored
fix(tests): add events_checker() fixture(#1151)
* Wait for directory reader thread to start. * fix: add events_checker() fixture, use for tests This makes the unit tests more reliable by allowing events to be received in different orders and by allowing additional (non-expected) events. * Check for DirModifiedEvent on file create. This matches the original test logic, before addition of the events_checker() fixture. * Make events_checker() validate order by default. Rather than allow events in any order, match the order of the events as added. On MacOS, fsevents does not produce events in a determistic order so allow any ordering on that platform. * Use events_checker() as a context manager. This reduces the code a little. Use `ec` as the short local name of the checker instance. Add `verbose` argument to `events_checker()` that turns on verbose debugging output. * Fix lint warnings. * Unit test fixes for MacOS. Remove events that are sometimes not generated. * Fix unreliable test for Window. * Fix lint warning, improve comment.
1 parent 1e48d0b commit 3f8a12f

File tree

6 files changed

+473
-338
lines changed

6 files changed

+473
-338
lines changed

src/watchdog/observers/winapi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ def _run(self) -> None:
412412
# missed, we re-use these for each call to _run_inner().
413413
event_buffer = ctypes.create_string_buffer(BUFFER_SIZE)
414414
nbytes = DWORD()
415+
self._buf_queue.put(b'') # indicates that this thread has started
415416
try:
416417
while not self._should_stop:
417418
self._run_inner(handle, event_buffer, nbytes)
@@ -430,6 +431,11 @@ def start(self) -> None:
430431
return # stop already called, do not start
431432
self._reader_thread = threading.Thread(target=self._run)
432433
self._reader_thread.start()
434+
# Wait for empty bytes object, indicating reader thread has started.
435+
# This reduces the time window between this method returning and the
436+
# actual ReadDirectoryChangesW() call, which reduces the time that
437+
# events could be missed.
438+
_ = self._buf_queue.get(timeout=2)
433439

434440
def stop(self) -> None:
435441
with self._lock:

tests/conftest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from .utils import ExpectEvent, Helper, P, StartWatching, TestEventQueue
11+
from .utils import EventsChecker, ExpectEvent, Helper, P, StartWatching, TestEventQueue
1212

1313

1414
@pytest.fixture
@@ -78,3 +78,8 @@ def start_watching_fixture(helper: Helper) -> StartWatching:
7878
@pytest.fixture(name="expect_event")
7979
def expect_event_fixture(helper: Helper) -> ExpectEvent:
8080
return helper.expect_event
81+
82+
83+
@pytest.fixture(name="events_checker")
84+
def events_checker_fixture(helper: Helper) -> EventsChecker:
85+
return helper.events_checker

0 commit comments

Comments
 (0)