diff --git a/mina-core/src/test/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptorTest.java b/mina-core/src/test/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptorTest.java index cca9be7ca..07d40e482 100644 --- a/mina-core/src/test/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptorTest.java +++ b/mina-core/src/test/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptorTest.java @@ -23,7 +23,11 @@ import org.apache.mina.transport.socket.DatagramSessionConfig; import org.apache.mina.util.AcceptorBindUtil; import org.apache.mina.util.ExceptionMonitor; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -34,14 +38,24 @@ public class NioDatagramAcceptorTest { + private static final Logger LOGGER = LoggerFactory.getLogger(NioDatagramAcceptorTest.class); + + @Rule + public TestName testName = new TestName(); + @Test public void shouldDisposeAcceptorSilentlyWhenExecutorIsShutdown() { AtomicReference exception = new AtomicReference<>(); + ExceptionMonitor previous = ExceptionMonitor.getInstance(); ExceptionMonitor.setInstance(new ExceptionMonitor() { @Override public void exceptionCaught(Throwable cause) { - exception.set(cause); + LOGGER.error("Exception caught in test {}", testName.getMethodName(), cause); + + if (isFromNioDatagramAcceptor(cause)) { + exception.set(cause); + } } }); @@ -61,7 +75,7 @@ public void exceptionCaught(Throwable cause) { assertNull("Exception must not be thrown when disposing executor service", exception.get()); } } finally { - ExceptionMonitor.setInstance(null); + ExceptionMonitor.setInstance(previous); } } @@ -69,10 +83,15 @@ public void exceptionCaught(Throwable cause) { public void shouldThrowExceptionWhenThreadIsInterruptedAndServiceIsNotDisposing() { AtomicReference exception = new AtomicReference<>(); + ExceptionMonitor previous = ExceptionMonitor.getInstance(); ExceptionMonitor.setInstance(new ExceptionMonitor() { @Override public void exceptionCaught(Throwable cause) { - exception.set(cause); + LOGGER.error("Exception caught in test {}", testName.getMethodName(), cause); + + if (isFromNioDatagramAcceptor(cause)) { + exception.set(cause); + } } }); @@ -103,7 +122,17 @@ public void exceptionCaught(Throwable cause) { } } } finally { - ExceptionMonitor.setInstance(null); + ExceptionMonitor.setInstance(previous); + } + } + + private static boolean isFromNioDatagramAcceptor(Throwable cause) { + for (StackTraceElement stackTraceElement : cause.getStackTrace()) { + String className = stackTraceElement.getClassName(); + if (className.equals(NioDatagramAcceptor.class.getName())) { + return true; + } } + return false; } }