Skip to content

Commit 28b9cdd

Browse files
committed
Rewrote more test to new framework and joined waits together
1 parent 5663257 commit 28b9cdd

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import static engineering.swat.watch.WatchEvent.Kind.DELETED;
3131
import static engineering.swat.watch.WatchEvent.Kind.MODIFIED;
3232
import static engineering.swat.watch.WatchEvent.Kind.OVERFLOW;
33-
import static org.awaitility.Awaitility.await;
33+
import static engineering.swat.watch.util.WaitFor.await;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
3435

3536
import java.io.IOException;
3637
import java.nio.file.Files;
3738
import java.nio.file.attribute.FileTime;
3839
import java.time.Instant;
3940
import java.util.concurrent.atomic.AtomicBoolean;
41+
import java.util.function.BooleanSupplier;
4042

4143
import org.awaitility.Awaitility;
4244
import org.junit.jupiter.api.AfterEach;
@@ -45,6 +47,7 @@
4547
import org.junit.jupiter.api.Test;
4648

4749
import engineering.swat.watch.impl.EventHandlingWatch;
50+
import engineering.swat.watch.util.WaitFor;
4851

4952
class SingleFileTests {
5053
private TestDirectory testDir;
@@ -63,7 +66,7 @@ void cleanup() {
6366

6467
@BeforeAll
6568
static void setupEverything() {
66-
Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT);
69+
WaitFor.setDefaultTimeout(TestHelper.NORMAL_WAIT);
6770
}
6871

6972
@Test
@@ -86,12 +89,11 @@ void singleFileShouldNotTriggerOnOtherFilesInSameDir() throws IOException, Inter
8689
Files.writeString(f, "Hello");
8790
}
8891
}
89-
Thread.sleep(TestHelper.SHORT_WAIT.toMillis());
92+
Thread.sleep(TestHelper.SMALL_WAIT.toMillis());
9093
Files.writeString(target, "Hello world");
9194
await("Single file does trigger")
92-
.pollDelay(TestHelper.NORMAL_WAIT.minusMillis(10))
93-
.failFast("No others should be notified", others::get)
94-
.untilTrue(seen);
95+
.failFast("Other files should not trigger", others::get)
96+
.until(seen);
9597
}
9698
}
9799

@@ -115,41 +117,50 @@ void singleFileThatMonitorsOnlyADirectory() throws IOException, InterruptedExcep
115117
Files.writeString(f, "Hello");
116118
}
117119
}
118-
Thread.sleep(TestHelper.SHORT_WAIT.toMillis());
120+
Thread.sleep(TestHelper.SMALL_WAIT.toMillis());
119121
Files.setLastModifiedTime(target, FileTime.from(Instant.now()));
120122
await("Single directory does trigger")
121-
.pollDelay(TestHelper.NORMAL_WAIT.minusMillis(10))
122123
.failFast("No others should be notified", others::get)
123-
.untilTrue(seen);
124+
.until(seen);
124125
}
125126
}
126127

127128
@Test
128129
void noRescanOnOverflow() throws IOException, InterruptedException {
129130
var bookkeeper = new TestHelper.Bookkeeper();
130131
try (var watch = startWatchAndTriggerOverflow(Approximation.NONE, bookkeeper)) {
131-
Thread.sleep(TestHelper.SHORT_WAIT.toMillis());
132+
//Thread.sleep(TestHelper.SMALL_WAIT.toMillis());
132133

133-
await("Overflow shouldn't trigger created, modified, or deleted events: " + bookkeeper)
134-
.until(() -> bookkeeper.events().kind(CREATED, MODIFIED, DELETED).none());
135134
await("Overflow should be visible to user-defined event handler")
136135
.until(() -> bookkeeper.events().kind(OVERFLOW).any());
136+
await("Overflow shouldn't trigger created, modified, or deleted events: " + bookkeeper)
137+
.time(TestHelper.SMALL_WAIT)
138+
.holds(() -> bookkeeper.events().kind(CREATED, MODIFIED, DELETED).none());
137139
}
138140
}
139141

