Skip to content

Commit d01f47c

Browse files
committed
Also made the torture test a bit faster to run
1 parent 28b9cdd commit d01f47c

2 files changed

Lines changed: 69 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
package engineering.swat.watch;
2828

29-
import static org.awaitility.Awaitility.await;
29+
import static engineering.swat.watch.util.WaitFor.await;
3030
import static org.junit.jupiter.api.Assertions.assertTrue;
3131
import static org.junit.jupiter.api.Assertions.fail;
3232

@@ -45,12 +45,10 @@
4545
import java.util.concurrent.Semaphore;
4646
import java.util.concurrent.TimeUnit;
4747
import java.util.concurrent.atomic.AtomicInteger;
48-
import java.util.function.Predicate;
4948
import java.util.stream.Stream;
5049

5150
import org.apache.logging.log4j.LogManager;
5251
import org.apache.logging.log4j.Logger;
53-
import org.awaitility.Awaitility;
5452
import org.junit.jupiter.api.AfterEach;
5553
import org.junit.jupiter.api.BeforeAll;
5654
import org.junit.jupiter.api.BeforeEach;
@@ -60,6 +58,8 @@
6058
import org.junit.jupiter.params.provider.EnumSource;
6159
import org.junit.jupiter.params.provider.MethodSource;
6260

61+
import engineering.swat.watch.util.WaitFor;
62+
6363
class TortureTests {
6464

6565
private final Logger logger = LogManager.getLogger();
@@ -68,7 +68,7 @@ class TortureTests {
6868

6969
@BeforeAll
7070
static void setupEverything() {
71-
Awaitility.setDefaultTimeout(TestHelper.LONG_WAIT.getSeconds(), TimeUnit.SECONDS);
71+
WaitFor.setDefaultTimeout(TestHelper.LONG_WAIT);
7272
}
7373

7474
@BeforeEach
@@ -181,13 +181,13 @@ void pressureOnFSShouldNotMissNewFilesAnything(Approximation whichFiles) throws
181181
logger.info("Starting {} jobs", THREADS);
182182
io.start();
183183
// now we generate a whole bunch of events
184-
Thread.sleep(TestHelper.NORMAL_WAIT.toMillis());
184+
Thread.sleep(Math.min(TestHelper.NORMAL_WAIT.toMillis(), Duration.ofSeconds(30).toMillis()));
185185
logger.info("Stopping jobs");
186186
pathsWritten = io.stop();
187187
logger.info("Generated: {} files", pathsWritten.size());
188188

189189
await("After a while we should have seen all the create events")
190-
.timeout(TestHelper.LONG_WAIT.multipliedBy(50))
190+
.time(TestHelper.LONG_WAIT.multipliedBy(50))
191191
.pollInterval(Duration.ofMillis(500))
192192
.until(() -> seenCreates.containsAll(pathsWritten));
193193
}
@@ -254,10 +254,10 @@ void manyRegistrationsForSamePath() throws InterruptedException, IOException {
254254
Files.writeString(target, "Hello World");
255255
var expected = Collections.singleton(target);
256256
await("We should see only one event")
257-
.failFast(() -> !exceptions.isEmpty())
258-
.timeout(TestHelper.LONG_WAIT)
257+
.failFast(() -> "There was an exception: "+ exceptions, () -> !exceptions.isEmpty())
258+
.time(TestHelper.NORMAL_WAIT)
259259
.pollInterval(Duration.ofMillis(10))
260-
.until(() -> seen, expected::equals);
260+
.untilEquals(() -> seen, expected);
261261
if (!exceptions.isEmpty()) {
262262
fail(exceptions.pop());
263263
}
@@ -269,7 +269,7 @@ void manyRegistrationsForSamePath() throws InterruptedException, IOException {
269269

270270
static Stream<Approximation> manyRegisterAndUnregisterSameTimeSource() {
271271
Approximation[] values = { Approximation.ALL, Approximation.DIFF };
272-
return TestHelper.streamOf(values, 5);
272+
return TestHelper.streamOf(values, 3);
273273
}
274274

275275
@ParameterizedTest
@@ -328,9 +328,9 @@ void manyRegisterAndUnregisterSameTime(Approximation whichFiles) throws Interrup
328328
assertTrue(seen.isEmpty(), "No events should have been sent");
329329
Files.writeString(target, "Hello World");
330330
await("We should see only exactly the " + amountOfWatchersActive + " events we expect")
331-
.failFast(() -> !exceptions.isEmpty())
332-
.pollDelay(TestHelper.NORMAL_WAIT.minusMillis(100))
333-
.until(seen::size, Predicate.isEqual(amountOfWatchersActive))
331+
.time(TestHelper.SHORT_WAIT)
332+
.failFast(() -> "There was an exception: "+ exceptions, () -> !exceptions.isEmpty())
333+
.delayedHoldsEquals(seen::size, amountOfWatchersActive)
334334
;
335335
if (!exceptions.isEmpty()) {
336336
fail(exceptions.pop());

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
/*
2+
* BSD 2-Clause License
3+
*
4+
* Copyright (c) 2023, Swat.engineering
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
127
package engineering.swat.watch.util;
228

329
import static java.lang.System.currentTimeMillis;
@@ -172,6 +198,11 @@ public void until(BooleanSupplier p) {
172198
assertTrue(block(p), message);
173199
}
174200

201+
public <T> void untilEquals(Supplier<T> val, T expected) {
202+
assertTrue(block(() -> expected.equals(val.get())), message);
203+
assertEquals(expected, val.get(), message);
204+
}
205+
175206
private void holdBlock(BooleanSupplier p) {
176207
// we keep going untill a false result
177208
block(() -> !p.getAsBoolean());
@@ -199,5 +230,30 @@ public void holdsEmpty(Supplier<Stream<?>> stream) {
199230
});
200231
}
201232

233+
private void delayedHoldsBlock(BooleanSupplier p, Runnable finalCheck) {
234+
AtomicBoolean turnedTrue = new AtomicBoolean(false);
235+
block(() -> {
236+
var result = p.getAsBoolean();
237+
if (result) {
238+
turnedTrue.set(true);
239+
// keep running, all good
240+
return false;
241+
}
242+
// now if false, that could be fine, if we're never turned true yet
243+
return turnedTrue.get();
244+
});
245+
finalCheck.run();
246+
}
247+
248+
public void delayedHolds(BooleanSupplier p) {
249+
delayedHoldsBlock(p, () -> assertTrue(p, message));
250+
}
251+
252+
public <T> void delayedHoldsEquals(Supplier<T> sup, T expected) {
253+
delayedHoldsBlock(
254+
() -> expected.equals(sup.get()),
255+
() -> assertEquals(expected, sup.get(), message)
256+
);
257+
}
202258

203259
}

0 commit comments

Comments
 (0)