mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Allow BrambleTestCase to handle background thread exceptions gracefully during after()
This commit is contained in:
@@ -4,31 +4,37 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.lang.Thread.UncaughtExceptionHandler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public abstract class BrambleTestCase {
|
||||
|
||||
private volatile boolean exceptionInBackgroundThread = false;
|
||||
private static final Logger LOG =
|
||||
getLogger(BrambleTestCase.class.getName());
|
||||
|
||||
protected volatile boolean exceptionInBackgroundThread = false;
|
||||
|
||||
public BrambleTestCase() {
|
||||
// Ensure exceptions thrown on worker threads cause tests to fail
|
||||
UncaughtExceptionHandler fail = (thread, throwable) -> {
|
||||
throwable.printStackTrace();
|
||||
LOG.log(Level.WARNING, "Caught unhandled exception", throwable);
|
||||
exceptionInBackgroundThread = true;
|
||||
};
|
||||
Thread.setDefaultUncaughtExceptionHandler(fail);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
public void beforeBrambleTestCase() {
|
||||
exceptionInBackgroundThread = false;
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
public void afterBrambleTestCase() {
|
||||
if (exceptionInBackgroundThread) {
|
||||
fail("background thread has thrown an exception");
|
||||
fail("background thread has thrown an exception unexpectedly");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -27,7 +28,9 @@ public class ThreadExceptionTest extends BrambleTestCase {
|
||||
System.out.println("interrupted while joining thread");
|
||||
fail();
|
||||
}
|
||||
|
||||
Assert.assertTrue(exceptionInBackgroundThread);
|
||||
exceptionInBackgroundThread = false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user