Skip to content
Draft
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
32 changes: 16 additions & 16 deletions src/test/java/engineering/swat/watch/ParallelWatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.awaitility.Awaitility.await;
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.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;

import org.awaitility.Awaitility;
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;

Expand All @@ -60,7 +60,7 @@ void cleanup() {

@BeforeAll
static void setupEverything() {
Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT);
WaitFor.setDefaultTimeout(TestHelper.NORMAL_WAIT);
}

@Test
Expand All @@ -73,9 +73,9 @@ void directoryAndFileBothTrigger() throws IOException {
try (var fileWatch = watch(file, WatchScope.PATH_ONLY, fileTriggered)) {
Files.write(file, "test".getBytes());
await("Directory should have picked up the file")
.untilTrue(dirTriggered);
.until(dirTriggered);
await("File should have picked up the file")
.untilTrue(fileTriggered);
.until(fileTriggered);
}
}
}
Expand All @@ -92,10 +92,10 @@ void fileShouldNotTrigger() throws IOException {
try (var fileWatch = watch(file, WatchScope.PATH_ONLY, fileTriggered)) {
Files.write(file2, "test2".getBytes());
await("Directory should have picked up the file")
.untilTrue(dirTriggered);
.until(dirTriggered);
await("File should not have picked up the file")
.pollDelay(TestHelper.NORMAL_WAIT.minus(Duration.ofMillis(100)))
.untilFalse(fileTriggered);
.time(TestHelper.SHORT_WAIT)
.holdsFalse(fileTriggered);
}
}
}
Expand All @@ -111,9 +111,9 @@ void nestedDirectory() throws IOException {
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")
.untilTrue(dirTriggered);
.until(dirTriggered);
await("Nested directory should have picked up the file")
.untilTrue(nestedDirTriggered);
.until(nestedDirTriggered);
}
}
}
Expand All @@ -129,10 +129,10 @@ void nestedDirectoryNotTrigger() throws IOException {
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")
.untilTrue(dirTriggered);
.until(dirTriggered);
await("Nested dir should not have picked up the file")
.pollDelay(TestHelper.NORMAL_WAIT.minus(Duration.ofMillis(100)))
.untilFalse(nestedDirTriggered);
.time(TestHelper.SHORT_WAIT)
.holdsFalse(nestedDirTriggered);
}
}
}
Expand All @@ -153,9 +153,9 @@ void watchingSameDirectory() throws IOException {
Files.write(dir.resolve("a1.txt"), "1".getBytes());

await("Watch 1 should have triggered")
.untilTrue(trig1);
.until(trig1);
await("Directory should have picked up the file")
.untilTrue(trig2);
.until(trig2);
}
}
}
Expand Down
80 changes: 48 additions & 32 deletions src/test/java/engineering/swat/watch/RecursiveWatchTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@
package engineering.swat.watch;

import static engineering.swat.watch.WatchEvent.Kind.CREATED;
import static org.awaitility.Awaitility.await;
import static engineering.swat.watch.util.WaitFor.await;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -49,6 +56,7 @@

import engineering.swat.watch.WatchEvent.Kind;
import engineering.swat.watch.impl.EventHandlingWatch;
import engineering.swat.watch.util.WaitFor;

class RecursiveWatchTests {
private final Logger logger = LogManager.getLogger();
Expand All @@ -69,7 +77,7 @@ void cleanup() {

@BeforeAll
static void setupEverything() {
Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT);
WaitFor.setDefaultTimeout(TestHelper.NORMAL_WAIT);
}

@Test
Expand Down Expand Up @@ -99,9 +107,9 @@ void newDirectoryWithFilesChangesDetected() throws IOException {
target.set(freshFile);
logger.debug("Interested in: {}", freshFile);
Files.writeString(freshFile, "Hello world");
await("New files should have been seen").untilTrue(created);
await("New files should have been seen").until(created);
Files.writeString(freshFile, "Hello world 2");
await("Fresh file change have been detected").untilTrue(changed);
await("Fresh file change have been detected").until(changed);
}
}

