Skip to content

Commit cd257bd

Browse files
committed
Use longer timeouts to stabilize flaky tests
1 parent 718d34f commit cd257bd

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertTimeoutAssertionsTests.java

+23-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
2323
import static org.junit.jupiter.api.Assertions.fail;
2424

25+
import java.time.Duration;
2526
import java.util.concurrent.CountDownLatch;
2627
import java.util.concurrent.atomic.AtomicBoolean;
2728
import java.util.concurrent.atomic.AtomicReference;
@@ -37,6 +38,8 @@
3738
*/
3839
class AssertTimeoutAssertionsTests {
3940

41+
private static final Duration PREEMPTIVE_TIMEOUT = ofMillis(1000);
42+
4043
private static ThreadLocal<AtomicBoolean> changed = ThreadLocal.withInitial(() -> new AtomicBoolean(false));
4144

4245
private final Executable nix = () -> {
@@ -189,26 +192,29 @@ void assertTimeoutPreemptivelyForExecutableThatThrowsAnAssertionFailedError() {
189192
@Test
190193
void assertTimeoutPreemptivelyForExecutableThatCompletesAfterTheTimeout() {
191194
AssertionFailedError error = assertThrows(AssertionFailedError.class,
192-
() -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt));
193-
assertMessageEquals(error, "execution timed out after 10 ms");
195+
() -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt));
196+
assertMessageEquals(error, "execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
194197
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
195198
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
196199
}
197200

198201
@Test
199202
void assertTimeoutPreemptivelyWithMessageForExecutableThatCompletesAfterTheTimeout() {
200203
AssertionFailedError error = assertThrows(AssertionFailedError.class,
201-
() -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt, "Tempus Fugit"));
202-
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");
204+
() -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt, "Tempus Fugit"));
205+
assertMessageEquals(error,
206+
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
203207
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
204208
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
205209
}
206210

207211
@Test
208212
void assertTimeoutPreemptivelyWithMessageSupplierForExecutableThatCompletesAfterTheTimeout() {
209213
AssertionFailedError error = assertThrows(AssertionFailedError.class,
210-
() -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt, () -> "Tempus" + " " + "Fugit"));
211-
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");
214+
() -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt,
215+
() -> "Tempus" + " " + "Fugit"));
216+
assertMessageEquals(error,
217+
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
212218
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
213219
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
214220
}
@@ -258,38 +264,43 @@ void assertTimeoutPreemptivelyForSupplierThatThrowsAnAssertionFailedError() {
258264
@Test
259265
void assertTimeoutPreemptivelyForSupplierThatCompletesAfterTheTimeout() {
260266
AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
261-
assertTimeoutPreemptively(ofMillis(10), () -> {
267+
assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> {
262268
waitForInterrupt();
263269
return "Tempus Fugit";
264270
});
265271
});
266-
assertMessageEquals(error, "execution timed out after 10 ms");
272+
273+
assertMessageEquals(error, "execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
267274
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
268275
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
269276
}
270277

271278
@Test
272279
void assertTimeoutPreemptivelyWithMessageForSupplierThatCompletesAfterTheTimeout() {
273280
AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
274-
assertTimeoutPreemptively(ofMillis(10), () -> {
281+
assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> {
275282
waitForInterrupt();
276283
return "Tempus Fugit";
277284
}, "Tempus Fugit");
278285
});
279-
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");
286+
287+
assertMessageEquals(error,
288+
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
280289
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
281290
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
282291
}
283292

284293
@Test
285294
void assertTimeoutPreemptivelyWithMessageSupplierForSupplierThatCompletesAfterTheTimeout() {
286295
AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
287-
assertTimeoutPreemptively(ofMillis(10), () -> {
296+
assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> {
288297
waitForInterrupt();
289298
return "Tempus Fugit";
290299
}, () -> "Tempus" + " " + "Fugit");
291300
});
292-
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");
301+
302+
assertMessageEquals(error,
303+
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
293304
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
294305
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
295306
}

0 commit comments

Comments
 (0)