Test that CompositeByteBuf releases its components

Motivation:

In order to prevent a regression, add test case for a bug that caused a CompositeByteBuf to not release its components.

Modifications:

Add a test case that asserts a CompositeByteBuf's component buffers have indeed been released.

Result:

AbstractCompositeByteBuf gains a test case that will prevent future regressions.
This commit is contained in:
Kevin Herron 2016-05-20 10:07:46 -07:00 committed by Norman Maurer
parent 2562ef7cbe
commit ee3c8e0580

View File

@ -15,6 +15,7 @@
*/
package io.netty.buffer;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;
import java.nio.ByteBuffer;
@ -930,4 +931,34 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
cbuf.release();
}
}
@Test
public void testReleasesItsComponents() {
ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); // 1
buffer.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
ByteBuf s1 = buffer.readSlice(2).retain(); // 2
ByteBuf s2 = s1.readSlice(2).retain(); // 3
ByteBuf s3 = s2.readSlice(2).retain(); // 4
ByteBuf s4 = s3.readSlice(2).retain(); // 5
ByteBuf composite = PooledByteBufAllocator.DEFAULT.compositeBuffer()
.addComponent(s1)
.addComponents(s2, s3, s4)
.order(ByteOrder.LITTLE_ENDIAN);
assertEquals(composite.refCnt(), 1);
assertEquals(buffer.refCnt(), 5);
// releasing composite should release the 4 components
ReferenceCountUtil.release(composite);
assertEquals(composite.refCnt(), 0);
assertEquals(buffer.refCnt(), 1);
// last remaining ref to buffer
ReferenceCountUtil.release(buffer);
assertEquals(buffer.refCnt(), 0);
}
}