Skip to content

Commit ce46e33

Browse files
authored
Merge pull request #53 from SWAT-engineering/chore/test-paralell-watches
Added some test cases for parallel watches of same/nested paths
2 parents 6da04e9 + c761506 commit ce46e33

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* BSD 2-Clause License
3+
*
4+
* Copyright (c) 2023, Swat.engineering
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
package engineering.swat.watch;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
import static org.awaitility.Awaitility.await;
32+
33+
import java.io.Closeable;
34+
import java.io.IOException;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
37+
import java.time.Duration;
38+
import java.util.concurrent.atomic.AtomicBoolean;
39+
40+
import org.awaitility.Awaitility;
41+
import org.junit.jupiter.api.AfterEach;
42+
import org.junit.jupiter.api.BeforeAll;
43+
import org.junit.jupiter.api.BeforeEach;
44+
import org.junit.jupiter.api.Test;
45+
46+
class ParallelWatches {
47+
private TestDirectory testDir;
48+
49+
@BeforeEach
50+
void setup() throws IOException {
51+
testDir = new TestDirectory();
52+
}
53+
54+
@AfterEach
55+
void cleanup() {
56+
if (testDir != null) {
57+
testDir.close();
58+
}
59+
}
60+
61+
@BeforeAll
62+
static void setupEverything() {
63+
Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT);
64+
}
65+
66+
@Test
67+
void directoryAndFileBothTrigger() throws IOException {
68+
var dirTriggered = new AtomicBoolean();
69+
var fileTriggered = new AtomicBoolean();
70+
var file = testDir.getTestFiles().get(0);
71+
assertEquals(testDir.getTestDirectory(), file.getParent(), "Test file should be a direct child of the test dir");
72+
try (var dirWatch = watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN, dirTriggered)) {
73+
try (var fileWatch = watch(file, WatchScope.PATH_ONLY, fileTriggered)) {
74+
Files.write(file, "test".getBytes());
75+
await("Directory should have picked up the file")
76+
.untilTrue(dirTriggered);
77+
await("File should have picked up the file")
78+
.untilTrue(fileTriggered);
79+
}
80+
}
81+
}
82+
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)) {
93+
Files.write(file2, "test2".getBytes());
94+
await("Directory should have picked up the file")
95+
.untilTrue(dirTriggered);
96+
await("File should not have picked up the file")
97+
.pollDelay(TestHelper.NORMAL_WAIT.minus(Duration.ofMillis(100)))
98+
.untilFalse(fileTriggered);
99+
}
100+
}
101+
}
102+
103+
@Test
104+
void nestedDirectory() throws IOException {
105+
var dirTriggered = new AtomicBoolean();
106+
var nestedDirTriggered = new AtomicBoolean();
107+
var dir1 = testDir.getTestDirectory();
108+
var dir2 = dir1.resolve("nested");
109+
Files.createDirectories(dir2);
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)) {
130+
Files.write(dir1.resolve("a1.txt"), "1".getBytes());
131+
await("Directory should have picked up the file")
132+
.untilTrue(dirTriggered);
133+
await("Nested dir should not have picked up the file")
134+
.pollDelay(TestHelper.NORMAL_WAIT.minus(Duration.ofMillis(100)))
135+
.untilFalse(nestedDirTriggered);
136+
}
137+
}
138+
}
139+
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+
}
143+
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);
159+
}
160+
}
161+
}
162+
163+
164+
165+
}

0 commit comments

Comments
 (0)