mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Make BrambleTestCase fail if background thread throws an exception
This commit is contained in:
@@ -1,17 +1,34 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.lang.Thread.UncaughtExceptionHandler;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public abstract class BrambleTestCase {
|
||||
|
||||
private volatile boolean exceptionInBackgroundThread = false;
|
||||
|
||||
public BrambleTestCase() {
|
||||
// Ensure exceptions thrown on worker threads cause tests to fail
|
||||
UncaughtExceptionHandler fail = (thread, throwable) -> {
|
||||
throwable.printStackTrace();
|
||||
fail();
|
||||
exceptionInBackgroundThread = true;
|
||||
};
|
||||
Thread.setDefaultUncaughtExceptionHandler(fail);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
exceptionInBackgroundThread = false;
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
if (exceptionInBackgroundThread) {
|
||||
fail("background thread has thrown an exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public class ThreadExceptionTest extends BrambleTestCase {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
@Test
|
||||
public void testExceptionInThreadMakesTestCaseFail() {
|
||||
Thread t = new Thread(() -> {
|
||||
System.out.println("thread before exception");
|
||||
@@ -24,7 +24,8 @@ public class ThreadExceptionTest extends BrambleTestCase {
|
||||
t.join();
|
||||
System.out.println("joined thread");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("interrupted while joining thread");
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user