Expand All @@ -121,7 +129,7 @@ void correctRelativePathIsReported() throws IOException {
var targetFile = testDir.getTestDirectory().resolve(relative);
Files.createDirectories(targetFile.getParent());
Files.writeString(targetFile, "Hello World");
await("Nested path is seen").untilTrue(seen);
await("Nested path is seen").until(seen);
}

}
Expand All @@ -143,19 +151,19 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException {
try (var watch = watchConfig.start()) {
Files.delete(target);
await("File deletion should generate delete event")
.untilTrue(seen);
.until(seen);
}
}

@ParameterizedTest
@EnumSource // Repeat test for each `Approximation` value
void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, InterruptedException {
var parent = testDir.getTestDirectory();
var descendants = new Path[] {
var descendants = List.of(
Path.of("foo"),
Path.of("bar"),
Path.of("bar", "x", "y", "z")
};
);

// Configure and start watch
var dropEvents = new AtomicBoolean(false); // Toggles overflow simulation
Expand All @@ -169,28 +177,38 @@ void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, Int
try (var watch = (EventHandlingWatch) watchConfig.start()) {

// Define helper functions to test which events have happened
Consumer<Path> awaitCreation = p ->
await("Creation of `" + p + "` should be observed")
.until(() -> bookkeeper.events().kind(CREATED).rootPath(parent).relativePath(p).any());

Consumer<Path> awaitNotCreation = p ->
await("Creation of `" + p + "` shouldn't be observed: " + bookkeeper)
.pollDelay(TestHelper.TINY_WAIT)
.until(() -> bookkeeper.events().kind(CREATED).rootPath(parent).relativePath(p).none());
Consumer<Collection<Path>> awaitCreation = paths ->
WaitFor.await("Creation should be observed")
.untilContainsAll(() ->
bookkeeper.events()
.kind(CREATED)
.rootPath(parent)
.relativePath(paths)
.events()
.map(WatchEvent::getRelativePath)
, paths);

// Begin overflow simulation
dropEvents.set(true);

// Create descendants and files. They *shouldn't* be observed yet.
var missedCreates = new ArrayList<Path>();
var file1 = Path.of("file1.txt");
for (var descendant : descendants) {
Files.createDirectories(parent.resolve(descendant));
Files.createFile(parent.resolve(descendant).resolve(file1));
}
for (var descendant : descendants) {
awaitNotCreation.accept(descendant);
awaitNotCreation.accept(descendant.resolve(file1));
var d = parent.resolve(descendant);
var f = d.resolve(file1);
Files.createDirectories(d);
Files.createFile(f);
missedCreates.add(descendant);
missedCreates.add(descendant.resolve(file1));
}
WaitFor.await(() -> "We should not have seen any events")
.time(TestHelper.TINY_WAIT)
.holdsEmpty(() -> bookkeeper.events()
.kind(CREATED)
.rootPath(parent)
.relativePath(missedCreates)
.events());

// End overflow simulation, and generate the `OVERFLOW` event. The
// previous creation of descendants and files *should* now be
Expand All @@ -201,10 +219,7 @@ void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, Int
watch.handleEvent(overflow);

if (whichFiles != Approximation.NONE) { // Auto-handler is configured
for (var descendant : descendants) {
awaitCreation.accept(descendant);
awaitCreation.accept(descendant.resolve(file1));
}
awaitCreation.accept(missedCreates);
} else {
// Give the watch some time to process the `OVERFLOW` event and
// do internal bookkeeping
Expand All @@ -213,13 +228,14 @@ void overflowsAreRecoveredFrom(Approximation whichFiles) throws IOException, Int

// Create more files. They *should* be observed (regardless of
// whether an auto-handler for `OVERFLOW` events is configured).
var file2 = Path.of("file2.txt");
for (var descendant : descendants) {
Files.createFile(parent.resolve(descendant).resolve(file2));
}
for (var descendant : descendants) {
awaitCreation.accept(descendant.resolve(file2));
var moreFiles = descendants.stream()
.map(d -> d.resolve(Path.of("file2.txt")))
.collect(Collectors.toList());

for (var f : moreFiles) {
Files.createFile(parent.resolve(f));
}
awaitCreation.accept(moreFiles);
}
}
}
26 changes: 14 additions & 12 deletions src/test/java/engineering/swat/watch/SingleDirectoryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static engineering.swat.watch.WatchEvent.Kind.DELETED;
import static engineering.swat.watch.WatchEvent.Kind.MODIFIED;
import static engineering.swat.watch.WatchEvent.Kind.OVERFLOW;
import static org.awaitility.Awaitility.await;
import static engineering.swat.watch.util.WaitFor.await;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -45,6 +45,7 @@

import engineering.swat.watch.WatchEvent.Kind;
import engineering.swat.watch.impl.EventHandlingWatch;
import engineering.swat.watch.util.WaitFor;

class SingleDirectoryTests {
private TestDirectory testDir;
Expand All @@ -63,7 +64,7 @@ void cleanup() {

@BeforeAll
static void setupEverything() {
Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT);
WaitFor.setDefaultTimeout(TestHelper.NORMAL_WAIT);
}

@Test
Expand All @@ -85,12 +86,12 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException {
// Delete the file
Files.delete(target);
await("File deletion should generate delete event")
.untilTrue(seenDelete);
.until(seenDelete);

// Re-create it again
Files.writeString(target, "Hello World");
await("File creation should generate create event")
.untilTrue(seenCreate);
.until(seenCreate);
}
}

Expand All @@ -116,12 +117,12 @@ public void onDeleted(WatchEvent ev) {
// Delete the file
Files.delete(target);
await("File deletion should generate delete event")
.untilTrue(seenDelete);
.until(seenDelete);

// Re-create it again
Files.writeString(target, "Hello World");
await("File creation should generate create event")
.untilTrue(seenCreate);
.until(seenCreate);
}
}

