|
26 | 26 | import io.netty.channel.ChannelPromise; |
27 | 27 | import io.netty.channel.DefaultChannelConfig; |
28 | 28 | import io.netty.channel.DefaultChannelPromise; |
| 29 | +import io.netty.channel.WriteBufferWaterMark; |
| 30 | +import io.netty.channel.embedded.EmbeddedChannel; |
29 | 31 | import io.netty.handler.codec.http.HttpResponseStatus; |
30 | 32 | import io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator; |
31 | 33 | import io.netty.handler.codec.http2.Http2Exception.ShutdownHint; |
|
47 | 49 | import org.mockito.invocation.InvocationOnMock; |
48 | 50 | import org.mockito.stubbing.Answer; |
49 | 51 |
|
| 52 | +import java.net.InetSocketAddress; |
50 | 53 | import java.util.List; |
51 | 54 | import java.util.concurrent.CountDownLatch; |
52 | 55 | import java.util.concurrent.TimeUnit; |
@@ -363,6 +366,48 @@ public Void answer(InvocationOnMock invocation) throws Throwable { |
363 | 366 | verify(remoteFlow).channelWritabilityChanged(); |
364 | 367 | } |
365 | 368 |
|
| 369 | + /** |
| 370 | + * A large body must fully drain even when the channel briefly becomes unwritable while it is being written |
| 371 | + * and then flips back to writable synchronously during {@code flush()}. That writable transition re-enters |
| 372 | + * {@link Http2ConnectionHandler#channelWritabilityChanged(ChannelHandlerContext)} while the flush is still in |
| 373 | + * progress; the handler must not recurse (which livelocks), but it must still write the frames that were |
| 374 | + * queued in the remote flow controller. Dropping the reentrant flush strands the tail of the body forever. |
| 375 | + */ |
| 376 | + @Test |
| 377 | + public void largeWriteDrainsFullyWhenWritabilityTogglesDuringFlush() throws Exception { |
| 378 | + // Small watermarks so writing ~1 KiB flips the channel unwritable, and EmbeddedChannel draining the |
| 379 | + // outbound buffer during flush() flips it back to writable synchronously (ChannelOutboundBuffer.remove |
| 380 | + // fires the event inline), re-entering flush(). |
| 381 | + EmbeddedChannel channel = new EmbeddedChannel(); |
| 382 | + channel.config().setWriteBufferWaterMark(new WriteBufferWaterMark(512, 1024)); |
| 383 | + |
| 384 | + Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder() |
| 385 | + .server(false) |
| 386 | + .frameListener(new Http2FrameAdapter()) |
| 387 | + .validateHeaders(false) |
| 388 | + .gracefulShutdownTimeoutMillis(0) |
| 389 | + .build(); |
| 390 | + |
| 391 | + channel.connect(new InetSocketAddress(0)); |
| 392 | + channel.pipeline().addLast(handler); |
| 393 | + channel.pipeline().fireChannelActive(); |
| 394 | + // Discard the connection preface + SETTINGS the handler wrote on activation. |
| 395 | + channel.releaseOutbound(); |
| 396 | + |
| 397 | + ChannelHandlerContext ctx = channel.pipeline().context(handler); |
| 398 | + handler.encoder().writeHeaders(ctx, 3, new DefaultHttp2Headers(), 0, false, ctx.newPromise()); |
| 399 | + |
| 400 | + // Larger than the write high-watermark but within the default 65535-byte flow-control window, so only |
| 401 | + // channel writability (not flow control) gates the write. |
| 402 | + final int size = 60 * 1024; |
| 403 | + ByteBuf data = Unpooled.buffer(size).writeZero(size); |
| 404 | + ChannelPromise dataPromise = ctx.newPromise(); |
| 405 | + handler.encoder().writeData(ctx, 3, data, 0, false, dataPromise); |
| 406 | + handler.flush(ctx); |
| 407 | + assertTrue(dataPromise.isSuccess()); |
| 408 | + channel.finishAndReleaseAll(); |
| 409 | + } |
| 410 | + |
366 | 411 | @Test |
367 | 412 | public void serverReceivingInvalidClientPrefaceStringShouldHandleException() throws Exception { |
368 | 413 | when(connection.isServer()).thenReturn(true); |
|