Don't warn about background exceptions unless one was thrown.

This commit is contained in:
akwizgran
2022-03-30 16:54:11 +01:00
parent 101ffa2f08
commit 7f6b31d36c

View File

@@ -4,11 +4,11 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
public abstract class BrambleTestCase { public abstract class BrambleTestCase {
@@ -22,7 +22,7 @@ public abstract class BrambleTestCase {
public BrambleTestCase() { public BrambleTestCase() {
// Ensure exceptions thrown on worker threads cause tests to fail // Ensure exceptions thrown on worker threads cause tests to fail
UncaughtExceptionHandler fail = (thread, throwable) -> { UncaughtExceptionHandler fail = (thread, throwable) -> {
LOG.log(Level.WARNING, "Caught unhandled exception", throwable); LOG.log(WARNING, "Caught unhandled exception", throwable);
exceptionInBackgroundThread = throwable; exceptionInBackgroundThread = throwable;
}; };
Thread.setDefaultUncaughtExceptionHandler(fail); Thread.setDefaultUncaughtExceptionHandler(fail);
@@ -36,7 +36,11 @@ public abstract class BrambleTestCase {
@After @After
public void afterBrambleTestCase() { public void afterBrambleTestCase() {
Throwable thrown = exceptionInBackgroundThread; Throwable thrown = exceptionInBackgroundThread;
LOG.warning("background thread has thrown an exception unexpectedly"); if (thrown != null) {
if (thrown != null) throw new AssertionError(thrown); LOG.log(WARNING,
"Background thread has thrown an exception unexpectedly",
thrown);
throw new AssertionError(thrown);
}
} }
} }