@@ -81,7 +81,7 @@ public void setUp() {
81
81
@ Override
82
82
void addInput (ByteBufAllocator alloc , CompositeByteBuf composite , ByteBuf in ) {
83
83
// To limit the testing scope to NettyAdaptiveCumulator.cumulate(), always compose
84
- composite .addComponent (true , in );
84
+ composite .addFlattenedComponents (true , in );
85
85
}
86
86
};
87
87
@@ -122,7 +122,7 @@ public void cumulate_contiguousCumulation_newCompositeFromContiguousAndInput() {
122
122
123
123
@ Test
124
124
public void cumulate_compositeCumulation_inputAppendedAsANewComponent () {
125
- CompositeByteBuf composite = alloc .compositeBuffer ().addComponent (true , contiguous );
125
+ CompositeByteBuf composite = alloc .compositeBuffer ().addFlattenedComponents (true , contiguous );
126
126
assertSame (composite , cumulator .cumulate (alloc , composite , in ));
127
127
assertEquals (DATA_INITIAL , composite .component (0 ).toString (US_ASCII ));
128
128
assertEquals (DATA_INCOMING , composite .component (1 ).toString (US_ASCII ));
@@ -136,7 +136,7 @@ public void cumulate_compositeCumulation_inputAppendedAsANewComponent() {
136
136
137
137
@ Test
138
138
public void cumulate_compositeCumulation_inputReleasedOnError () {
139
- CompositeByteBuf composite = alloc .compositeBuffer ().addComponent (true , contiguous );
139
+ CompositeByteBuf composite = alloc .compositeBuffer ().addFlattenedComponents (true , contiguous );
140
140
try {
141
141
throwingCumulator .cumulate (alloc , composite , in );
142
142
fail ("Cumulator didn't throw" );
@@ -208,8 +208,8 @@ public void setUp() {
208
208
in = ByteBufUtil .writeAscii (alloc , inData );
209
209
tail = ByteBufUtil .writeAscii (alloc , tailData );
210
210
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 );
213
213
}
214
214
215
215
@ After
@@ -345,7 +345,7 @@ public void mergeWithCompositeTail_tailExpandable_write() {
345
345
assertThat (in .readableBytes ()).isAtMost (tail .writableBytes ());
346
346
347
347
// All fits, so tail capacity must stay the same.
348
- composite .addComponent (true , tail );
348
+ composite .addFlattenedComponents (true , tail );
349
349
assertTailExpanded (EXPECTED_TAIL_DATA , fitCapacity );
350
350
}
351
351
@@ -362,7 +362,7 @@ public void mergeWithCompositeTail_tailExpandable_fastWrite() {
362
362
alloc .calculateNewCapacity (EXPECTED_TAIL_DATA .length (), Integer .MAX_VALUE );
363
363
364
364
// Tail capacity is extended to its fast capacity.
365
- composite .addComponent (true , tail );
365
+ composite .addFlattenedComponents (true , tail );
366
366
assertTailExpanded (EXPECTED_TAIL_DATA , tailFastCapacity );
367
367
}
368
368
@@ -372,7 +372,7 @@ public void mergeWithCompositeTail_tailExpandable_reallocateInMemory() {
372
372
@ SuppressWarnings ("InlineMeInliner" ) // Requires Java 11
373
373
String inSuffixOverFastBytes = Strings .repeat ("a" , tailFastCapacity + 1 );
374
374
int newTailSize = tail .readableBytes () + inSuffixOverFastBytes .length ();
375
- composite .addComponent (true , tail );
375
+ composite .addFlattenedComponents (true , tail );
376
376
377
377
// Make input larger than tailFastCapacity
378
378
in .writeCharSequence (inSuffixOverFastBytes , US_ASCII );
@@ -386,6 +386,9 @@ public void mergeWithCompositeTail_tailExpandable_reallocateInMemory() {
386
386
}
387
387
388
388
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
+ }
389
392
int originalNumComponents = composite .numComponents ();
390
393
391
394
// Handle the case when reader index is beyond all readable bytes of the cumulation.
@@ -435,21 +438,21 @@ public void mergeWithCompositeTail_tailNotExpandable_maxCapacityReached() {
435
438
@ SuppressWarnings ("InlineMeInliner" ) // Requires Java 11
436
439
String tailSuffixFullCapacity = Strings .repeat ("a" , tail .maxWritableBytes ());
437
440
tail .writeCharSequence (tailSuffixFullCapacity , US_ASCII );
438
- composite .addComponent (true , tail );
441
+ composite .addFlattenedComponents (true , tail );
439
442
assertTailReplaced ();
440
443
}
441
444
442
445
@ Test
443
446
public void mergeWithCompositeTail_tailNotExpandable_shared () {
444
447
tail .retain ();
445
- composite .addComponent (true , tail );
448
+ composite .addFlattenedComponents (true , tail );
446
449
assertTailReplaced ();
447
450
tail .release ();
448
451
}
449
452
450
453
@ Test
451
454
public void mergeWithCompositeTail_tailNotExpandable_readOnly () {
452
- composite .addComponent (true , tail .asReadOnly ());
455
+ composite .addFlattenedComponents (true , tail .asReadOnly ());
453
456
assertTailReplaced ();
454
457
}
455
458
@@ -527,7 +530,8 @@ public void mergeWithCompositeTail_tailExpandable_mergedReleaseOnThrow() {
527
530
CompositeByteBuf compositeThrows = new CompositeByteBuf (alloc , false , Integer .MAX_VALUE ,
528
531
tail ) {
529
532
@ Override
530
- public CompositeByteBuf addComponent (boolean increaseWriterIndex , ByteBuf buffer ) {
533
+ public CompositeByteBuf addFlattenedComponents (boolean increaseWriterIndex ,
534
+ ByteBuf buffer ) {
531
535
throw expectedError ;
532
536
}
533
537
};
@@ -560,7 +564,8 @@ public void mergeWithCompositeTail_tailNotExpandable_mergedReleaseOnThrow() {
560
564
CompositeByteBuf compositeRo = new CompositeByteBuf (alloc , false , Integer .MAX_VALUE ,
561
565
tail .asReadOnly ()) {
562
566
@ Override
563
- public CompositeByteBuf addComponent (boolean increaseWriterIndex , ByteBuf buffer ) {
567
+ public CompositeByteBuf addFlattenedComponents (boolean increaseWriterIndex ,
568
+ ByteBuf buffer ) {
564
569
throw expectedError ;
565
570
}
566
571
};
@@ -614,16 +619,20 @@ public void mergeWithCompositeTail_outOfSyncComposite() {
614
619
ByteBuf buf = alloc .buffer (32 ).writeBytes ("---01234" .getBytes (US_ASCII ));
615
620
616
621
// 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 );
618
623
// Read composite1 buf to the beginning of the numbers.
619
624
assertThat (composite1 .readCharSequence (3 , US_ASCII ).toString ()).isEqualTo ("---" );
620
625
621
626
// Wrap composite1 into another cumulation. This is similar to
622
627
// what NettyAdaptiveCumulator.cumulate() does in the case the cumulation has refCnt != 1.
623
628
CompositeByteBuf composite2 =
624
- alloc .compositeBuffer (8 ).addComponent (true , composite1 );
629
+ alloc .compositeBuffer (8 ).addFlattenedComponents (true , composite1 );
625
630
assertThat (composite2 .toString (US_ASCII )).isEqualTo ("01234" );
626
631
632
+ if (!GrpcHttp2ConnectionHandler .usingPre4_1_111_Netty ()) {
633
+ return ; // Netty 4.1.111 doesn't work with NettyAdaptiveCumulator
634
+ }
635
+
627
636
// The previous operation does not adjust the read indexes of the underlying buffers,
628
637
// only the internal Component offsets. When the cumulator attempts to append the input to
629
638
// 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() {
637
646
CompositeByteBuf cumulation = (CompositeByteBuf ) cumulator .cumulate (alloc , composite2 ,
638
647
ByteBufUtil .writeAscii (alloc , "56789" ));
639
648
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 );
652
649
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" );
661
656
}
662
657
}
663
658
}
0 commit comments