|
21 | 21 | import io.netty.channel.ChannelHandlerContext; |
22 | 22 | import io.netty.channel.ChannelPipeline; |
23 | 23 | import io.netty.channel.DefaultChannelConfig; |
| 24 | +import io.netty.channel.WriteBufferWaterMark; |
| 25 | +import io.netty.channel.embedded.EmbeddedChannel; |
24 | 26 | import io.netty.handler.codec.http.HttpResponseStatus; |
25 | 27 | import io.netty.handler.codec.http2.Http2Exception.ShutdownHint; |
26 | 28 | import io.netty.util.ReferenceCountUtil; |
|
41 | 43 | import org.mockito.invocation.InvocationOnMock; |
42 | 44 | import org.mockito.stubbing.Answer; |
43 | 45 |
|
| 46 | +import java.net.InetSocketAddress; |
44 | 47 | import java.util.List; |
45 | 48 | import java.util.concurrent.CountDownLatch; |
46 | 49 | import java.util.concurrent.TimeUnit; |
@@ -333,6 +336,48 @@ public Void answer(InvocationOnMock invocation) throws Throwable { |
333 | 336 | verify(remoteFlow).channelWritabilityChanged(); |
334 | 337 | } |
335 | 338 |
|
| 339 | + /** |
| 340 | + * A large body must fully drain even when the channel briefly becomes unwritable while it is being written |
| 341 | + * and then flips back to writable synchronously during {@code flush()}. That writable transition re-enters |
| 342 | + * {@link Http2ConnectionHandler#channelWritabilityChanged(ChannelHandlerContext)} while the flush is still in |
| 343 | + * progress; the handler must not recurse (which livelocks), but it must still write the frames that were |
| 344 | + * queued in the remote flow controller. Dropping the reentrant flush strands the tail of the body forever. |
| 345 | + */ |
| 346 | + @Test |
| 347 | + public void largeWriteDrainsFullyWhenWritabilityTogglesDuringFlush() throws Exception { |
| 348 | + // Small watermarks so writing ~1 KiB flips the channel unwritable, and EmbeddedChannel draining the |
| 349 | + // outbound buffer during flush() flips it back to writable synchronously (ChannelOutboundBuffer.remove |
| 350 | + // fires the event inline), re-entering flush(). |
| 351 | + EmbeddedChannel channel = new EmbeddedChannel(); |
| 352 | + channel.config().setWriteBufferWaterMark(new WriteBufferWaterMark(512, 1024)); |
| 353 | + |
| 354 | + Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder() |
| 355 | + .server(false) |
| 356 | + .frameListener(new Http2FrameAdapter()) |
| 357 | + .validateHeaders(false) |
| 358 | + .gracefulShutdownTimeoutMillis(0) |
| 359 | + .build(); |
| 360 | + |
| 361 | + channel.connect(new InetSocketAddress(0)); |
| 362 | + channel.pipeline().addLast(handler); |
| 363 | + channel.pipeline().fireChannelActive(); |
| 364 | + // Discard the connection preface + SETTINGS the handler wrote on activation. |
| 365 | + channel.releaseOutbound(); |
| 366 | + |
| 367 | + ChannelHandlerContext ctx = channel.pipeline().context(handler); |
| 368 | + handler.encoder().writeHeaders(ctx, 3, new DefaultHttp2Headers(), 0, false, ctx.newPromise()); |
| 369 | + |
| 370 | + // Larger than the write high-watermark but within the default 65535-byte flow-control window, so only |
| 371 | + // channel writability (not flow control) gates the write. |
| 372 | + final int size = 60 * 1024; |
| 373 | + ByteBuf data = Unpooled.buffer(size).writeZero(size); |
| 374 | + Promise<Void> dataPromise = ctx.newPromise(); |
| 375 | + handler.encoder().writeData(ctx, 3, data, 0, false, dataPromise); |
| 376 | + handler.flush(ctx); |
| 377 | + assertTrue(dataPromise.isSuccess()); |
| 378 | + channel.finishAndReleaseAll(); |
| 379 | + } |
| 380 | + |
336 | 381 | @Test |
337 | 382 | public void serverReceivingInvalidClientPrefaceStringShouldHandleException() throws Exception { |
338 | 383 | when(connection.isServer()).thenReturn(true); |
|