Include background exception in test failure report.

This commit is contained in:
akwizgran
2022-03-28 14:59:01 +01:00
parent 32b62d3e30
commit 288f3331ec
2 changed files with 14 additions and 9 deletions

View File

@@ -7,34 +7,39 @@ import java.lang.Thread.UncaughtExceptionHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import static java.util.logging.Logger.getLogger;
import static org.junit.Assert.fail;
public abstract class BrambleTestCase {
private static final Logger LOG =
getLogger(BrambleTestCase.class.getName());
protected volatile boolean exceptionInBackgroundThread = false;
@Nullable
protected volatile Throwable exceptionInBackgroundThread = null;
public BrambleTestCase() {
// Ensure exceptions thrown on worker threads cause tests to fail
UncaughtExceptionHandler fail = (thread, throwable) -> {
LOG.log(Level.WARNING, "Caught unhandled exception", throwable);
exceptionInBackgroundThread = true;
exceptionInBackgroundThread = throwable;
};
Thread.setDefaultUncaughtExceptionHandler(fail);
}
@Before
public void beforeBrambleTestCase() {
exceptionInBackgroundThread = false;
exceptionInBackgroundThread = null;
}
@After
public void afterBrambleTestCase() {
if (exceptionInBackgroundThread) {
fail("background thread has thrown an exception unexpectedly");
Throwable thrown = exceptionInBackgroundThread;
if (thrown != null) {
throw new AssertionError(
"background thread has thrown an exception unexpectedly",
thrown);
}
}
}

View File

@@ -1,8 +1,8 @@
package org.briarproject.bramble.test;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
public class ThreadExceptionTest extends BrambleTestCase {
@@ -29,8 +29,8 @@ public class ThreadExceptionTest extends BrambleTestCase {
fail();
}
Assert.assertTrue(exceptionInBackgroundThread);
exceptionInBackgroundThread = false;
assertNotNull(exceptionInBackgroundThread);
exceptionInBackgroundThread = null;
}
}