|
21 | 21 | import io.netty.channel.ChannelPromise; |
22 | 22 | import io.netty.channel.embedded.EmbeddedChannel; |
23 | 23 | import org.junit.jupiter.params.ParameterizedTest; |
24 | | -import org.junit.jupiter.params.provider.ValueSource; |
| 24 | +import org.junit.jupiter.params.provider.EnumSource; |
25 | 25 |
|
26 | 26 | import java.nio.ByteBuffer; |
| 27 | +import java.util.function.Function; |
| 28 | +import java.util.function.Supplier; |
27 | 29 |
|
28 | 30 | import static org.junit.jupiter.api.Assertions.assertEquals; |
29 | 31 | import static org.junit.jupiter.api.Assertions.assertFalse; |
30 | 32 | import static org.junit.jupiter.api.Assertions.assertTrue; |
31 | 33 |
|
32 | 34 | public class SslHandlerCoalescingBufferQueueTest { |
| 35 | + static final Supplier<ByteBuf> BYTEBUF_SUPPLIER = new Supplier<ByteBuf>() { |
| 36 | + @Override |
| 37 | + public ByteBuf get() { |
| 38 | + ByteBuf buf = Unpooled.directBuffer(128); |
| 39 | + buf.writeLong(1); |
| 40 | + buf.writeLong(2); |
| 41 | + return buf; |
| 42 | + } |
| 43 | + }; |
| 44 | + static final Function<ByteBuf, ByteBuf> NO_WRAPPER = new Function<ByteBuf, ByteBuf>() { |
| 45 | + @Override |
| 46 | + public ByteBuf apply(ByteBuf byteBuf) { |
| 47 | + return byteBuf; |
| 48 | + } |
| 49 | + }; |
| 50 | + static final Function<ByteBuf, ByteBuf> DUPLICATE_WRAPPER = new Function<ByteBuf, ByteBuf>() { |
| 51 | + @Override |
| 52 | + public ByteBuf apply(ByteBuf byteBuf) { |
| 53 | + return byteBuf.duplicate(); |
| 54 | + } |
| 55 | + }; |
| 56 | + static final Function<ByteBuf, ByteBuf> SLICE_WRAPPER = new Function<ByteBuf, ByteBuf>() { |
| 57 | + @Override |
| 58 | + public ByteBuf apply(ByteBuf byteBuf) { |
| 59 | + return byteBuf.slice(); |
| 60 | + } |
| 61 | + }; |
| 62 | + static ByteBuffer createNioBuffer() { |
| 63 | + ByteBuffer nioBuffer = ByteBuffer.allocateDirect(128); |
| 64 | + nioBuffer.putLong(1); |
| 65 | + nioBuffer.putLong(2); |
| 66 | + nioBuffer.position(nioBuffer.limit()); |
| 67 | + nioBuffer.flip(); |
| 68 | + return nioBuffer; |
| 69 | + } |
| 70 | + |
| 71 | + enum CumulationTestScenario { |
| 72 | + BASIC_NIO_BUFFER(new Supplier() { |
| 73 | + @Override |
| 74 | + public ByteBuf get() { |
| 75 | + return Unpooled.wrappedBuffer(createNioBuffer()); |
| 76 | + } |
| 77 | + }, NO_WRAPPER), |
| 78 | + READ_ONLY_AND_DUPLICATE_NIO_BUFFER(new Supplier() { |
| 79 | + @Override |
| 80 | + public ByteBuf get() { |
| 81 | + return Unpooled.wrappedBuffer(createNioBuffer().asReadOnlyBuffer()); |
| 82 | + } |
| 83 | + }, DUPLICATE_WRAPPER), |
| 84 | + BASIC_DIRECT_BUFFER(BYTEBUF_SUPPLIER, NO_WRAPPER), |
| 85 | + DUPLICATE_DIRECT_BUFFER(BYTEBUF_SUPPLIER, DUPLICATE_WRAPPER), |
| 86 | + SLICED_DIRECT_BUFFER(BYTEBUF_SUPPLIER, SLICE_WRAPPER); |
| 87 | + |
| 88 | + final Supplier<ByteBuf> bufferSupplier; |
| 89 | + final Function<ByteBuf, ByteBuf> bufferWrapper; |
| 90 | + |
| 91 | + CumulationTestScenario(Supplier<ByteBuf> bufferSupplier, Function<ByteBuf, ByteBuf> bufferWrapper) { |
| 92 | + this.bufferSupplier = bufferSupplier; |
| 93 | + this.bufferWrapper = bufferWrapper; |
| 94 | + } |
| 95 | + } |
33 | 96 |
|
34 | 97 | @ParameterizedTest |
35 | | - @ValueSource(booleans = { true, false }) |
36 | | - public void testCumulation(boolean readOnlyAndDuplicate) { |
| 98 | + @EnumSource(CumulationTestScenario.class) |
| 99 | + public void testCumulation(CumulationTestScenario testScenario) { |
37 | 100 | EmbeddedChannel channel = new EmbeddedChannel(); |
38 | 101 | SslHandlerCoalescingBufferQueue queue = new SslHandlerCoalescingBufferQueue(channel, 16, false) { |
39 | 102 | @Override |
@@ -42,34 +105,29 @@ protected int wrapDataSize() { |
42 | 105 | } |
43 | 106 | }; |
44 | 107 |
|
45 | | - ByteBuffer nioBuffer = ByteBuffer.allocateDirect(128); |
46 | | - nioBuffer.putLong(0); |
47 | | - nioBuffer.putLong(0); |
48 | | - nioBuffer.putLong(0); |
49 | | - nioBuffer.flip(); |
50 | | - ByteBuf first; |
51 | | - if (readOnlyAndDuplicate) { |
52 | | - first = Unpooled.wrappedBuffer(nioBuffer.asReadOnlyBuffer()).duplicate(); |
53 | | - first.writerIndex(8); |
54 | | - } else { |
55 | | - first = Unpooled.wrappedBuffer(nioBuffer); |
56 | | - first.writerIndex(8); |
57 | | - } |
| 108 | + ByteBuf original = testScenario.bufferSupplier.get(); |
| 109 | + original.writerIndex(8); |
| 110 | + ByteBuf first = testScenario.bufferWrapper.apply(original); |
| 111 | + first.retain(); |
58 | 112 | queue.add(first); |
59 | | - ByteBuf second = Unpooled.copyLong(1); |
| 113 | + ByteBuf second = Unpooled.copyLong(3); |
60 | 114 | queue.add(second); |
61 | 115 |
|
62 | 116 | ChannelPromise promise = channel.newPromise(); |
63 | 117 | assertFalse(queue.isEmpty()); |
64 | 118 | ByteBuf buffer = queue.remove(UnpooledByteBufAllocator.DEFAULT, 128, promise); |
65 | 119 | try { |
66 | 120 | assertEquals(16, buffer.readableBytes()); |
67 | | - assertEquals(0, buffer.readLong()); |
68 | 121 | assertEquals(1, buffer.readLong()); |
| 122 | + assertEquals(3, buffer.readLong()); |
69 | 123 | } finally { |
70 | 124 | buffer.release(); |
71 | 125 | } |
72 | 126 | assertTrue(queue.isEmpty()); |
| 127 | + assertEquals(8, original.writerIndex()); |
| 128 | + original.writerIndex(original.capacity()); |
| 129 | + assertEquals(2, original.getLong(8)); |
| 130 | + first.release(); |
73 | 131 | assertEquals(0, first.refCnt()); |
74 | 132 | assertEquals(0, second.refCnt()); |
75 | 133 | } |
|