Skip to content

Commit

Permalink
Use longer timeouts to stabilize flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Nov 1, 2021
1 parent 718d34f commit cd257bd
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -37,6 +38,8 @@
*/
class AssertTimeoutAssertionsTests {

private static final Duration PREEMPTIVE_TIMEOUT = ofMillis(1000);

private static ThreadLocal<AtomicBoolean> changed = ThreadLocal.withInitial(() -> new AtomicBoolean(false));

private final Executable nix = () -> {
Expand Down Expand Up @@ -189,26 +192,29 @@ void assertTimeoutPreemptivelyForExecutableThatThrowsAnAssertionFailedError() {
@Test
void assertTimeoutPreemptivelyForExecutableThatCompletesAfterTheTimeout() {
AssertionFailedError error = assertThrows(AssertionFailedError.class,
() -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt));
assertMessageEquals(error, "execution timed out after 10 ms");
() -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt));
assertMessageEquals(error, "execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
}

@Test
void assertTimeoutPreemptivelyWithMessageForExecutableThatCompletesAfterTheTimeout() {
AssertionFailedError error = assertThrows(AssertionFailedError.class,
() -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt, "Tempus Fugit"));
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");
() -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt, "Tempus Fugit"));
assertMessageEquals(error,
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
}

@Test
void assertTimeoutPreemptivelyWithMessageSupplierForExecutableThatCompletesAfterTheTimeout() {
AssertionFailedError error = assertThrows(AssertionFailedError.class,
() -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt, () -> "Tempus" + " " + "Fugit"));
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");
() -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt,
() -> "Tempus" + " " + "Fugit"));
assertMessageEquals(error,
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
}
Expand Down Expand Up @@ -258,38 +264,43 @@ void assertTimeoutPreemptivelyForSupplierThatThrowsAnAssertionFailedError() {
@Test
void assertTimeoutPreemptivelyForSupplierThatCompletesAfterTheTimeout() {
AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
assertTimeoutPreemptively(ofMillis(10), () -> {
assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> {
waitForInterrupt();
return "Tempus Fugit";
});
});
assertMessageEquals(error, "execution timed out after 10 ms");

assertMessageEquals(error, "execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
}

@Test
void assertTimeoutPreemptivelyWithMessageForSupplierThatCompletesAfterTheTimeout() {
AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
assertTimeoutPreemptively(ofMillis(10), () -> {
assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> {
waitForInterrupt();
return "Tempus Fugit";
}, "Tempus Fugit");
});
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");

assertMessageEquals(error,
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
}

@Test
void assertTimeoutPreemptivelyWithMessageSupplierForSupplierThatCompletesAfterTheTimeout() {
AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
assertTimeoutPreemptively(ofMillis(10), () -> {
assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> {
waitForInterrupt();
return "Tempus Fugit";
}, () -> "Tempus" + " " + "Fugit");
});
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms");

assertMessageEquals(error,
"Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms");
assertMessageStartsWith(error.getCause(), "Execution timed out in ");
assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
}
Expand Down

0 comments on commit cd257bd

Please sign in to comment.