@@ -585,4 +585,57 @@ public CompositeByteBuf addFlattenedComponents(boolean increaseWriterIndex, |
585 | 585 | } |
586 | 586 | } |
587 | 587 | } |
| 588 | + |
| 589 | + /** |
| 590 | + * Miscellaneous tests for {@link NettyAdaptiveCumulator#mergeWithCompositeTail} that don't |
| 591 | + * fit into {@link MergeWithCompositeTailTests}, and require custom-crafted scenarios. |
| 592 | + */ |
| 593 | + @RunWith(JUnit4.class) |
| 594 | + public static class MergeWithCompositeTailMiscTests { |
| 595 | + private final ByteBufAllocator alloc = new PooledByteBufAllocator(); |
| 596 | + |
| 597 | + /** |
| 598 | + * Test the issue with {@link CompositeByteBuf#component(int)} returning a ByteBuf with |
| 599 | + * the indexes out-of-sync with {@code CompositeByteBuf.Component} offsets. |
| 600 | + */ |
| 601 | + @Test |
| 602 | + public void mergeWithCompositeTail_outOfSyncComposite() { |
| 603 | + NettyAdaptiveCumulator cumulator = new NettyAdaptiveCumulator(1024); |
| 604 | + |
| 605 | + // Create underlying buffer spacious enough for the test data. |
| 606 | + ByteBuf buf = alloc.buffer(32).writeBytes("---01234".getBytes(US_ASCII)); |
| 607 | + |
| 608 | + // Start with a regular cumulation and add the buf as the only component. |
| 609 | + CompositeByteBuf composite1 = alloc.compositeBuffer(8).addFlattenedComponents(true, buf); |
| 610 | + // Read composite1 buf to the beginning of the numbers. |
| 611 | + assertThat(composite1.readCharSequence(3, US_ASCII).toString()).isEqualTo("---"); |
| 612 | + |
| 613 | + // Wrap composite1 into another cumulation. This is similar to |
| 614 | + // what NettyAdaptiveCumulator.cumulate() does in the case the cumulation has refCnt != 1. |
| 615 | + CompositeByteBuf composite2 = |
| 616 | + alloc.compositeBuffer(8).addFlattenedComponents(true, composite1); |
| 617 | + assertThat(composite2.toString(US_ASCII)).isEqualTo("01234"); |
| 618 | + |
| 619 | + // The previous operation does not adjust the read indexes of the underlying buffers, |
| 620 | + // only the internal Component offsets. When the cumulator attempts to append the input to |
| 621 | + // the tail buffer, it extracts it from the cumulation, writes to it, and then adds it back. |
| 622 | + // Because the readerIndex on the tail buffer is not adjusted during the read operation |
| 623 | + // on the CompositeByteBuf, adding the tail back results in the discarded bytes of the tail |
| 624 | + // to be added back to the cumulator as if they were never read. |
| 625 | + // |
| 626 | + // If the reader index of the tail is not manually corrected, the resulting |
| 627 | + // cumulation will contain the discarded part of the tail: "---". |
| 628 | + // If it's corrected, it will only contain the numbers. |
| 629 | + CompositeByteBuf cumulation = (CompositeByteBuf) cumulator.cumulate(alloc, composite2, |
| 630 | + ByteBufUtil.writeAscii(alloc, "56789")); |
| 631 | + assertThat(cumulation.toString(US_ASCII)).isEqualTo("0123456789"); |
| 632 | + |
| 633 | + // Correctness check: we still have a single component, and this component is still the |
| 634 | + // original underlying buffer. |
| 635 | + assertThat(cumulation.numComponents()).isEqualTo(1); |
| 636 | + // Replace '2' with '*', and '8' with '*'. |
| 637 | + buf.setByte(5, '*').setByte(11, '$'); |
| 638 | + assertThat(cumulation.toString(US_ASCII)).isEqualTo("01*34567$9"); |
| 639 | + } |
| 640 | + } |
588 | 641 | } |