|
18 | 18 | import io.netty.buffer.ByteBuf; |
19 | 19 | import io.netty.buffer.CompositeByteBuf; |
20 | 20 | import io.netty.channel.embedded.EmbeddedChannel; |
| 21 | +import io.netty.util.AbstractReferenceCounted; |
21 | 22 | import io.netty.util.CharsetUtil; |
| 23 | +import io.netty.util.ReferenceCounted; |
22 | 24 | import io.netty.util.concurrent.DefaultThreadFactory; |
23 | 25 | import io.netty.util.concurrent.RejectedExecutionHandlers; |
24 | 26 | import io.netty.util.concurrent.SingleThreadEventExecutor; |
|
27 | 29 |
|
28 | 30 | import java.net.SocketAddress; |
29 | 31 | import java.nio.ByteBuffer; |
| 32 | +import java.nio.channels.ClosedChannelException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
30 | 35 | import java.util.Queue; |
31 | 36 | import java.util.concurrent.CountDownLatch; |
32 | 37 | import java.util.concurrent.RejectedExecutionException; |
33 | 38 | import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
| 40 | +import java.util.concurrent.atomic.AtomicLong; |
| 41 | +import java.util.concurrent.atomic.AtomicReference; |
34 | 42 |
|
35 | 43 | import static io.netty.buffer.Unpooled.*; |
36 | 44 |
|
37 | 45 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 46 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
38 | 48 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
39 | 49 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 50 | +import static org.junit.jupiter.api.Assertions.assertSame; |
40 | 51 | import static org.junit.jupiter.api.Assertions.assertTrue; |
41 | 52 |
|
42 | 53 | public class ChannelOutboundBufferTest { |
@@ -427,6 +438,220 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) { |
427 | 438 | safeClose(ch); |
428 | 439 | } |
429 | 440 |
|
| 441 | + @Test |
| 442 | + public void testWriteAndFlushFromWritabilityChangedCausedByCancelledWrite() { |
| 443 | + final List<ChannelFuture> reentrantWrites = new ArrayList<ChannelFuture>(); |
| 444 | + EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { |
| 445 | + @Override |
| 446 | + public void channelWritabilityChanged(ChannelHandlerContext ctx) { |
| 447 | + if (ctx.channel().isWritable() && reentrantWrites.isEmpty()) { |
| 448 | + reentrantWrites.add(ctx.writeAndFlush(wrappedBuffer(new byte[] { 2 }))); |
| 449 | + } |
| 450 | + ctx.fireChannelWritabilityChanged(); |
| 451 | + } |
| 452 | + }); |
| 453 | + ch.config().setWriteBufferWaterMark(new WriteBufferWaterMark(256, 512)); |
| 454 | + |
| 455 | + ByteBuf cancelledBuf = buffer().writeZero(512); |
| 456 | + ChannelFuture cancelled = ch.write(cancelledBuf); |
| 457 | + ChannelFuture second = ch.write(wrappedBuffer(new byte[] { 1 })); |
| 458 | + assertFalse(ch.isWritable()); |
| 459 | + assertTrue(cancelled.cancel(false)); |
| 460 | + |
| 461 | + // Releasing the cancelled write makes the channel writable while addFlush() runs. |
| 462 | + ch.flush(); |
| 463 | + |
| 464 | + assertTrue(ch.isWritable()); |
| 465 | + assertEquals(1, reentrantWrites.size()); |
| 466 | + assertEquals(0, cancelledBuf.refCnt()); |
| 467 | + assertTrue(second.isSuccess()); |
| 468 | + assertTrue(reentrantWrites.get(0).isSuccess()); |
| 469 | + ChannelOutboundBuffer buffer = ch.unsafe().outboundBuffer(); |
| 470 | + assertTrue(buffer.isEmpty()); |
| 471 | + assertEquals(0, buffer.size()); |
| 472 | + assertEquals(0, buffer.totalPendingWriteBytes()); |
| 473 | + // The cancelled write is replaced by an empty buffer. |
| 474 | + assertOutbound(ch, 0); |
| 475 | + assertOutbound(ch, 1); |
| 476 | + assertOutbound(ch, 2); |
| 477 | + assertNull(ch.readOutbound()); |
| 478 | + |
| 479 | + assertFalse(ch.finish()); |
| 480 | + assertFalse(ch.isOpen()); |
| 481 | + } |
| 482 | + |
| 483 | + @Test |
| 484 | + public void testCloseFromWritabilityChangedCausedByCancelledWrite() { |
| 485 | + final AtomicInteger inactive = new AtomicInteger(); |
| 486 | + EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() { |
| 487 | + @Override |
| 488 | + public void channelWritabilityChanged(ChannelHandlerContext ctx) { |
| 489 | + if (ctx.channel().isWritable()) { |
| 490 | + ctx.close(); |
| 491 | + } |
| 492 | + ctx.fireChannelWritabilityChanged(); |
| 493 | + } |
| 494 | + |
| 495 | + @Override |
| 496 | + public void channelInactive(ChannelHandlerContext ctx) { |
| 497 | + inactive.incrementAndGet(); |
| 498 | + ctx.fireChannelInactive(); |
| 499 | + } |
| 500 | + }); |
| 501 | + ch.config().setWriteBufferWaterMark(new WriteBufferWaterMark(256, 512)); |
| 502 | + |
| 503 | + ByteBuf cancelledBuf = buffer().writeZero(512); |
| 504 | + ChannelFuture cancelled = ch.write(cancelledBuf); |
| 505 | + ByteBuf second = wrappedBuffer(new byte[] { 1 }); |
| 506 | + ChannelFuture secondFuture = ch.write(second); |
| 507 | + ByteBuf third = wrappedBuffer(new byte[] { 2 }); |
| 508 | + ChannelFuture thirdFuture = ch.write(third); |
| 509 | + assertFalse(ch.isWritable()); |
| 510 | + assertTrue(cancelled.cancel(false)); |
| 511 | + |
| 512 | + // Releasing the cancelled write makes the channel writable while addFlush() runs. |
| 513 | + ch.flush(); |
| 514 | + ch.runPendingTasks(); |
| 515 | + |
| 516 | + assertFalse(ch.isOpen()); |
| 517 | + assertEquals(1, inactive.get()); |
| 518 | + assertEquals(0, cancelledBuf.refCnt()); |
| 519 | + assertInstanceOf(ClosedChannelException.class, secondFuture.cause()); |
| 520 | + assertInstanceOf(ClosedChannelException.class, thirdFuture.cause()); |
| 521 | + assertEquals(0, second.refCnt()); |
| 522 | + assertEquals(0, third.refCnt()); |
| 523 | + assertNull(ch.readOutbound()); |
| 524 | + assertFalse(ch.finish()); |
| 525 | + } |
| 526 | + |
| 527 | + private static void assertOutbound(EmbeddedChannel ch, int expected) { |
| 528 | + ByteBuf buf = ch.readOutbound(); |
| 529 | + assertNotNull(buf); |
| 530 | + try { |
| 531 | + if (expected == 0) { |
| 532 | + assertEquals(0, buf.readableBytes()); |
| 533 | + } else { |
| 534 | + assertEquals(1, buf.readableBytes()); |
| 535 | + assertEquals(expected, buf.readByte()); |
| 536 | + } |
| 537 | + } finally { |
| 538 | + buf.release(); |
| 539 | + } |
| 540 | + } |
| 541 | + |
| 542 | + @Test |
| 543 | + public void testMultipleCancelledWritesWithEarlierFlush() { |
| 544 | + final AtomicInteger writable = new AtomicInteger(); |
| 545 | + final AtomicInteger sizeAtEvent = new AtomicInteger(-1); |
| 546 | + final AtomicLong pendingAtEvent = new AtomicLong(-1); |
| 547 | + final AtomicReference<Object> currentAtEvent = new AtomicReference<Object>(); |
| 548 | + EmbeddedChannel ch = new EmbeddedChannel(); |
| 549 | + final ChannelOutboundBuffer buffer = ch.unsafe().outboundBuffer(); |
| 550 | + ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { |
| 551 | + @Override |
| 552 | + public void channelWritabilityChanged(ChannelHandlerContext ctx) { |
| 553 | + if (ctx.channel().isWritable()) { |
| 554 | + writable.incrementAndGet(); |
| 555 | + sizeAtEvent.set(buffer.size()); |
| 556 | + pendingAtEvent.set(buffer.totalPendingWriteBytes()); |
| 557 | + currentAtEvent.set(buffer.current()); |
| 558 | + } |
| 559 | + ctx.fireChannelWritabilityChanged(); |
| 560 | + } |
| 561 | + }); |
| 562 | + ch.config().setWriteBufferWaterMark(new WriteBufferWaterMark(400, 800)); |
| 563 | + |
| 564 | + // Use the ChannelOutboundBuffer directly so nothing is written and flushedEntry stays set after the flush. |
| 565 | + ByteBuf live0 = wrappedBuffer(new byte[] { 0 }); |
| 566 | + buffer.addMessage(live0, 1, ch.newPromise()); |
| 567 | + buffer.addFlush(); |
| 568 | + assertEquals(1, buffer.size()); |
| 569 | + assertSame(live0, buffer.current()); |
| 570 | + |
| 571 | + // Cancelled writes at the head, in the middle and at the tail of the second batch. |
| 572 | + ByteBuf[] cancelledBufs = new ByteBuf[3]; |
| 573 | + ByteBuf[] liveBufs = new ByteBuf[2]; |
| 574 | + ChannelPromise[] cancelledPromises = new ChannelPromise[3]; |
| 575 | + for (int i = 0; i < 3; i++) { |
| 576 | + cancelledBufs[i] = buffer().writeZero(300); |
| 577 | + cancelledPromises[i] = ch.newPromise(); |
| 578 | + buffer.addMessage(cancelledBufs[i], 300, cancelledPromises[i]); |
| 579 | + if (i < 2) { |
| 580 | + liveBufs[i] = wrappedBuffer(new byte[] { (byte) (i + 1) }); |
| 581 | + buffer.addMessage(liveBufs[i], 1, ch.newPromise()); |
| 582 | + } |
| 583 | + } |
| 584 | + assertFalse(ch.isWritable()); |
| 585 | + for (ChannelPromise promise : cancelledPromises) { |
| 586 | + assertTrue(promise.cancel(false)); |
| 587 | + } |
| 588 | + |
| 589 | + buffer.addFlush(); |
| 590 | + |
| 591 | + assertEquals(1, writable.get()); |
| 592 | + // The buffer must already be consistent when the event is fired. |
| 593 | + assertEquals(6, sizeAtEvent.get()); |
| 594 | + assertEquals(buffer.totalPendingWriteBytes(), pendingAtEvent.get()); |
| 595 | + assertSame(live0, currentAtEvent.get()); |
| 596 | + assertEquals(6, buffer.size()); |
| 597 | + assertTrue(ch.isWritable()); |
| 598 | + for (ByteBuf cancelledBuf : cancelledBufs) { |
| 599 | + assertEquals(0, cancelledBuf.refCnt()); |
| 600 | + } |
| 601 | + assertEquals(1, live0.refCnt()); |
| 602 | + for (ByteBuf liveBuf : liveBufs) { |
| 603 | + assertEquals(1, liveBuf.refCnt()); |
| 604 | + } |
| 605 | + |
| 606 | + ch.close(); |
| 607 | + assertEquals(0, live0.refCnt()); |
| 608 | + for (ByteBuf liveBuf : liveBufs) { |
| 609 | + assertEquals(0, liveBuf.refCnt()); |
| 610 | + } |
| 611 | + assertEquals(0, buffer.totalPendingWriteBytes()); |
| 612 | + } |
| 613 | + |
| 614 | + @Test |
| 615 | + public void testWriteAndFlushFromReleaseOfCancelledWrite() { |
| 616 | + final EmbeddedChannel ch = new EmbeddedChannel(); |
| 617 | + final List<ChannelFuture> reentrantWrites = new ArrayList<ChannelFuture>(); |
| 618 | + ReferenceCounted cancelledMsg = new AbstractReferenceCounted() { |
| 619 | + @Override |
| 620 | + protected void deallocate() { |
| 621 | + // Release of a cancelled write runs user code which writes and flushes on the same channel. |
| 622 | + reentrantWrites.add(ch.writeAndFlush(wrappedBuffer(new byte[] { 2 }))); |
| 623 | + } |
| 624 | + |
| 625 | + @Override |
| 626 | + public ReferenceCounted touch(Object hint) { |
| 627 | + return this; |
| 628 | + } |
| 629 | + }; |
| 630 | + |
| 631 | + ChannelFuture cancelled = ch.write(cancelledMsg); |
| 632 | + ChannelFuture second = ch.write(wrappedBuffer(new byte[] { 1 })); |
| 633 | + assertTrue(cancelled.cancel(false)); |
| 634 | + |
| 635 | + ch.flush(); |
| 636 | + |
| 637 | + assertEquals(1, reentrantWrites.size()); |
| 638 | + assertEquals(0, cancelledMsg.refCnt()); |
| 639 | + assertTrue(second.isSuccess()); |
| 640 | + assertTrue(reentrantWrites.get(0).isSuccess()); |
| 641 | + ChannelOutboundBuffer buffer = ch.unsafe().outboundBuffer(); |
| 642 | + assertTrue(buffer.isEmpty()); |
| 643 | + assertEquals(0, buffer.size()); |
| 644 | + assertEquals(0, buffer.totalPendingWriteBytes()); |
| 645 | + // The cancelled write is replaced by an empty buffer. |
| 646 | + assertOutbound(ch, 0); |
| 647 | + assertOutbound(ch, 1); |
| 648 | + assertOutbound(ch, 2); |
| 649 | + assertNull(ch.readOutbound()); |
| 650 | + |
| 651 | + assertFalse(ch.finish()); |
| 652 | + assertFalse(ch.isOpen()); |
| 653 | + } |
| 654 | + |
430 | 655 | @Test |
431 | 656 | @Timeout(value = 5000, unit = TimeUnit.MILLISECONDS) |
432 | 657 | public void testWriteTaskRejected() throws Exception { |
|