diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2CodecTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2CodecTest.java index 198a9fb45c..d8b37b3625 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2CodecTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2CodecTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; import io.netty.channel.DefaultEventLoop; import io.netty.channel.EventLoopGroup; import io.netty.channel.local.LocalAddress; @@ -34,6 +35,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + import static io.netty.handler.codec.http2.Http2CodecUtil.isStreamIdValid; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; @@ -47,6 +51,7 @@ public class Http2CodecTest { private static EventLoopGroup group; private Channel serverChannel; + private volatile Channel serverConnectedChannel; private Channel clientChannel; private LastInboundHandler serverLastInboundHandler; @@ -57,12 +62,20 @@ public class Http2CodecTest { @Before public void setUp() throws InterruptedException { + final CountDownLatch serverChannelLatch = new CountDownLatch(1); LocalAddress serverAddress = new LocalAddress(getClass().getName()); serverLastInboundHandler = new SharableLastInboundHandler(); ServerBootstrap sb = new ServerBootstrap() .channel(LocalServerChannel.class) .group(group) - .childHandler(new Http2Codec(true, serverLastInboundHandler)); + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(Channel ch) throws Exception { + serverConnectedChannel = ch; + ch.pipeline().addLast(new Http2Codec(true, serverLastInboundHandler)); + serverChannelLatch.countDown(); + } + }); serverChannel = sb.bind(serverAddress).sync().channel(); Bootstrap cb = new Bootstrap() @@ -70,6 +83,7 @@ public class Http2CodecTest { .group(group) .handler(new Http2Codec(false, new TestChannelInitializer())); clientChannel = cb.connect(serverAddress).sync().channel(); + assertTrue(serverChannelLatch.await(2, TimeUnit.SECONDS)); } @AfterClass @@ -81,6 +95,10 @@ public class Http2CodecTest { public void tearDown() throws Exception { clientChannel.close().sync(); serverChannel.close().sync(); + if (serverConnectedChannel != null) { + serverConnectedChannel.close().sync(); + serverConnectedChannel = null; + } } @Test diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java index aa0c976a7a..39eafa8610 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java @@ -88,6 +88,7 @@ public class Http2ConnectionRoundtripTest { private ServerBootstrap sb; private Bootstrap cb; private Channel serverChannel; + private volatile Channel serverConnectedChannel; private Channel clientChannel; private FrameCountDown serverFrameCountDown; private CountDownLatch requestLatch; @@ -113,6 +114,10 @@ public class Http2ConnectionRoundtripTest { serverChannel.close().sync(); serverChannel = null; } + if (serverConnectedChannel != null) { + serverConnectedChannel.close().sync(); + serverConnectedChannel = null; + } Future serverGroup = sb.config().group().shutdownGracefully(0, 0, MILLISECONDS); Future serverChildGroup = sb.config().childGroup().shutdownGracefully(0, 0, MILLISECONDS); Future clientGroup = cb.config().group().shutdownGracefully(0, 0, MILLISECONDS); @@ -537,6 +542,7 @@ public class Http2ConnectionRoundtripTest { sb.childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { + serverConnectedChannel = ch; ChannelPipeline p = ch.pipeline(); serverFrameCountDown = new FrameCountDown(serverListener, serverSettingsAckLatch, diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/HttpToHttp2ConnectionHandlerTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/HttpToHttp2ConnectionHandlerTest.java index 669139e649..49690a61da 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/HttpToHttp2ConnectionHandlerTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/HttpToHttp2ConnectionHandlerTest.java @@ -52,6 +52,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import static io.netty.handler.codec.http.HttpMethod.CONNECT; import static io.netty.handler.codec.http.HttpMethod.GET; @@ -89,6 +90,7 @@ public class HttpToHttp2ConnectionHandlerTest { private ServerBootstrap sb; private Bootstrap cb; private Channel serverChannel; + private volatile Channel serverConnectedChannel; private Channel clientChannel; private CountDownLatch requestLatch; private CountDownLatch serverSettingsAckLatch; @@ -110,6 +112,10 @@ public class HttpToHttp2ConnectionHandlerTest { serverChannel.close().sync(); serverChannel = null; } + if (serverConnectedChannel != null) { + serverConnectedChannel.close().sync(); + serverConnectedChannel = null; + } Future serverGroup = sb.config().group().shutdownGracefully(0, 0, MILLISECONDS); Future serverChildGroup = sb.config().childGroup().shutdownGracefully(0, 0, MILLISECONDS); Future clientGroup = cb.config().group().shutdownGracefully(0, 0, MILLISECONDS); @@ -493,6 +499,7 @@ public class HttpToHttp2ConnectionHandlerTest { } private void bootstrapEnv(int requestCountDown, int serverSettingsAckCount, int trailersCount) throws Exception { + final CountDownLatch serverChannelLatch = new CountDownLatch(1); requestLatch = new CountDownLatch(requestCountDown); serverSettingsAckLatch = new CountDownLatch(serverSettingsAckCount); trailersLatch = trailersCount == 0 ? null : new CountDownLatch(trailersCount); @@ -505,6 +512,7 @@ public class HttpToHttp2ConnectionHandlerTest { sb.childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { + serverConnectedChannel = ch; ChannelPipeline p = ch.pipeline(); serverFrameCountDown = new FrameCountDown(serverListener, serverSettingsAckLatch, requestLatch, null, trailersLatch); @@ -512,6 +520,7 @@ public class HttpToHttp2ConnectionHandlerTest { .server(true) .frameListener(serverFrameCountDown) .build()); + serverChannelLatch.countDown(); } }); @@ -535,6 +544,7 @@ public class HttpToHttp2ConnectionHandlerTest { ChannelFuture ccf = cb.connect(serverChannel.localAddress()); assertTrue(ccf.awaitUninterruptibly().isSuccess()); clientChannel = ccf.channel(); + assertTrue(serverChannelLatch.await(2, TimeUnit.SECONDS)); } private void verifyHeadersOnly(Http2Headers expected, ChannelPromise writePromise, ChannelFuture writeFuture) diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/InboundHttp2ToHttpAdapterTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/InboundHttp2ToHttpAdapterTest.java index 009b67be42..0b5618a551 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/InboundHttp2ToHttpAdapterTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/InboundHttp2ToHttpAdapterTest.java @@ -88,7 +88,7 @@ public class InboundHttp2ToHttpAdapterTest { private ServerBootstrap sb; private Bootstrap cb; private Channel serverChannel; - private Channel serverConnectedChannel; + private volatile Channel serverConnectedChannel; private Channel clientChannel; private CountDownLatch serverLatch; private CountDownLatch clientLatch; @@ -118,15 +118,16 @@ public class InboundHttp2ToHttpAdapterTest { serverChannel.close().sync(); serverChannel = null; } + if (serverConnectedChannel != null) { + serverConnectedChannel.close().sync(); + serverConnectedChannel = null; + } Future serverGroup = sb.config().group().shutdownGracefully(0, 0, MILLISECONDS); Future serverChildGroup = sb.config().childGroup().shutdownGracefully(0, 0, MILLISECONDS); Future clientGroup = cb.config().group().shutdownGracefully(0, 0, MILLISECONDS); serverGroup.sync(); serverChildGroup.sync(); clientGroup.sync(); - clientDelegator = null; - serverDelegator = null; - serverConnectedChannel = null; } @Test @@ -763,6 +764,7 @@ public class InboundHttp2ToHttpAdapterTest { sb.childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { + serverConnectedChannel = ch; ChannelPipeline p = ch.pipeline(); Http2Connection connection = new DefaultHttp2Connection(true); @@ -779,7 +781,6 @@ public class InboundHttp2ToHttpAdapterTest { serverDelegator = new HttpResponseDelegator(serverListener, serverLatch, serverLatch2); p.addLast(serverDelegator); - serverConnectedChannel = ch; settingsDelegator = new HttpSettingsDelegator(settingsListener, settingsLatch); p.addLast(settingsDelegator); serverChannelLatch.countDown();