|
26 | 26 | */ |
27 | 27 | package engineering.swat.watch; |
28 | 28 |
|
| 29 | +import static engineering.swat.watch.WatchEvent.Kind.CREATED; |
| 30 | +import static engineering.swat.watch.WatchEvent.Kind.DELETED; |
| 31 | +import static engineering.swat.watch.WatchEvent.Kind.MODIFIED; |
| 32 | +import static engineering.swat.watch.WatchEvent.Kind.OVERFLOW; |
29 | 33 | import static org.awaitility.Awaitility.await; |
30 | 34 |
|
31 | 35 | import java.io.IOException; |
32 | 36 | import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
33 | 38 | import java.nio.file.attribute.FileTime; |
34 | 39 | import java.time.Instant; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.List; |
35 | 44 | import java.util.concurrent.atomic.AtomicBoolean; |
| 45 | +import java.util.function.Consumer; |
| 46 | +import java.util.function.Predicate; |
| 47 | +import java.util.stream.Stream; |
36 | 48 |
|
37 | 49 | import org.awaitility.Awaitility; |
38 | 50 | import org.junit.jupiter.api.AfterEach; |
39 | 51 | import org.junit.jupiter.api.BeforeAll; |
40 | 52 | import org.junit.jupiter.api.BeforeEach; |
41 | 53 | import org.junit.jupiter.api.Test; |
42 | 54 |
|
| 55 | +import engineering.swat.watch.impl.EventHandlingWatch; |
| 56 | + |
43 | 57 | class SingleFileTests { |
44 | 58 | private TestDirectory testDir; |
45 | 59 |
|
@@ -117,4 +131,72 @@ void singleFileThatMonitorsOnlyADirectory() throws IOException, InterruptedExcep |
117 | 131 | .untilTrue(seen); |
118 | 132 | } |
119 | 133 | } |
| 134 | + |
| 135 | + @Test |
| 136 | + void noRescanOnOverflow() throws IOException, InterruptedException { |
| 137 | + var bookkeeper = new Bookkeeper(); |
| 138 | + try (var watch = startWatchAndTriggerOverflow(OnOverflow.NONE, bookkeeper)) { |
| 139 | + Thread.sleep(TestHelper.SHORT_WAIT.toMillis()); |
| 140 | + |
| 141 | + await("Overflow shouldn't trigger created, modified, or deleted events") |
| 142 | + .until(() -> bookkeeper.fullPaths(CREATED, MODIFIED, DELETED).count() == 0); |
| 143 | + await("Overflow should be visible to user-defined event handler") |
| 144 | + .until(() -> bookkeeper.fullPaths(OVERFLOW).count() == 1); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void memorylessRescanOnOverflow() throws IOException, InterruptedException { |
| 150 | + var bookkeeper = new Bookkeeper(); |
| 151 | + try (var watch = startWatchAndTriggerOverflow(OnOverflow.ALL, bookkeeper)) { |
| 152 | + Thread.sleep(TestHelper.SHORT_WAIT.toMillis()); |
| 153 | + |
| 154 | + var isFile = Predicate.isEqual(watch.getPath()); |
| 155 | + var isNotFile = Predicate.not(isFile); |
| 156 | + |
| 157 | + await("Overflow should trigger created event for `file`") |
| 158 | + .until(() -> bookkeeper.fullPaths(CREATED).filter(isFile).count() == 1); |
| 159 | + await("Overflow shouldn't trigger created events for other files") |
| 160 | + .until(() -> bookkeeper.fullPaths(CREATED).filter(isNotFile).count() == 0); |
| 161 | + await("Overflow shouldn't trigger modified events (`file` is empty)") |
| 162 | + .until(() -> bookkeeper.fullPaths(MODIFIED).count() == 0); |
| 163 | + await("Overflow shouldn't trigger deleted events") |
| 164 | + .until(() -> bookkeeper.fullPaths(DELETED).count() == 0); |
| 165 | + await("Overflow should be visible to user-defined event handler") |
| 166 | + .until(() -> bookkeeper.fullPaths(OVERFLOW).count() == 1); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private ActiveWatch startWatchAndTriggerOverflow(OnOverflow whichFiles, Bookkeeper bookkeeper) throws IOException { |
| 171 | + var parent = testDir.getTestDirectory(); |
| 172 | + var file = parent.resolve("a.txt"); |
| 173 | + |
| 174 | + var watch = Watcher |
| 175 | + .watch(file, WatchScope.PATH_ONLY) |
| 176 | + .approximate(whichFiles) |
| 177 | + .on(bookkeeper) |
| 178 | + .start(); |
| 179 | + |
| 180 | + var overflow = new WatchEvent(WatchEvent.Kind.OVERFLOW, parent); |
| 181 | + ((EventHandlingWatch) watch).handleEvent(overflow); |
| 182 | + return watch; |
| 183 | + } |
| 184 | + |
| 185 | + private static class Bookkeeper implements Consumer<WatchEvent> { |
| 186 | + private final List<WatchEvent> events = Collections.synchronizedList(new ArrayList<>()); |
| 187 | + |
| 188 | + public Stream<WatchEvent> events(WatchEvent.Kind... kinds) { |
| 189 | + var list = Arrays.asList(kinds.length == 0 ? WatchEvent.Kind.values() : kinds); |
| 190 | + return events.stream().filter(e -> list.contains(e.getKind())); |
| 191 | + } |
| 192 | + |
| 193 | + public Stream<Path> fullPaths(WatchEvent.Kind... kinds) { |
| 194 | + return events(kinds).map(WatchEvent::calculateFullPath); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void accept(WatchEvent e) { |
| 199 | + events.add(e); |
| 200 | + } |
| 201 | + } |
120 | 202 | } |
0 commit comments