Add the unit test of ChannelOutboundBuffer (#10180)

Motivation:

Parameter maxCount needs the unit test.

Modifications:

1. Change the conditional statement to avoid the ineffective maxCount (enhance the robustness of the code merely).
2. Add the unit test for maxCount.

Result:

Enable this parameter to be tested.
This commit is contained in:
Cenjie Ho 2020-04-15 14:22:51 +08:00 committed by Norman Maurer
parent 0905180ccd
commit 65aa270edc
2 changed files with 26 additions and 1 deletions

View File

@ -451,7 +451,7 @@ public final class ChannelOutboundBuffer {
// branch is not very likely to get hit very frequently.
nioBufferCount = nioBuffers(entry, buf, nioBuffers, nioBufferCount, maxCount);
}
if (nioBufferCount == maxCount) {
if (nioBufferCount >= maxCount) {
break;
}
}

View File

@ -120,6 +120,31 @@ public class ChannelOutboundBufferTest {
buf.release();
}
@Test
public void testNioBuffersMaxCount() {
TestChannel channel = new TestChannel();
ChannelOutboundBuffer buffer = new ChannelOutboundBuffer(channel);
CompositeByteBuf comp = compositeBuffer(256);
ByteBuf buf = directBuffer().writeBytes("buf1".getBytes(CharsetUtil.US_ASCII));
for (int i = 0; i < 65; i++) {
comp.addComponent(true, buf.copy());
}
assertEquals(65, comp.nioBufferCount());
buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise());
assertEquals("Should still be 0 as not flushed yet", 0, buffer.nioBufferCount());
buffer.addFlush();
final int maxCount = 10; // less than comp.nioBufferCount()
ByteBuffer[] buffers = buffer.nioBuffers(maxCount, Integer.MAX_VALUE);
assertTrue("Should not be greater than maxCount", buffer.nioBufferCount() <= maxCount);
for (int i = 0; i < buffer.nioBufferCount(); i++) {
assertEquals(buffers[i], buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()));
}
release(buffer);
buf.release();
}
private static void release(ChannelOutboundBuffer buffer) {
for (;;) {
if (!buffer.remove()) {