diff --git a/transport/src/main/java/io/netty/bootstrap/Bootstrap.java b/transport/src/main/java/io/netty/bootstrap/Bootstrap.java index dbb6942f6b..f54f240c99 100644 --- a/transport/src/main/java/io/netty/bootstrap/Bootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/Bootstrap.java @@ -257,7 +257,6 @@ public class Bootstrap extends AbstractBootstrap { } @Override - @SuppressWarnings("unchecked") void init(Channel channel) { ChannelPipeline p = channel.pipeline(); p.addLast(config.handler()); diff --git a/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java b/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java index 05b3da299e..e57fde3c0e 100644 --- a/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java +++ b/transport/src/test/java/io/netty/bootstrap/BootstrapTest.java @@ -35,6 +35,7 @@ import io.netty.channel.ServerChannel; import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalChannel; import io.netty.channel.local.LocalServerChannel; +import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.resolver.AddressResolver; import io.netty.resolver.AddressResolverGroup; import io.netty.resolver.AbstractAddressResolver; @@ -98,6 +99,25 @@ public class BootstrapTest { assertEquals(value, attributesArray[0].getValue()); } + @Test + public void optionsAndAttributesMustBeAvailableOnChannelInit() throws InterruptedException { + final AttributeKey key = AttributeKey.valueOf(UUID.randomUUID().toString()); + new Bootstrap() + .group(groupA) + .channel(LocalChannel.class) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4242) + .attr(key, "value") + .handler(new ChannelInitializer() { + @Override + protected void initChannel(LocalChannel ch) throws Exception { + Integer option = ch.config().getOption(ChannelOption.CONNECT_TIMEOUT_MILLIS); + assertEquals(4242, (int) option); + assertEquals("value", ch.attr(key).get()); + } + }) + .bind(LocalAddress.ANY).sync(); + } + @Test(timeout = 10000) public void testBindDeadLock() throws Exception { final Bootstrap bootstrapA = new Bootstrap(); diff --git a/transport/src/test/java/io/netty/bootstrap/ServerBootstrapTest.java b/transport/src/test/java/io/netty/bootstrap/ServerBootstrapTest.java index deb225da34..ca9bf73703 100644 --- a/transport/src/test/java/io/netty/bootstrap/ServerBootstrapTest.java +++ b/transport/src/test/java/io/netty/bootstrap/ServerBootstrapTest.java @@ -21,18 +21,22 @@ import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoopGroup; import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalChannel; import io.netty.channel.local.LocalEventLoopGroup; import io.netty.channel.local.LocalServerChannel; +import io.netty.util.AttributeKey; import org.junit.Test; import java.util.UUID; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -137,4 +141,37 @@ public class ServerBootstrapTest { group.shutdownGracefully(); } } + + @Test + public void optionsAndAttributesMustBeAvailableOnChildChannelInit() throws InterruptedException { + EventLoopGroup group = new DefaultEventLoopGroup(1); + LocalAddress addr = new LocalAddress(UUID.randomUUID().toString()); + final AttributeKey key = AttributeKey.valueOf(UUID.randomUUID().toString()); + final AtomicBoolean requestServed = new AtomicBoolean(); + ServerBootstrap sb = new ServerBootstrap() + .group(group) + .channel(LocalServerChannel.class) + .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4242) + .childAttr(key, "value") + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(LocalChannel ch) throws Exception { + Integer option = ch.config().getOption(ChannelOption.CONNECT_TIMEOUT_MILLIS); + assertEquals(4242, (int) option); + assertEquals("value", ch.attr(key).get()); + requestServed.set(true); + } + }); + Channel serverChannel = sb.bind(addr).syncUninterruptibly().channel(); + + Bootstrap cb = new Bootstrap(); + cb.group(group) + .channel(LocalChannel.class) + .handler(new ChannelInboundHandlerAdapter()); + Channel clientChannel = cb.connect(addr).syncUninterruptibly().channel(); + serverChannel.close().syncUninterruptibly(); + clientChannel.close().syncUninterruptibly(); + group.shutdownGracefully(); + assertTrue(requestServed.get()); + } }