140142
@Test
141143
void memorylessRescanOnOverflow() throws IOException, InterruptedException {
142144
var bookkeeper = new TestHelper.Bookkeeper();
143145
try (var watch = startWatchAndTriggerOverflow(Approximation.ALL, bookkeeper)) {
144-
Thread.sleep(TestHelper.SHORT_WAIT.toMillis());
146+
//Thread.sleep(TestHelper.SMALL_WAIT.toMillis());
145147

146148
var path = watch.getPath();
147149
await("Overflow should trigger created event for `" + path + "`")
148150
.until(() -> bookkeeper.events().kind(CREATED).rootPath(path).any());
149-
await("Overflow shouldn't trigger created events for other files")
150-
.until(() -> bookkeeper.events().kind(CREATED).rootPathNot(path).none());
151-
await("Overflow shouldn't trigger modified or deleted events")
152-
.until(() -> bookkeeper.events().kind(MODIFIED, DELETED).none());
151+
await("No extra events")
152+
.time(TestHelper.SMALL_WAIT)
153+
.holds(() -> {
154+
assertTrue(
155+
bookkeeper.events().kind(CREATED).rootPathNot(path).none(),
156+
"Overflow shouldn't trigger created events for other files"
157+
);
158+
assertTrue(
159+
bookkeeper.events().kind(MODIFIED, DELETED).none()
160+
,"Overflow shouldn't trigger modified or deleted events"
161+
);
162+
return true;
163+
});
153164
await("Overflow should be visible to user-defined event handler")
154165
.until(() -> bookkeeper.events().kind(OVERFLOW).any());
155166
}

src/test/java/engineering/swat/watch/util/WaitFor.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static void setDefaultTimeout(Duration newDefaultTime) {
3030
private @Nullable BooleanSupplier failFast;
3131
private @Nullable Duration time;
3232
private @Nullable Duration poll;
33+
private @Nullable Supplier<String> failMessage;
3334

3435

3536
private WaitFor(Supplier<String> message) {
@@ -54,26 +55,38 @@ public WaitFor pollInterval(Duration d) {
5455
return this;
5556
}
5657

57-
public WaitFor failFast(BooleanSupplier b) {
58+
public WaitFor failFast(String message, BooleanSupplier b) {
59+
return failFast(() -> message, b);
60+
}
61+
62+
public WaitFor failFast(Supplier<String> message, BooleanSupplier b) {
5863
this.failFast = b;
64+
this.failMessage = message;
5965
return this;
6066
}
6167

62-
public WaitFor failFast(AtomicBoolean b) {
63-
return failFast(b::get);
68+
public WaitFor failFast(Supplier<String> message, AtomicBoolean b) {
69+
return failFast(message, b::get);
70+
}
71+
public WaitFor failFast(String message, AtomicBoolean b) {
72+
return failFast(message, b::get);
6473
}
6574

6675

6776

68-
private boolean alreadyFailed() {
77+
private void checkFailed() {
6978
if (failFast != null) {
7079
try {
71-
return failFast.getAsBoolean();
80+
if (failFast.getAsBoolean()) {
81+
var actualFailMessage = failMessage;
82+
if (actualFailMessage == null) {
83+
actualFailMessage = () -> this.message.get() + " was terminated earlier due to fail fast";
84+
}
85+
fail(actualFailMessage);
86+
}
7287
} catch (RuntimeException ex) {
73-
return false;
7488
}
7589
}
76-
return false;
7790
}
7891

7992
private Duration calculatePoll(Duration time) {
@@ -112,12 +125,11 @@ private boolean block(BooleanSupplier action) {
112125
var end = currentTimeMillis() + time.toMillis();
113126
while (end > currentTimeMillis()) {
114127
var start = currentTimeMillis();
128+
checkFailed();
115129
if (action.getAsBoolean()) {
116130
return true;
117131
}
118-
if (alreadyFailed()) {
119-
fail(message.get() + " was terminated by failFast predicate");
120-
}
132+
checkFailed();
121133
var stop = currentTimeMillis();
122134
var remaining = poll.toMillis() - (stop - start);
123135
if (remaining > 0) {

0 commit comments

Comments
 (0)