Add testcase for ChannelInitializer.initChannel(...) when throwing an Exception (#8188)

Motivation:

We had a report that the exception may not be correctly propagated. This test shows it is.

Modifications:

Add testcase.

Result:

Test for https://github.com/netty/netty/issues/8158
This commit is contained in:
Norman Maurer 2018-08-10 08:54:21 +02:00 committed by GitHub
parent f22781f176
commit bd25fd03e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@ import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -63,6 +64,47 @@ public class ChannelInitializerTest {
group.shutdownGracefully(0, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS).syncUninterruptibly();
}
@Test
public void testInitChannelThrowsRegisterFirst() {
testInitChannelThrows(true);
}
@Test
public void testInitChannelThrowsRegisterAfter() {
testInitChannelThrows(false);
}
private void testInitChannelThrows(boolean registerFirst) {
final Exception exception = new Exception();
final AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
ChannelPipeline pipeline = new LocalChannel().pipeline();
if (registerFirst) {
group.register(pipeline.channel()).syncUninterruptibly();
}
pipeline.addFirst(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
throw exception;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
causeRef.set(cause);
super.exceptionCaught(ctx, cause);
}
});
if (!registerFirst) {
group.register(pipeline.channel()).syncUninterruptibly();
}
pipeline.channel().close().syncUninterruptibly();
pipeline.channel().closeFuture().syncUninterruptibly();
assertSame(exception, causeRef.get());
}
@Test
public void testChannelInitializerInInitializerCorrectOrdering() {
final ChannelInboundHandlerAdapter handler1 = new ChannelInboundHandlerAdapter();