|
45 | 45 |
|
46 | 46 | public class ByteBufUtilTest { |
47 | 47 | private static final String PARAMETERIZED_NAME = "bufferType = {0}"; |
| 48 | + private final AdaptiveByteBufAllocator adaptiveByteBufAllocator = new AdaptiveByteBufAllocator(); |
48 | 49 |
|
49 | 50 | private enum BufferType { |
50 | | - DIRECT_UNPOOLED, DIRECT_POOLED, HEAP_POOLED, HEAP_UNPOOLED |
| 51 | + DIRECT_UNPOOLED, DIRECT_POOLED, DIRECT_ADAPTIVE, HEAP_POOLED, HEAP_UNPOOLED, HEAP_ADAPTIVE |
51 | 52 | } |
52 | 53 |
|
53 | 54 | private ByteBuf buffer(BufferType bufferType, int capacity) { |
@@ -61,17 +62,43 @@ private ByteBuf buffer(BufferType bufferType, int capacity) { |
61 | 62 | return PooledByteBufAllocator.DEFAULT.directBuffer(capacity); |
62 | 63 | case HEAP_POOLED: |
63 | 64 | return PooledByteBufAllocator.DEFAULT.buffer(capacity); |
| 65 | + case DIRECT_ADAPTIVE: |
| 66 | + return adaptiveByteBufAllocator.directBuffer(capacity); |
| 67 | + case HEAP_ADAPTIVE: |
| 68 | + return adaptiveByteBufAllocator.heapBuffer(capacity); |
64 | 69 | default: |
65 | 70 | throw new AssertionError("unexpected buffer type: " + bufferType); |
66 | 71 | } |
67 | 72 | } |
68 | 73 |
|
| 74 | + private CompositeByteBuf compositeByteBuf(BufferType bufferType) { |
| 75 | + switch (bufferType) { |
| 76 | + |
| 77 | + case DIRECT_UNPOOLED: |
| 78 | + return UnpooledByteBufAllocator.DEFAULT.compositeDirectBuffer(); |
| 79 | + case HEAP_UNPOOLED: |
| 80 | + return UnpooledByteBufAllocator.DEFAULT.compositeHeapBuffer(); |
| 81 | + case DIRECT_POOLED: |
| 82 | + return PooledByteBufAllocator.DEFAULT.compositeDirectBuffer(); |
| 83 | + case HEAP_POOLED: |
| 84 | + return PooledByteBufAllocator.DEFAULT.compositeHeapBuffer(); |
| 85 | + case DIRECT_ADAPTIVE: |
| 86 | + return adaptiveByteBufAllocator.compositeDirectBuffer(); |
| 87 | + case HEAP_ADAPTIVE: |
| 88 | + return adaptiveByteBufAllocator.compositeHeapBuffer(); |
| 89 | + default: |
| 90 | + throw new AssertionError("unexpected buffer type: " + bufferType); |
| 91 | + } |
| 92 | + } |
| 93 | + |
69 | 94 | public static Collection<Object[]> noUnsafe() { |
70 | 95 | return Arrays.asList(new Object[][] { |
71 | 96 | { BufferType.DIRECT_POOLED }, |
72 | 97 | { BufferType.DIRECT_UNPOOLED }, |
73 | 98 | { BufferType.HEAP_POOLED }, |
74 | | - { BufferType.HEAP_UNPOOLED } |
| 99 | + { BufferType.HEAP_UNPOOLED }, |
| 100 | + { BufferType.DIRECT_ADAPTIVE }, |
| 101 | + { BufferType.HEAP_ADAPTIVE } |
75 | 102 | }); |
76 | 103 | } |
77 | 104 |
|
@@ -403,7 +430,7 @@ public void testWriteUsAsciiComposite(BufferType bufferType) { |
403 | 430 | String usAscii = "NettyRocks"; |
404 | 431 | ByteBuf buf = buffer(bufferType, 16); |
405 | 432 | buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); |
406 | | - ByteBuf buf2 = Unpooled.compositeBuffer().addComponent( |
| 433 | + ByteBuf buf2 = compositeByteBuf(bufferType).addComponent( |
407 | 434 | buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)); |
408 | 435 | // write some byte so we start writing with an offset. |
409 | 436 | buf2.writeByte(1); |
@@ -422,7 +449,7 @@ public void testWriteUsAsciiCompositeWrapped(BufferType bufferType) { |
422 | 449 | String usAscii = "NettyRocks"; |
423 | 450 | ByteBuf buf = buffer(bufferType, 16); |
424 | 451 | buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); |
425 | | - ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent( |
| 452 | + ByteBuf buf2 = new WrappedCompositeByteBuf(compositeByteBuf(bufferType).addComponent( |
426 | 453 | buffer(bufferType, 8)).addComponent(buffer(bufferType, 24))); |
427 | 454 | // write some byte so we start writing with an offset. |
428 | 455 | buf2.writeByte(1); |
@@ -456,7 +483,7 @@ public void testWriteUtf8Composite(BufferType bufferType) { |
456 | 483 | String utf8 = "Some UTF-8 like äÄ∏ŒŒ"; |
457 | 484 | ByteBuf buf = buffer(bufferType, 16); |
458 | 485 | buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8)); |
459 | | - ByteBuf buf2 = Unpooled.compositeBuffer().addComponent( |
| 486 | + ByteBuf buf2 = compositeByteBuf(bufferType).addComponent( |
460 | 487 | buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)); |
461 | 488 | // write some byte so we start writing with an offset. |
462 | 489 | buf2.writeByte(1); |
@@ -475,7 +502,7 @@ public void testWriteUtf8CompositeWrapped(BufferType bufferType) { |
475 | 502 | String utf8 = "Some UTF-8 like äÄ∏ŒŒ"; |
476 | 503 | ByteBuf buf = buffer(bufferType, 16); |
477 | 504 | buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8)); |
478 | | - ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent( |
| 505 | + ByteBuf buf2 = new WrappedCompositeByteBuf(compositeByteBuf(bufferType).addComponent( |
479 | 506 | buffer(bufferType, 8)).addComponent(buffer(bufferType, 24))); |
480 | 507 | // write some byte so we start writing with an offset. |
481 | 508 | buf2.writeByte(1); |
@@ -816,7 +843,7 @@ private static void testDecodeString(String text, Charset charset) { |
816 | 843 | @ParameterizedTest(name = PARAMETERIZED_NAME) |
817 | 844 | @MethodSource("noUnsafe") |
818 | 845 | public void testToStringDoesNotThrowIndexOutOfBounds(BufferType bufferType) { |
819 | | - CompositeByteBuf buffer = Unpooled.compositeBuffer(); |
| 846 | + CompositeByteBuf buffer = compositeByteBuf(bufferType); |
820 | 847 | try { |
821 | 848 | byte[] bytes = "1234".getBytes(CharsetUtil.UTF_8); |
822 | 849 | buffer.addComponent(buffer(bufferType, bytes.length).writeBytes(bytes)); |
|