Expand Down Expand Up @@ -181,22 +182,23 @@ void indexingRescanOnOverflow() throws IOException, InterruptedException {
// Perform some file operations (after a short wait to ensure a new
// last-modified-time). No events should be observed (because the
// overflow simulation is running).
Thread.sleep(TestHelper.SHORT_WAIT.toMillis());
Thread.sleep(100);
Files.writeString(directory.resolve("a.txt"), "foo");
Files.writeString(directory.resolve("b.txt"), "bar");
Files.delete(directory.resolve("c.txt"));
Files.createFile(directory.resolve("d.txt"));

await("No events should have been triggered")
.pollDelay(TestHelper.SHORT_WAIT)
.until(() -> bookkeeper.events().none());
.time(TestHelper.SMALL_WAIT)
.holds(() -> bookkeeper.events().none());

// End overflow simulation, and generate an `OVERFLOW` event.
// Synthetic events should now be issued and observed.
dropEvents.set(false);
var overflow = new WatchEvent(WatchEvent.Kind.OVERFLOW, directory);
((EventHandlingWatch) watch).handleEvent(overflow);


for (var e : new WatchEvent[] {
new WatchEvent(MODIFIED, directory, Path.of("a.txt")),
new WatchEvent(MODIFIED, directory, Path.of("b.txt")),
Expand All @@ -211,7 +213,7 @@ void indexingRescanOnOverflow() throws IOException, InterruptedException {

// Perform some more file operations. All events should be observed
// (because the overflow simulation is no longer running).
Thread.sleep(TestHelper.SHORT_WAIT.toMillis());
Thread.sleep(500);
Files.delete(directory.resolve("a.txt"));
Files.writeString(directory.resolve("b.txt"), "baz");
Files.createFile(directory.resolve("c.txt"));
Expand All @@ -232,8 +234,8 @@ void indexingRescanOnOverflow() throws IOException, InterruptedException {
((EventHandlingWatch) watch).handleEvent(overflow);

await("No events should have been triggered")
.pollDelay(TestHelper.SHORT_WAIT)
.until(() -> bookkeeper.events().kindNot(OVERFLOW).none());
.time(TestHelper.SMALL_WAIT)
.holds(() -> bookkeeper.events().kindNot(OVERFLOW).none());
}
}
}
Loading
Loading