diff --git a/tests/0-dirwatcher-rec.d b/tests/0-dirwatcher-rec.d index a52eab2..d6b9538 100644 --- a/tests/0-dirwatcher-rec.d +++ b/tests/0-dirwatcher-rec.d @@ -35,13 +35,15 @@ void main() // test non-recursive watcher watcher = eventDriver.watchers.watchDirectory(testDir, false, toDelegate(&testCallback)); assert(watcher != WatcherID.invalid); - Thread.sleep(400.msecs); // some watcher implementations need time to initialize + // some watcher implementations need time to initialize or report past events + dropChanges(2000.msecs); testFile( "file1.dat"); testFile( "file2.dat"); testFile( "dira/file1.dat", false); testCreateDir("dirb"); testFile( "dirb/file1.dat", false); testRemoveDir("dirb"); + testFile( "file1.dat"); eventDriver.watchers.releaseRef(watcher); testFile( "file1.dat", false); testRemoveDir("dira", false); @@ -50,7 +52,8 @@ void main() // test recursive watcher watcher = eventDriver.watchers.watchDirectory(testDir, true, toDelegate(&testCallback)); assert(watcher != WatcherID.invalid); - Thread.sleep(400.msecs); // some watcher implementations need time to initialize + // some watcher implementations need time to initialize or report past events + dropChanges(2000.msecs); testFile( "file1.dat"); testFile( "file2.dat"); testFile( "dira/file1.dat"); @@ -58,6 +61,7 @@ void main() testFile( "dirb/file1.dat"); testRename( "dirb", "dirc"); testFile( "dirc/file2.dat"); + testFile( "file1.dat"); eventDriver.watchers.releaseRef(watcher); testFile( "file1.dat", false); testFile( "dira/file1.dat", false); @@ -78,11 +82,30 @@ void testCallback(WatcherID w, in ref FileChange ch) pendingChanges ~= ch; } +void dropChanges(Duration dur) +{ + auto starttime = MonoTime.currTime(); + auto remdur = dur; + while (remdur > 0.msecs) { + auto er = eventDriver.core.processEvents(remdur); + switch (er) { + default: assert(false, format("Unexpected event loop exit code: %s", er)); + case ExitReason.idle: break; + case ExitReason.timeout: break; + case ExitReason.outOfWaiters: + assert(false, "No watcher left, but expected change."); + } + remdur = dur - (MonoTime.currTime() - starttime); + } + + pendingChanges = null; +} + void expectChange(FileChange ch, bool expect_change) { auto starttime = MonoTime.currTime(); again: while (!pendingChanges.length) { - auto er = eventDriver.core.processEvents(10.msecs); + auto er = eventDriver.core.processEvents(100.msecs); switch (er) { default: assert(false, format("Unexpected event loop exit code: %s", er)); case ExitReason.idle: break; @@ -123,9 +146,12 @@ void expectChange(FileChange ch, bool expect_change) if (pch.kind == FileChangeKind.modified && (pch.name == "dira" || pch.name == "dirb")) goto again; - // test all field excep the isDir one, which does not work on all systems - assert(pch.kind == ch.kind && pch.baseDirectory == ch.baseDirectory && - pch.directory == ch.directory && pch.name == ch.name, + // test all field except the isDir one, which does not work on all systems + // we allow "modified" instead of "added" here, as the FSEvents based watcher + // has strange results on the CI VM + assert((pch.kind == ch.kind || pch.kind == FileChangeKind.modified && ch.kind == FileChangeKind.added) + && pch.baseDirectory == ch.baseDirectory + && pch.directory == ch.directory && pch.name == ch.name, format("Unexpected change: %s vs %s", pch, ch)); }