|
15 | 15 | */ |
16 | 16 | package io.netty.handler.codec.quic; |
17 | 17 |
|
| 18 | +import io.netty.buffer.ByteBuf; |
18 | 19 | import io.netty.buffer.Unpooled; |
19 | 20 | import io.netty.channel.Channel; |
20 | 21 | import io.netty.channel.ChannelHandlerContext; |
|
31 | 32 | import org.junit.jupiter.params.provider.MethodSource; |
32 | 33 |
|
33 | 34 | import java.nio.channels.ClosedChannelException; |
| 35 | +import java.util.concurrent.CountDownLatch; |
34 | 36 | import java.util.concurrent.Executor; |
| 37 | +import java.util.concurrent.Semaphore; |
35 | 38 | import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
36 | 40 |
|
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
37 | 42 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
38 | 44 |
|
39 | 45 | public class QuicStreamChannelCloseTest extends AbstractQuicTest { |
40 | 46 |
|
@@ -292,6 +298,201 @@ public void channelActive(ChannelHandlerContext ctx) { |
292 | 298 | } |
293 | 299 | } |
294 | 300 |
|
| 301 | + @ParameterizedTest |
| 302 | + @MethodSource("newSslTaskExecutors") |
| 303 | + public void testCloseBeforeEndOfInputReturnsFlowControlCredit(Executor executor) throws Throwable { |
| 304 | + final int window = 64 * 1024; |
| 305 | + // Larger than the windows of the client, so the response can't be received completely before it reads. |
| 306 | + final int responseSize = 2 * window; |
| 307 | + Channel server = null; |
| 308 | + Channel channel = null; |
| 309 | + try { |
| 310 | + server = QuicTestUtils.newServer(executor, QuicTestUtils.NOOP_HANDLER, |
| 311 | + new ChannelInboundHandler() { |
| 312 | + @Override |
| 313 | + public boolean isSharable() { |
| 314 | + return true; |
| 315 | + } |
| 316 | + |
| 317 | + @Override |
| 318 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 319 | + ReferenceCountUtil.release(msg); |
| 320 | + } |
| 321 | + |
| 322 | + @Override |
| 323 | + public void channelShutdown(ChannelHandlerContext ctx, ChannelShutdownType type) { |
| 324 | + if (type.direction() == ChannelShutdownDirection.Inbound) { |
| 325 | + ctx.writeAndFlush(Unpooled.directBuffer(responseSize).writeZero(responseSize)) |
| 326 | + .addListener(f -> ctx.shutdown(ChannelShutdownType.newOutbound())); |
| 327 | + } |
| 328 | + ctx.fireChannelShutdown(type); |
| 329 | + } |
| 330 | + }); |
| 331 | + channel = QuicTestUtils.newClient(QuicTestUtils.newQuicClientBuilder(executor) |
| 332 | + .initialMaxData(window).initialMaxStreamDataBidirectionalLocal(window)); |
| 333 | + QuicChannel quicChannel = QuicTestUtils.newQuicChannelBootstrap(channel) |
| 334 | + .handler(new ChannelInboundHandler() { }) |
| 335 | + .streamHandler(new ChannelInboundHandler() { }) |
| 336 | + .remoteAddress(server.localAddress()) |
| 337 | + .connect().get(); |
| 338 | + |
| 339 | + // Close the stream once the first bytes of the response were received, the rest is never read by the |
| 340 | + // user. |
| 341 | + QuicStreamChannel cancelled = quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, |
| 342 | + new ChannelInboundHandler() { |
| 343 | + @Override |
| 344 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 345 | + ReferenceCountUtil.release(msg); |
| 346 | + ctx.close(); |
| 347 | + } |
| 348 | + }).get(); |
| 349 | + cancelled.writeAndFlush(Unpooled.directBuffer().writeZero(8)) |
| 350 | + .addListener(f -> cancelled.shutdown(ChannelShutdownType.newOutbound())); |
| 351 | + cancelled.closeFuture().sync(); |
| 352 | + |
| 353 | + // The next response must still be received completely. |
| 354 | + AtomicInteger received = new AtomicInteger(); |
| 355 | + CountDownLatch endOfInput = new CountDownLatch(1); |
| 356 | + QuicStreamChannel stream = quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, |
| 357 | + new ChannelInboundHandler() { |
| 358 | + @Override |
| 359 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 360 | + received.addAndGet(((ByteBuf) msg).readableBytes()); |
| 361 | + ReferenceCountUtil.release(msg); |
| 362 | + } |
| 363 | + |
| 364 | + @Override |
| 365 | + public void channelShutdown(ChannelHandlerContext ctx, ChannelShutdownType type) { |
| 366 | + if (type.direction() == ChannelShutdownDirection.Inbound) { |
| 367 | + endOfInput.countDown(); |
| 368 | + } |
| 369 | + ctx.fireChannelShutdown(type); |
| 370 | + } |
| 371 | + }).get(); |
| 372 | + stream.writeAndFlush(Unpooled.directBuffer().writeZero(8)) |
| 373 | + .addListener(f -> stream.shutdown(ChannelShutdownType.newOutbound())); |
| 374 | + assertTrue(endOfInput.await(5, TimeUnit.SECONDS), "received only " + received.get() + " bytes"); |
| 375 | + assertEquals(responseSize, received.get()); |
| 376 | + stream.closeFuture().sync(); |
| 377 | + quicChannel.close().sync(); |
| 378 | + } finally { |
| 379 | + QuicTestUtils.closeIfNotNull(channel); |
| 380 | + QuicTestUtils.closeIfNotNull(server); |
| 381 | + |
| 382 | + shutdown(executor); |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + @ParameterizedTest |
| 387 | + @MethodSource("newSslTaskExecutors") |
| 388 | + public void testCloseOutsideOfReadBeforeEndOfInputReturnsFlowControlCredit(Executor executor) throws Throwable { |
| 389 | + final int window = 64 * 1024; |
| 390 | + // Larger than the windows of the client, so the response can't be received completely before it reads. |
| 391 | + final int responseSize = 2 * window; |
| 392 | + Semaphore responseWritten = new Semaphore(0); |
| 393 | + Channel server = null; |
| 394 | + Channel channel = null; |
| 395 | + try { |
| 396 | + server = QuicTestUtils.newServer(executor, QuicTestUtils.NOOP_HANDLER, |
| 397 | + new ChannelInboundHandler() { |
| 398 | + @Override |
| 399 | + public boolean isSharable() { |
| 400 | + return true; |
| 401 | + } |
| 402 | + |
| 403 | + @Override |
| 404 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 405 | + ReferenceCountUtil.release(msg); |
| 406 | + } |
| 407 | + |
| 408 | + @Override |
| 409 | + public void channelShutdown(ChannelHandlerContext ctx, ChannelShutdownType type) { |
| 410 | + if (type.direction() == ChannelShutdownDirection.Inbound) { |
| 411 | + // The write only completes once the client opened its flow control windows far |
| 412 | + // enough to accept the whole response. |
| 413 | + ctx.writeAndFlush(Unpooled.directBuffer(responseSize).writeZero(responseSize)) |
| 414 | + .addListener(future -> { |
| 415 | + if (future.isSuccess()) { |
| 416 | + responseWritten.release(); |
| 417 | + } |
| 418 | + }) |
| 419 | + .addListener(f -> ctx.shutdown(ChannelShutdownType.newOutbound())); |
| 420 | + } |
| 421 | + ctx.fireChannelShutdown(type); |
| 422 | + } |
| 423 | + }); |
| 424 | + channel = QuicTestUtils.newClient(QuicTestUtils.newQuicClientBuilder(executor) |
| 425 | + .initialMaxData(window).initialMaxStreamDataBidirectionalLocal(window)); |
| 426 | + QuicChannel quicChannel = QuicTestUtils.newQuicChannelBootstrap(channel) |
| 427 | + .handler(new ChannelInboundHandler() { }) |
| 428 | + .streamHandler(new ChannelInboundHandler() { }) |
| 429 | + .remoteAddress(server.localAddress()) |
| 430 | + .connect().get(); |
| 431 | + |
| 432 | + // Read only the first bytes of the response, the rest stays in quiche as the user never asks for it. |
| 433 | + CountDownLatch firstRead = new CountDownLatch(1); |
| 434 | + QuicStreamChannel cancelled = quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, |
| 435 | + new ChannelInboundHandler() { |
| 436 | + @Override |
| 437 | + public void handlerAdded(ChannelHandlerContext ctx) { |
| 438 | + ctx.channel().config().setAutoRead(false); |
| 439 | + } |
| 440 | + |
| 441 | + @Override |
| 442 | + public void channelActive(ChannelHandlerContext ctx) { |
| 443 | + ctx.read(); |
| 444 | + ctx.fireChannelActive(); |
| 445 | + } |
| 446 | + |
| 447 | + @Override |
| 448 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 449 | + ReferenceCountUtil.release(msg); |
| 450 | + firstRead.countDown(); |
| 451 | + } |
| 452 | + }).get(); |
| 453 | + cancelled.writeAndFlush(Unpooled.directBuffer().writeZero(8)) |
| 454 | + .addListener(f -> cancelled.shutdown(ChannelShutdownType.newOutbound())); |
| 455 | + assertTrue(firstRead.await(5, TimeUnit.SECONDS)); |
| 456 | + |
| 457 | + // Close the stream outside of a read callback, so it is up to close() to drop what is left and to send |
| 458 | + // the resulting window updates. Nothing else is sent by the client until the response is written. |
| 459 | + cancelled.close().sync(); |
| 460 | + assertTrue(responseWritten.tryAcquire(5, TimeUnit.SECONDS), |
| 461 | + "server could not write the whole response of the closed stream"); |
| 462 | + |
| 463 | + // The next response must be received completely as well. |
| 464 | + AtomicInteger received = new AtomicInteger(); |
| 465 | + CountDownLatch endOfInput = new CountDownLatch(1); |
| 466 | + QuicStreamChannel stream = quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, |
| 467 | + new ChannelInboundHandler() { |
| 468 | + @Override |
| 469 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 470 | + received.addAndGet(((ByteBuf) msg).readableBytes()); |
| 471 | + ReferenceCountUtil.release(msg); |
| 472 | + } |
| 473 | + |
| 474 | + @Override |
| 475 | + public void channelShutdown(ChannelHandlerContext ctx, ChannelShutdownType type) { |
| 476 | + if (type.direction() == ChannelShutdownDirection.Inbound) { |
| 477 | + endOfInput.countDown(); |
| 478 | + } |
| 479 | + ctx.fireChannelShutdown(type); |
| 480 | + } |
| 481 | + }).get(); |
| 482 | + stream.writeAndFlush(Unpooled.directBuffer().writeZero(8)) |
| 483 | + .addListener(f -> stream.shutdown(ChannelShutdownType.newOutbound())); |
| 484 | + assertTrue(endOfInput.await(5, TimeUnit.SECONDS), "received only " + received.get() + " bytes"); |
| 485 | + assertEquals(responseSize, received.get()); |
| 486 | + stream.closeFuture().sync(); |
| 487 | + quicChannel.close().sync(); |
| 488 | + } finally { |
| 489 | + QuicTestUtils.closeIfNotNull(channel); |
| 490 | + QuicTestUtils.closeIfNotNull(server); |
| 491 | + |
| 492 | + shutdown(executor); |
| 493 | + } |
| 494 | + } |
| 495 | + |
295 | 496 | private static final class StreamHandler implements ChannelInboundHandler { |
296 | 497 |
|
297 | 498 | @Override |
|