diff --git a/transport/src/test/java/io/netty/channel/ChannelInitializerTest.java b/transport/src/test/java/io/netty/channel/ChannelInitializerTest.java index 5ab48867bc..26b5e4e9fc 100644 --- a/transport/src/test/java/io/netty/channel/ChannelInitializerTest.java +++ b/transport/src/test/java/io/netty/channel/ChannelInitializerTest.java @@ -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 causeRef = new AtomicReference(); + + ChannelPipeline pipeline = new LocalChannel().pipeline(); + + if (registerFirst) { + group.register(pipeline.channel()).syncUninterruptibly(); + } + pipeline.addFirst(new ChannelInitializer() { + @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();