mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Include background exception in test failure report.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user