Skip to content

Commit c761506

Browse files
committed
Added more test cases
1 parent 22a6027 commit c761506

File tree

1 file changed

+69
-48
lines changed

1 file changed

+69
-48
lines changed

src/test/java/engineering/swat/watch/ParallelWatches.java

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828

2929
import static org.junit.jupiter.api.Assertions.assertEquals;
3030
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
import static org.awaitility.Awaitility.await;
3132

33+
import java.io.Closeable;
3234
import java.io.IOException;
3335
import java.nio.file.Files;
36+
import java.nio.file.Path;
3437
import java.time.Duration;
3538
import java.util.concurrent.atomic.AtomicBoolean;
3639

@@ -61,42 +64,37 @@ static void setupEverything() {
6164
}
6265

6366
@Test
64-
void directoryAndFile() throws IOException {
67+
void directoryAndFileBothTrigger() throws IOException {
6568
var dirTriggered = new AtomicBoolean();
6669
var fileTriggered = new AtomicBoolean();
6770
var file = testDir.getTestFiles().get(0);
68-
var file2 = testDir.getTestFiles().get(1);
6971
assertEquals(testDir.getTestDirectory(), file.getParent(), "Test file should be a direct child of the test dir");
70-
assertEquals(testDir.getTestDirectory(), file2.getParent(), "Test file should be a direct child of the test dir");
71-
try (var dirWatch = Watch.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
72-
.on(e -> dirTriggered.set(true))
73-
.start()) {
74-
try (var fileWatch = Watch.build(file, WatchScope.PATH_ONLY).on(e -> fileTriggered.set(true)).start()) {
72+
try (var dirWatch = watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN, dirTriggered)) {
73+
try (var fileWatch = watch(file, WatchScope.PATH_ONLY, fileTriggered)) {
7574
Files.write(file, "test".getBytes());
76-
Awaitility.await()
77-
.alias("Directory should have picked up the file")
75+
await("Directory should have picked up the file")
7876
.untilTrue(dirTriggered);
79-
Awaitility.await()
80-
.alias("File should have picked up the file")
77+
await("File should have picked up the file")
8178
.untilTrue(fileTriggered);
79+
}
80+
}
81+
}
8282

83-
dirTriggered.set(false);
84-
fileTriggered.set(false);
85-
86-
Awaitility.await()
87-
.alias("should remain false")
88-
.pollDelay(TestHelper.SHORT_WAIT.minus(Duration.ofMillis(100)))
89-
.untilFalse(fileTriggered);
90-
91-
assertFalse(dirTriggered.get(), "Directory should also have not seen new events");
92-
83+
@Test
84+
void fileShouldNotTrigger() throws IOException {
85+
var dirTriggered = new AtomicBoolean();
86+
var fileTriggered = new AtomicBoolean();
87+
var file = testDir.getTestFiles().get(0);
88+
var file2 = testDir.getTestFiles().get(1);
89+
assertEquals(testDir.getTestDirectory(), file.getParent(), "Test file should be a direct child of the test dir");
90+
assertEquals(testDir.getTestDirectory(), file2.getParent(), "Test file should be a direct child of the test dir");
91+
try (var dirWatch = watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN, dirTriggered)) {
92+
try (var fileWatch = watch(file, WatchScope.PATH_ONLY, fileTriggered)) {
9393
Files.write(file2, "test2".getBytes());
94-
Awaitility.await()
95-
.alias("Directory should have picked up the file")
94+
await("Directory should have picked up the file")
9695
.untilTrue(dirTriggered);
97-
Awaitility.await()
98-
.alias("File should not have picked up the file")
99-
.during(TestHelper.SHORT_WAIT)
96+
await("File should not have picked up the file")
97+
.pollDelay(TestHelper.NORMAL_WAIT.minus(Duration.ofMillis(100)))
10098
.untilFalse(fileTriggered);
10199
}
102100
}
@@ -109,36 +107,59 @@ void nestedDirectory() throws IOException {
109107
var dir1 = testDir.getTestDirectory();
110108
var dir2 = dir1.resolve("nested");
111109
Files.createDirectories(dir2);
112-
try (var dirWatch = Watch.build(dir1, WatchScope.PATH_AND_ALL_DESCENDANTS)
113-
.on(e -> dirTriggered.set(true))
114-
.start()) {
115-
try (var nestedDirWatch = Watch.build(dir2, WatchScope.PATH_AND_CHILDREN).on(e -> nestedDirTriggered.set(true)).start()) {
110+
try (var dirWatch = watch(dir1, WatchScope.PATH_AND_ALL_DESCENDANTS, dirTriggered)) {
111+
try (var nestedDirWatch = watch(dir2, WatchScope.PATH_AND_CHILDREN, nestedDirTriggered)) {
112+
Files.write(dir2.resolve("a2.txt"), "test2".getBytes());
113+
await("Directory should have picked up nested file")
114+
.untilTrue(dirTriggered);
115+
await("Nested directory should have picked up the file")
116+
.untilTrue(nestedDirTriggered);
117+
}
118+
}
119+
}
120+
121+
@Test
122+
void nestedDirectoryNotTrigger() throws IOException {
123+
var dirTriggered = new AtomicBoolean();
124+
var nestedDirTriggered = new AtomicBoolean();
125+
var dir1 = testDir.getTestDirectory();
126+
var dir2 = dir1.resolve("nested");
127+
Files.createDirectories(dir2);
128+
try (var dirWatch = watch(dir1, WatchScope.PATH_AND_ALL_DESCENDANTS, dirTriggered)) {
129+
try (var nestedDirWatch = watch(dir2, WatchScope.PATH_AND_CHILDREN, nestedDirTriggered)) {
116130
Files.write(dir1.resolve("a1.txt"), "1".getBytes());
117-
Awaitility.await().alias("Directory should have picked up the file")
131+
await("Directory should have picked up the file")
118132
.untilTrue(dirTriggered);
119-
Awaitility.await().alias("Nested dir should not have picked up the file")
120-
.during(TestHelper.SHORT_WAIT)
133+
await("Nested dir should not have picked up the file")
134+
.pollDelay(TestHelper.NORMAL_WAIT.minus(Duration.ofMillis(100)))
121135
.untilFalse(nestedDirTriggered);
136+
}
137+
}
138+
}
122139

123-
dirTriggered.set(false);
124-
125-
Awaitility.await()
126-
.alias("should remain false")
127-
.pollDelay(TestHelper.SHORT_WAIT.minus(Duration.ofMillis(100)))
128-
.untilFalse(dirTriggered);
129-
130-
assertFalse(nestedDirTriggered.get(), "Nested directory should also have not seen new events");
140+
private static Closeable watch(Path p, WatchScope s, AtomicBoolean t) throws IOException {
141+
return Watch.build(p, s).on(e -> t.set(true)).start();
142+
}
131143

132-
Files.write(dir2.resolve("a2.txt"), "test2".getBytes());
133-
Awaitility.await()
134-
.alias("Directory should have picked up the file")
135-
.untilTrue(dirTriggered);
136-
Awaitility.await()
137-
.alias("Nested directory should have picked up the file")
138-
.untilTrue(nestedDirTriggered);
144+
@Test
145+
void watchingSameDirectory() throws IOException {
146+
var trig1 = new AtomicBoolean();
147+
var trig2 = new AtomicBoolean();
148+
var dir = testDir.getTestDirectory();
149+
var nestedDir = dir.resolve("a/b/");
150+
Files.createDirectories(nestedDir);
151+
try (var dirWatch = watch(dir, WatchScope.PATH_AND_ALL_DESCENDANTS, trig1)) {
152+
try (var dirWatch2 = watch(dir, WatchScope.PATH_AND_ALL_DESCENDANTS, trig2)) {
153+
Files.write(dir.resolve("a1.txt"), "1".getBytes());
154+
155+
await("Watch 1 should have triggered")
156+
.untilTrue(trig1);
157+
await("Directory should have picked up the file")
158+
.untilTrue(trig2);
139159
}
140160
}
141161
}
142162

143163

164+
144165
}

0 commit comments

Comments
 (0)