Skip to content

Commit 5663257

Browse files
committed
Another test made 3x faster
1 parent 39e1f43 commit 5663257

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
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;
3434

3535
import java.io.IOException;
3636
import java.nio.file.Files;
@@ -45,6 +45,7 @@
4545

4646
import engineering.swat.watch.WatchEvent.Kind;
4747
import engineering.swat.watch.impl.EventHandlingWatch;
48+
import engineering.swat.watch.util.WaitFor;
4849

4950
class SingleDirectoryTests {
5051
private TestDirectory testDir;
@@ -63,7 +64,7 @@ void cleanup() {
6364

6465
@BeforeAll
6566
static void setupEverything() {
66-
Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT);
67+
WaitFor.setDefaultTimeout(TestHelper.NORMAL_WAIT);
6768
}
6869

6970
@Test
@@ -85,12 +86,12 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException {
8586
// Delete the file
8687
Files.delete(target);
8788
await("File deletion should generate delete event")
88-
.untilTrue(seenDelete);
89+
.until(seenDelete);
8990

9091
// Re-create it again
9192
Files.writeString(target, "Hello World");
9293
await("File creation should generate create event")
93-
.untilTrue(seenCreate);
94+
.until(seenCreate);
9495
}
9596
}
9697

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

121122
// Re-create it again
122123
Files.writeString(target, "Hello World");
123124
await("File creation should generate create event")
124-
.untilTrue(seenCreate);
125+
.until(seenCreate);
125126
}
126127
}
127128

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

190191
await("No events should have been triggered")
191-
.pollDelay(TestHelper.SHORT_WAIT)
192-
.until(() -> bookkeeper.events().none());
192+
.time(TestHelper.SMALL_WAIT)
193+
.holds(() -> bookkeeper.events().none());
193194

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

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

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

234236
await("No events should have been triggered")
235-
.pollDelay(TestHelper.SHORT_WAIT)
236-
.until(() -> bookkeeper.events().kindNot(OVERFLOW).none());
237+
.time(TestHelper.SMALL_WAIT)
238+
.holds(() -> bookkeeper.events().kindNot(OVERFLOW).none());
237239
}
238240
}
239241
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
public class TestHelper {
4141

4242
public static final Duration TINY_WAIT;
43+
public static final Duration SMALL_WAIT;
4344
public static final Duration SHORT_WAIT;
4445
public static final Duration NORMAL_WAIT;
4546
public static final Duration LONG_WAIT;
@@ -59,6 +60,7 @@ else if (os.contains("win")) {
5960
delayFactor *= 4;
6061
}
6162
TINY_WAIT = Duration.ofMillis(250 * delayFactor);
63+
SMALL_WAIT = TINY_WAIT.multipliedBy(2);
6264
SHORT_WAIT = Duration.ofSeconds(1 * delayFactor);
6365
NORMAL_WAIT = Duration.ofSeconds(4 * delayFactor);
6466
LONG_WAIT = Duration.ofSeconds(8 * delayFactor);

0 commit comments

Comments
 (0)