Add tests verifying channel configure/initialisation order (#11050)

Motivation:
Channels need to have their configurations applied before we can call out to user-code via handlerAdded and initChannel.

Modification:
This adds tests for this behaviour, which already works correctly.

Result:
Better test coverage.
This commit is contained in:
Chris Vest 2021-03-02 15:46:23 +01:00 committed by GitHub
parent c22c6b845d
commit 690d1a53d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 1 deletions

View File

@ -257,7 +257,6 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap, Channel> {
}
@Override
@SuppressWarnings("unchecked")
void init(Channel channel) {
ChannelPipeline p = channel.pipeline();
p.addLast(config.handler());

View File

@ -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<String> 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<LocalChannel>() {
@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();

View File

@ -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<String> 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<LocalChannel>() {
@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());
}
}