Skip to content

Commit 0979b15

Browse files
committed
Add parameterization to torture tests
1 parent f5bb9a3 commit 0979b15

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
package engineering.swat.watch;
2828

2929
import java.time.Duration;
30+
import java.util.Arrays;
31+
import java.util.stream.IntStream;
32+
import java.util.stream.Stream;
3033

3134
public class TestHelper {
3235

@@ -54,4 +57,21 @@ else if (os.contains("win")) {
5457
NORMAL_WAIT = Duration.ofSeconds(4 * delayFactor);
5558
LONG_WAIT = Duration.ofSeconds(8 * delayFactor);
5659
}
60+
61+
public static <T> Stream<T> streamOf(T[] values, int repetitions) {
62+
return streamOf(values, repetitions, false);
63+
}
64+
65+
public static <T> Stream<T> streamOf(T[] values, int repetitions, boolean sortByRepetition) {
66+
if (sortByRepetition) {
67+
return IntStream
68+
.range(0, repetitions)
69+
.boxed()
70+
.flatMap(i -> Arrays.stream(values));
71+
}
72+
else { // Sort by value
73+
return Arrays.stream(values).flatMap(v ->
74+
IntStream.range(0, repetitions).mapToObj(i -> v));
75+
}
76+
}
5777
}

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@
4646
import java.util.concurrent.TimeUnit;
4747
import java.util.concurrent.atomic.AtomicInteger;
4848
import java.util.function.Predicate;
49+
import java.util.stream.Stream;
4950

5051
import org.apache.logging.log4j.LogManager;
5152
import org.apache.logging.log4j.Logger;
5253
import org.awaitility.Awaitility;
5354
import org.junit.jupiter.api.AfterEach;
5455
import org.junit.jupiter.api.BeforeAll;
5556
import org.junit.jupiter.api.BeforeEach;
56-
import org.junit.jupiter.api.RepeatedTest;
57-
import org.junit.jupiter.api.Test;
5857
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
58+
import org.junit.jupiter.params.ParameterizedTest;
59+
import org.junit.jupiter.params.provider.EnumSource;
60+
import org.junit.jupiter.params.provider.MethodSource;
5961

6062
class TortureTests {
6163

@@ -141,17 +143,18 @@ Set<Path> stop() throws InterruptedException {
141143

142144
private static final int THREADS = 4;
143145

144-
@Test
145-
void pressureOnFSShouldNotMissNewFilesAnything() throws InterruptedException, IOException {
146+
@ParameterizedTest
147+
@EnumSource(names = { "ALL", "DIRTY" })
148+
void pressureOnFSShouldNotMissNewFilesAnything(OnOverflow whichFiles) throws InterruptedException, IOException {
146149
final var root = testDir.getTestDirectory();
147150
var pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 4);
148151

149152
var io = new IOGenerator(THREADS, root, pool);
150153

151-
152154
var seenCreates = ConcurrentHashMap.<Path>newKeySet();
153155
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
154156
.withExecutor(pool)
157+
.approximate(whichFiles)
155158
.on(ev -> {
156159
var fullPath = ev.calculateFullPath();
157160
switch (ev.getKind()) {
@@ -206,8 +209,14 @@ void pressureOnFSShouldNotMissNewFilesAnything() throws InterruptedException, IO
206209

207210
private final int TORTURE_REGISTRATION_THREADS = THREADS * 500;
208211

209-
@RepeatedTest(failureThreshold=1, value = 20)
210-
void manyRegistrationsForSamePath() throws InterruptedException, IOException {
212+
static Stream<OnOverflow> manyRegistrationsForSamePathSource() {
213+
OnOverflow[] values = { OnOverflow.ALL, OnOverflow.DIRTY };
214+
return TestHelper.streamOf(values, 5);
215+
}
216+
217+
@ParameterizedTest
218+
@MethodSource("manyRegistrationsForSamePathSource")
219+
void manyRegistrationsForSamePath(OnOverflow whichFiles) throws InterruptedException, IOException {
211220
var startRegistering = new Semaphore(0);
212221
var startedWatching = new Semaphore(0);
213222
var startDeregistring = new Semaphore(0);
@@ -220,6 +229,7 @@ void manyRegistrationsForSamePath() throws InterruptedException, IOException {
220229
try {
221230
var watcher = Watcher
222231
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
232+
.approximate(whichFiles)
223233
.on(e -> seen.add(e.calculateFullPath()));
224234
startRegistering.acquire();
225235
try (var c = watcher.start()) {
@@ -263,8 +273,14 @@ void manyRegistrationsForSamePath() throws InterruptedException, IOException {
263273
}
264274
}
265275

266-
@RepeatedTest(failureThreshold=1, value = 20)
267-
void manyRegisterAndUnregisterSameTime() throws InterruptedException, IOException {
276+
static Stream<OnOverflow> manyRegisterAndUnregisterSameTimeSource() {
277+
OnOverflow[] values = { OnOverflow.ALL, OnOverflow.DIRTY };
278+
return TestHelper.streamOf(values, 5);
279+
}
280+
281+
@ParameterizedTest
282+
@MethodSource("manyRegisterAndUnregisterSameTimeSource")
283+
void manyRegisterAndUnregisterSameTime(OnOverflow whichFiles) throws InterruptedException, IOException {
268284
var startRegistering = new Semaphore(0);
269285
var startedWatching = new Semaphore(0);
270286
var stopAll = new Semaphore(0);
@@ -286,6 +302,7 @@ void manyRegisterAndUnregisterSameTime() throws InterruptedException, IOExceptio
286302
for (int k = 0; k < 1000; k++) {
287303
var watcher = Watcher
288304
.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN)
305+
.approximate(whichFiles)
289306
.on(e -> {
290307
if (e.calculateFullPath().equals(target)) {
291308
seen.add(id);
@@ -328,13 +345,13 @@ void manyRegisterAndUnregisterSameTime() throws InterruptedException, IOExceptio
328345
finally {
329346
stopAll.release(amountOfWatchersActive);
330347
}
331-
332348
}
333349

334-
@Test
350+
@ParameterizedTest
351+
@EnumSource(names = { "ALL", "DIRTY" })
335352
//Deletes can race the filesystem, so you might miss a few files in a dir, if that dir is already deleted
336353
@EnabledIfEnvironmentVariable(named="TORTURE_DELETE", matches="true")
337-
void pressureOnFSShouldNotMissDeletes() throws InterruptedException, IOException {
354+
void pressureOnFSShouldNotMissDeletes(OnOverflow whichFiles) throws InterruptedException, IOException {
338355
final var root = testDir.getTestDirectory();
339356
var pool = Executors.newCachedThreadPool();
340357

@@ -350,6 +367,7 @@ void pressureOnFSShouldNotMissDeletes() throws InterruptedException, IOException
350367
final var happened = new Semaphore(0);
351368
var watchConfig = Watcher.watch(testDir.getTestDirectory(), WatchScope.PATH_AND_ALL_DESCENDANTS)
352369
.withExecutor(pool)
370+
.approximate(whichFiles)
353371
.on(ev -> {
354372
events.getAndIncrement();
355373
happened.release();
@@ -393,8 +411,6 @@ void pressureOnFSShouldNotMissDeletes() throws InterruptedException, IOException
393411
}
394412
}
395413

396-
397-
398414
private void waitForStable(final AtomicInteger events, final Semaphore happened) throws InterruptedException {
399415
int lastEventCount = events.get();
400416
int stableCount = 0;

0 commit comments

Comments
 (0)