-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelWatches.java
More file actions
165 lines (147 loc) · 6.69 KB
/
ParallelWatches.java
File metadata and controls
165 lines (147 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* BSD 2-Clause License
*
* Copyright (c) 2023, Swat.engineering
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package engineering.swat.watch;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static engineering.swat.watch.util.WaitFor.await;
import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import engineering.swat.watch.util.WaitFor;
class ParallelWatches {
private TestDirectory testDir;
@BeforeEach
void setup() throws IOException {
testDir = new TestDirectory();
}
@AfterEach
void cleanup() {
if (testDir != null) {
testDir.close();
}
}
@BeforeAll
static void setupEverything() {
WaitFor.setDefaultTimeout(TestHelper.NORMAL_WAIT);
}
@Test
void directoryAndFileBothTrigger() throws IOException {
var dirTriggered = new AtomicBoolean();
var fileTriggered = new AtomicBoolean();
var file = testDir.getTestFiles().get(0);
assertEquals(testDir.getTestDirectory(), file.getParent(), "Test file should be a direct child of the test dir");
try (var dirWatch = watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN, dirTriggered)) {
try (var fileWatch = watch(file, WatchScope.PATH_ONLY, fileTriggered)) {
Files.write(file, "test".getBytes());
await("Directory should have picked up the file")
.until(dirTriggered);
await("File should have picked up the file")
.until(fileTriggered);
}
}
}
@Test
void fileShouldNotTrigger() throws IOException {
var dirTriggered = new AtomicBoolean();
var fileTriggered = new AtomicBoolean();
var file = testDir.getTestFiles().get(0);
var file2 = testDir.getTestFiles().get(1);
assertEquals(testDir.getTestDirectory(), file.getParent(), "Test file should be a direct child of the test dir");
assertEquals(testDir.getTestDirectory(), file2.getParent(), "Test file should be a direct child of the test dir");
try (var dirWatch = watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN, dirTriggered)) {
try (var fileWatch = watch(file, WatchScope.PATH_ONLY, fileTriggered)) {
Files.write(file2, "test2".getBytes());
await("Directory should have picked up the file")
.until(dirTriggered);
await("File should not have picked up the file")
.time(TestHelper.SHORT_WAIT)
.holdsFalse(fileTriggered);
}
}
}
@Test
void nestedDirectory() throws IOException {
var dirTriggered = new AtomicBoolean();
var nestedDirTriggered = new AtomicBoolean();
var dir1 = testDir.getTestDirectory();
var dir2 = dir1.resolve("nested");
Files.createDirectories(dir2);
try (var dirWatch = watch(dir1, WatchScope.PATH_AND_ALL_DESCENDANTS, dirTriggered)) {
try (var nestedDirWatch = watch(dir2, WatchScope.PATH_AND_CHILDREN, nestedDirTriggered)) {
Files.write(dir2.resolve("a2.txt"), "test2".getBytes());
await("Directory should have picked up nested file")
.until(dirTriggered);
await("Nested directory should have picked up the file")
.until(nestedDirTriggered);
}
}
}
@Test
void nestedDirectoryNotTrigger() throws IOException {
var dirTriggered = new AtomicBoolean();
var nestedDirTriggered = new AtomicBoolean();
var dir1 = testDir.getTestDirectory();
var dir2 = dir1.resolve("nested");
Files.createDirectories(dir2);
try (var dirWatch = watch(dir1, WatchScope.PATH_AND_ALL_DESCENDANTS, dirTriggered)) {
try (var nestedDirWatch = watch(dir2, WatchScope.PATH_AND_CHILDREN, nestedDirTriggered)) {
Files.write(dir1.resolve("a1.txt"), "1".getBytes());
await("Directory should have picked up the file")
.until(dirTriggered);
await("Nested dir should not have picked up the file")
.time(TestHelper.SHORT_WAIT)
.holdsFalse(nestedDirTriggered);
}
}
}
private static Closeable watch(Path p, WatchScope s, AtomicBoolean t) throws IOException {
return Watch.build(p, s).on(e -> t.set(true)).start();
}
@Test
void watchingSameDirectory() throws IOException {
var trig1 = new AtomicBoolean();
var trig2 = new AtomicBoolean();
var dir = testDir.getTestDirectory();
var nestedDir = dir.resolve("a/b/");
Files.createDirectories(nestedDir);
try (var dirWatch = watch(dir, WatchScope.PATH_AND_ALL_DESCENDANTS, trig1)) {
try (var dirWatch2 = watch(dir, WatchScope.PATH_AND_ALL_DESCENDANTS, trig2)) {
Files.write(dir.resolve("a1.txt"), "1".getBytes());
await("Watch 1 should have triggered")
.until(trig1);
await("Directory should have picked up the file")
.until(trig2);
}
}
}
}