Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ public JDKFileWatch(Path file, Executor exec,
assert !parent.equals(file);

this.internal = new JDKDirectoryWatch(parent, exec, (w, e) -> {
if (e.getKind() == WatchEvent.Kind.OVERFLOW) {
var overflow = new WatchEvent(WatchEvent.Kind.OVERFLOW, file);
eventHandler.accept(w, overflow);
}
if (fileName.equals(e.getRelativePath())) {
eventHandler.accept(w, e);
var kind = e.getKind();
if (kind == WatchEvent.Kind.OVERFLOW || e.getRelativePath().equals(fileName)) {
eventHandler.accept(w, new WatchEvent(kind, file));
}
}, eventFilter);

Expand Down
10 changes: 4 additions & 6 deletions src/test/java/engineering/swat/watch/SingleFileTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ void memorylessRescanOnOverflow() throws IOException, InterruptedException {
try (var watch = startWatchAndTriggerOverflow(Approximation.ALL, bookkeeper)) {
Thread.sleep(TestHelper.SHORT_WAIT.toMillis());

var fileName = watch.getPath().getFileName();
var parent = watch.getPath().getParent();

await("Overflow should trigger created event for `" + fileName + "`")
.until(() -> bookkeeper.events().kind(CREATED).rootPath(parent).relativePath(fileName).any());
var path = watch.getPath();
await("Overflow should trigger created event for `" + path + "`")
.until(() -> bookkeeper.events().kind(CREATED).rootPath(path).any());
await("Overflow shouldn't trigger created events for other files")
.until(() -> bookkeeper.events().kind(CREATED).rootPath(parent).relativePathNot(fileName).none());
.until(() -> bookkeeper.events().kind(CREATED).rootPathNot(path).none());
await("Overflow shouldn't trigger modified or deleted events")
.until(() -> bookkeeper.events().kind(MODIFIED, DELETED).none());
await("Overflow should be visible to user-defined event handler")
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/engineering/swat/watch/SmokeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,62 @@ void watchSingleFile() throws IOException {
await("Single file change").untilTrue(changed);
}
}

@Test
void moveRegularFileBetweenNestedDirectories() throws IOException {
var parent = testDir.getTestDirectory();
var child1 = Files.createDirectories(parent.resolve("from"));
var child2 = Files.createDirectories(parent.resolve("to"));
var file = Files.createFile(child1.resolve("file.txt"));

var parentWatchBookkeeper = new TestHelper.Bookkeeper();
var parentWatchConfig = Watch
.build(parent, WatchScope.PATH_AND_ALL_DESCENDANTS)
.on(parentWatchBookkeeper);

var child1WatchBookkeeper = new TestHelper.Bookkeeper();
var child1WatchConfig = Watch
.build(child1, WatchScope.PATH_AND_CHILDREN)
.on(child1WatchBookkeeper);

var child2WatchBookkeeper = new TestHelper.Bookkeeper();
var child2WatchConfig = Watch
.build(child2, WatchScope.PATH_AND_CHILDREN)
.on(child2WatchBookkeeper);

var fileWatchBookkeeper = new TestHelper.Bookkeeper();
var fileWatchConfig = Watch
.build(file, WatchScope.PATH_ONLY)
.on(fileWatchBookkeeper);

try (var parentWatch = parentWatchConfig.start();
var child1Watch = child1WatchConfig.start();
var child2Watch = child2WatchConfig.start();
var fileWatch = fileWatchConfig.start()) {

var source = child1.resolve(file.getFileName());
var target = child2.resolve(file.getFileName());
Files.move(source, target);

await("Move should be observed as delete by `parent` watch (file tree)")
.until(() -> parentWatchBookkeeper
.events().kind(DELETED).rootPath(parent).relativePath(parent.relativize(source)).any());

await("Move should be observed as create by `parent` watch (file tree)")
.until(() -> parentWatchBookkeeper
.events().kind(CREATED).rootPath(parent).relativePath(parent.relativize(target)).any());

await("Move should be observed as delete by `child1` watch (single directory)")
.until(() -> child1WatchBookkeeper
.events().kind(DELETED).rootPath(child1).relativePath(child1.relativize(source)).any());

await("Move should be observed as create by `child2` watch (single directory)")
.until(() -> child2WatchBookkeeper
.events().kind(CREATED).rootPath(child2).relativePath(child2.relativize(target)).any());

await("Move should be observed as delete by `file` watch (regular file)")
.until(() -> fileWatchBookkeeper
.events().kind(DELETED).rootPath(source).any());
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/engineering/swat/watch/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class TestHelper {
var os = System.getProperty("os.name", "?").toLowerCase();
if (os.contains("mac")) {
// OSX is SLOW on it's watches
delayFactor *= 2;
delayFactor *= 3;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes the OS test a lot slower again.

In a different PR we should try and remove all the sleeps and replace them by accurate awaits. as the sleeps get very long, especially with a high delayFactor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree, made an issue.

}
else if (os.contains("win")) {
// windows watches can be slow to get everything
Expand Down