@@ -81,7 +81,7 @@ public void setUp() {
8181 @ Override
8282 void addInput (ByteBufAllocator alloc , CompositeByteBuf composite , ByteBuf in ) {
8383 // To limit the testing scope to NettyAdaptiveCumulator.cumulate(), always compose
84- composite .addComponent (true , in );
84+ composite .addFlattenedComponents (true , in );
8585 }
8686 };
8787
@@ -122,7 +122,7 @@ public void cumulate_contiguousCumulation_newCompositeFromContiguousAndInput() {
122122
123123 @ Test
124124 public void cumulate_compositeCumulation_inputAppendedAsANewComponent () {
125- CompositeByteBuf composite = alloc .compositeBuffer ().addComponent (true , contiguous );
125+ CompositeByteBuf composite = alloc .compositeBuffer ().addFlattenedComponents (true , contiguous );
126126 assertSame (composite , cumulator .cumulate (alloc , composite , in ));
127127 assertEquals (DATA_INITIAL , composite .component (0 ).toString (US_ASCII ));
128128 assertEquals (DATA_INCOMING , composite .component (1 ).toString (US_ASCII ));
@@ -136,7 +136,7 @@ public void cumulate_compositeCumulation_inputAppendedAsANewComponent() {
136136
137137 @ Test
138138 public void cumulate_compositeCumulation_inputReleasedOnError () {
139- CompositeByteBuf composite = alloc .compositeBuffer ().addComponent (true , contiguous );
139+ CompositeByteBuf composite = alloc .compositeBuffer ().addFlattenedComponents (true , contiguous );
140140 try {
141141 throwingCumulator .cumulate (alloc , composite , in );
142142 fail ("Cumulator didn't throw" );
@@ -208,8 +208,8 @@ public void setUp() {
208208 in = ByteBufUtil .writeAscii (alloc , inData );
209209 tail = ByteBufUtil .writeAscii (alloc , tailData );
210210 composite = alloc .compositeBuffer (Integer .MAX_VALUE );
211- // Note that addComponent () will not add a new component when tail is not readable.
212- composite .addComponent (true , tail );
211+ // Note that addFlattenedComponents () will not add a new component when tail is not readable.
212+ composite .addFlattenedComponents (true , tail );
213213 }
214214
215215 @ After
@@ -345,7 +345,7 @@ public void mergeWithCompositeTail_tailExpandable_write() {
345345 assertThat (in .readableBytes ()).isAtMost (tail .writableBytes ());
346346
347347 // All fits, so tail capacity must stay the same.
348- composite .addComponent (true , tail );
348+ composite .addFlattenedComponents (true , tail );
349349 assertTailExpanded (EXPECTED_TAIL_DATA , fitCapacity );
350350 }
351351
@@ -362,7 +362,7 @@ public void mergeWithCompositeTail_tailExpandable_fastWrite() {
362362 alloc .calculateNewCapacity (EXPECTED_TAIL_DATA .length (), Integer .MAX_VALUE );
363363
364364 // Tail capacity is extended to its fast capacity.
365- composite .addComponent (true , tail );
365+ composite .addFlattenedComponents (true , tail );
366366 assertTailExpanded (EXPECTED_TAIL_DATA , tailFastCapacity );
367367 }
368368
@@ -372,7 +372,7 @@ public void mergeWithCompositeTail_tailExpandable_reallocateInMemory() {
372372 @ SuppressWarnings ("InlineMeInliner" ) // Requires Java 11
373373 String inSuffixOverFastBytes = Strings .repeat ("a" , tailFastCapacity + 1 );
374374 int newTailSize = tail .readableBytes () + inSuffixOverFastBytes .length ();
375- composite .addComponent (true , tail );
375+ composite .addFlattenedComponents (true , tail );
376376
377377 // Make input larger than tailFastCapacity
378378 in .writeCharSequence (inSuffixOverFastBytes , US_ASCII );
@@ -386,6 +386,9 @@ public void mergeWithCompositeTail_tailExpandable_reallocateInMemory() {
386386 }
387387
388388 private void assertTailExpanded (String expectedTailReadableData , int expectedNewTailCapacity ) {
389+ if (!GrpcHttp2ConnectionHandler .usingPre4_1_111_Netty ()) {
390+ return ; // Netty 4.1.111 doesn't work with NettyAdaptiveCumulator
391+ }
389392 int originalNumComponents = composite .numComponents ();
390393
391394 // Handle the case when reader index is beyond all readable bytes of the cumulation.
@@ -435,21 +438,21 @@ public void mergeWithCompositeTail_tailNotExpandable_maxCapacityReached() {
435438 @ SuppressWarnings ("InlineMeInliner" ) // Requires Java 11
436439 String tailSuffixFullCapacity = Strings .repeat ("a" , tail .maxWritableBytes ());
437440 tail .writeCharSequence (tailSuffixFullCapacity , US_ASCII );
438- composite .addComponent (true , tail );
441+ composite .addFlattenedComponents (true , tail );
439442 assertTailReplaced ();
440443 }
441444
442445 @ Test
443446 public void mergeWithCompositeTail_tailNotExpandable_shared () {
444447 tail .retain ();
445- composite .addComponent (true , tail );
448+ composite .addFlattenedComponents (true , tail );
446449 assertTailReplaced ();
447450 tail .release ();
448451 }
449452
450453 @ Test
451454 public void mergeWithCompositeTail_tailNotExpandable_readOnly () {
452- composite .addComponent (true , tail .asReadOnly ());
455+ composite .addFlattenedComponents (true , tail .asReadOnly ());
453456 assertTailReplaced ();
454457 }
455458
@@ -527,7 +530,8 @@ public void mergeWithCompositeTail_tailExpandable_mergedReleaseOnThrow() {
527530 CompositeByteBuf compositeThrows = new CompositeByteBuf (alloc , false , Integer .MAX_VALUE ,
528531 tail ) {
529532 @ Override
530- public CompositeByteBuf addComponent (boolean increaseWriterIndex , ByteBuf buffer ) {
533+ public CompositeByteBuf addFlattenedComponents (boolean increaseWriterIndex ,
534+ ByteBuf buffer ) {
531535 throw expectedError ;
532536 }
533537 };
@@ -560,7 +564,8 @@ public void mergeWithCompositeTail_tailNotExpandable_mergedReleaseOnThrow() {
560564 CompositeByteBuf compositeRo = new CompositeByteBuf (alloc , false , Integer .MAX_VALUE ,
561565 tail .asReadOnly ()) {
562566 @ Override
563- public CompositeByteBuf addComponent (boolean increaseWriterIndex , ByteBuf buffer ) {
567+ public CompositeByteBuf addFlattenedComponents (boolean increaseWriterIndex ,
568+ ByteBuf buffer ) {
564569 throw expectedError ;
565570 }
566571 };
@@ -614,16 +619,20 @@ public void mergeWithCompositeTail_outOfSyncComposite() {
614619 ByteBuf buf = alloc .buffer (32 ).writeBytes ("---01234" .getBytes (US_ASCII ));
615620
616621 // Start with a regular cumulation and add the buf as the only component.
617- CompositeByteBuf composite1 = alloc .compositeBuffer (8 ).addComponent (true , buf );
622+ CompositeByteBuf composite1 = alloc .compositeBuffer (8 ).addFlattenedComponents (true , buf );
618623 // Read composite1 buf to the beginning of the numbers.
619624 assertThat (composite1 .readCharSequence (3 , US_ASCII ).toString ()).isEqualTo ("---" );
620625
621626 // Wrap composite1 into another cumulation. This is similar to
622627 // what NettyAdaptiveCumulator.cumulate() does in the case the cumulation has refCnt != 1.
623628 CompositeByteBuf composite2 =
624- alloc .compositeBuffer (8 ).addComponent (true , composite1 );
629+ alloc .compositeBuffer (8 ).addFlattenedComponents (true , composite1 );
625630 assertThat (composite2 .toString (US_ASCII )).isEqualTo ("01234" );
626631
632+ if (!GrpcHttp2ConnectionHandler .usingPre4_1_111_Netty ()) {
633+ return ; // Netty 4.1.111 doesn't work with NettyAdaptiveCumulator
634+ }
635+
627636 // The previous operation does not adjust the read indexes of the underlying buffers,
628637 // only the internal Component offsets. When the cumulator attempts to append the input to
629638 // the tail buffer, it extracts it from the cumulation, writes to it, and then adds it back.
@@ -637,27 +646,13 @@ public void mergeWithCompositeTail_outOfSyncComposite() {
637646 CompositeByteBuf cumulation = (CompositeByteBuf ) cumulator .cumulate (alloc , composite2 ,
638647 ByteBufUtil .writeAscii (alloc , "56789" ));
639648 assertThat (cumulation .toString (US_ASCII )).isEqualTo ("0123456789" );
640- }
641-
642- @ Test
643- public void mergeWithNonCompositeTail () {
644- NettyAdaptiveCumulator cumulator = new NettyAdaptiveCumulator (1024 );
645- ByteBufAllocator alloc = new PooledByteBufAllocator ();
646- ByteBuf buf = alloc .buffer ().writeBytes ("tail" .getBytes (US_ASCII ));
647- ByteBuf in = alloc .buffer ().writeBytes ("-012345" .getBytes (US_ASCII ));
648-
649- CompositeByteBuf composite = alloc .compositeBuffer ().addComponent (true , buf );
650-
651- CompositeByteBuf cumulation = (CompositeByteBuf ) cumulator .cumulate (alloc , composite , in );
652649
653- assertEquals ("tail-012345" , cumulation .toString (US_ASCII ));
654- assertEquals (0 , in .refCnt ());
655- assertEquals (1 , cumulation .numComponents ());
656-
657- buf .setByte (2 , '*' ).setByte (7 , '$' );
658- assertEquals ("ta*l-01$345" , cumulation .toString (US_ASCII ));
659-
660- composite .release ();
650+ // Correctness check: we still have a single component, and this component is still the
651+ // original underlying buffer.
652+ assertThat (cumulation .numComponents ()).isEqualTo (1 );
653+ // Replace '2' with '*', and '8' with '$'.
654+ buf .setByte (5 , '*' ).setByte (11 , '$' );
655+ assertThat (cumulation .toString (US_ASCII )).isEqualTo ("01*34567$9" );
661656 }
662657 }
663658}
0 commit comments