Skip to content

Commit 630f4ac

Browse files
authored
Make the watchfiles reload tests deterministic (#2972)
1 parent 9154922 commit 630f4ac

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

‎tests/supervisors/test_reload.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections.abc import Callable, Generator
77
from pathlib import Path
88
from threading import Thread
9-
from time import sleep
9+
from time import monotonic, sleep
1010

1111
import pytest
1212
from pytest_mock import MockerFixture
@@ -74,11 +74,21 @@ def _reload_tester(
7474
reloader.restart()
7575
if WatchFilesReload is not None and isinstance(reloader, WatchFilesReload):
7676
touch_soon(*files)
77-
else:
78-
assert not next(reloader)
79-
sleep(0.1)
80-
for file in files:
81-
file.touch()
77+
# Poll until the touched files are reported, ignoring unrelated churn.
78+
expected = set(files)
79+
deadline = monotonic() + 5
80+
seen: set[Path] = set()
81+
while monotonic() < deadline:
82+
changes = next(reloader)
83+
if changes:
84+
seen.update(p for p in changes if p in expected)
85+
if seen == expected:
86+
break
87+
return sorted(seen) if seen else None
88+
assert not next(reloader)
89+
sleep(0.1)
90+
for file in files:
91+
file.touch()
8292
return next(reloader)
8393

8494
@pytest.mark.parametrize("reloader_class", [StatReload, WatchFilesReload])

0 commit comments

Comments
 (0)