mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 06:39:54 +01:00
Merge branch 'exceptions-on-thread-in-bramble-testcases' into 'master'
Exceptions on thread in bramble testcases See merge request briar/briar!1584
This commit is contained in:
@@ -1,17 +1,42 @@
|
|||||||
package org.briarproject.bramble.test;
|
package org.briarproject.bramble.test;
|
||||||
|
|
||||||
import java.lang.Thread.UncaughtExceptionHandler;
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
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;
|
||||||
|
|
||||||
public abstract class BrambleTestCase {
|
public abstract class BrambleTestCase {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(BrambleTestCase.class.getName());
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
protected volatile Throwable exceptionInBackgroundThread = null;
|
||||||
|
|
||||||
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) -> {
|
||||||
throwable.printStackTrace();
|
LOG.log(Level.WARNING, "Caught unhandled exception", throwable);
|
||||||
fail();
|
exceptionInBackgroundThread = throwable;
|
||||||
};
|
};
|
||||||
Thread.setDefaultUncaughtExceptionHandler(fail);
|
Thread.setDefaultUncaughtExceptionHandler(fail);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void beforeBrambleTestCase() {
|
||||||
|
exceptionInBackgroundThread = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void afterBrambleTestCase() {
|
||||||
|
Throwable thrown = exceptionInBackgroundThread;
|
||||||
|
LOG.warning("background thread has thrown an exception unexpectedly");
|
||||||
|
if (thrown != null) throw new AssertionError(thrown);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.briarproject.bramble.test;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
public class ThreadExceptionTest extends BrambleTestCase {
|
||||||
|
|
||||||
|
@Test(expected = AssertionError.class)
|
||||||
|
public void testAssertionErrorMakesTestCaseFail() {
|
||||||
|
// This is what BrambleTestCase does, too:
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExceptionInThreadMakesTestCaseFail() {
|
||||||
|
Thread t = new Thread(() -> {
|
||||||
|
System.out.println("thread before exception");
|
||||||
|
throw new RuntimeException("boom");
|
||||||
|
});
|
||||||
|
|
||||||
|
t.start();
|
||||||
|
try {
|
||||||
|
t.join();
|
||||||
|
System.out.println("joined thread");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("interrupted while joining thread");
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNotNull(exceptionInBackgroundThread);
|
||||||
|
exceptionInBackgroundThread = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -986,6 +986,10 @@ public class IntroductionIntegrationTest
|
|||||||
@Test
|
@Test
|
||||||
public void testIntroduceesRemovedCleanup() throws Exception {
|
public void testIntroduceesRemovedCleanup() throws Exception {
|
||||||
addListeners(true, true);
|
addListeners(true, true);
|
||||||
|
// The second introducee shouldn't respond to the introduction
|
||||||
|
// otherwise there would be a race between the response to the REQUEST
|
||||||
|
// and the delivery of the ABORT
|
||||||
|
listener2.answerRequests = false;
|
||||||
|
|
||||||
// make introduction
|
// make introduction
|
||||||
introductionManager0
|
introductionManager0
|
||||||
|
|||||||
Reference in New Issue
Block a user