1313
1414import io .netty .bootstrap .Bootstrap ;
1515import io .netty .buffer .ByteBuf ;
16- import io .netty .channel .Channel ;
17- import io .netty .channel .ChannelFuture ;
18- import io .netty .channel .ChannelHandlerContext ;
19- import io .netty .channel .ChannelInitializer ;
16+ import io .netty .channel .*;
2017import io .netty .channel .nio .NioEventLoopGroup ;
2118import io .netty .channel .socket .nio .NioSocketChannel ;
2219import io .netty .handler .codec .http2 .AbstractHttp2ConnectionHandlerBuilder ;
4441import io .vertx .test .core .Repeat ;
4542import org .junit .Test ;
4643
44+ import java .io .IOException ;
4745import java .net .InetSocketAddress ;
4846import java .util .concurrent .CompletableFuture ;
4947import java .util .concurrent .TimeUnit ;
@@ -82,7 +80,7 @@ private void testMYR(boolean multiplexImplementation) throws Exception {
8280 AtomicInteger inflightRequests = new AtomicInteger ();
8381 AtomicInteger maxInflightRequests = new AtomicInteger ();
8482 AtomicInteger receivedRstFrames = new AtomicInteger ();
85- CompletableFuture <Void > goAway = new CompletableFuture <>();
83+ CompletableFuture <Boolean > goAway = new CompletableFuture <>();
8684
8785 server .requestHandler (req -> {
8886 int val = inflightRequests .incrementAndGet ();
@@ -154,7 +152,7 @@ public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) th
154152
155153 @ Override
156154 public void onGoAwayRead (ChannelHandlerContext ctx , int lastStreamId , long errorCode , ByteBuf debugData ) throws Http2Exception {
157- goAway .complete (null );
155+ goAway .complete (true );
158156 }
159157 });
160158 return super .build ();
@@ -164,6 +162,16 @@ public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long error
164162 Builder clientHandlerBuilder = new Builder ();
165163 Http2ConnectionHandler clientHandler = clientHandlerBuilder .build ();
166164 ch .pipeline ().addLast (clientHandler );
165+ ch .pipeline ().addLast (new ChannelDuplexHandler () {
166+ @ Override
167+ public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) throws Exception {
168+ if (cause instanceof IOException && cause .getMessage ().startsWith ("Connection reset" )) {
169+ goAway .complete (false );
170+ } else {
171+ goAway .completeExceptionally (cause );
172+ }
173+ }
174+ });
167175 }
168176 };
169177 }
@@ -192,10 +200,14 @@ public ChannelFuture connect(int port, String host, BiConsumer<ChannelHandlerCon
192200 chctx .flush ();
193201 }).sync ();
194202
195- goAway .get (10 , TimeUnit .SECONDS );
196-
197203 // Check the number of rst frame received before getting a go away
198- assertEquals (receivedRstFrames .get (), maxRstFramePerWindow + 1 );
204+ if (goAway .get (20 , TimeUnit .SECONDS )) {
205+ assertEquals (receivedRstFrames .get (), maxRstFramePerWindow + 1 );
206+ } else {
207+ // Mitigate CI behavior
208+ assertTrue (receivedRstFrames .get () < maxRstFramePerWindow + 1 );
209+ }
210+
199211 assertTrue (maxInflightRequests .get () <= 2 * maxRstFramePerWindow );
200212 }
201213}
0 commit comments