@Test public void testReferenceCounts3() { ByteBuf c1 = buffer().writeByte(1); ByteBuf c2 = buffer().writeByte(2).retain(); ByteBuf c3 = buffer().writeByte(3).retain(2); CompositeByteBuf buf = freeLater(compositeBuffer()); assertThat(buf.refCnt(), is(1)); List<ByteBuf> components = new ArrayList<ByteBuf>(); Collections.addAll(components, c1, c2, c3); buf.addComponents(components); // Ensure that c[123]'s refCount did not change. assertThat(c1.refCnt(), is(1)); assertThat(c2.refCnt(), is(2)); assertThat(c3.refCnt(), is(3)); assertThat(buf.component(0).refCnt(), is(1)); assertThat(buf.component(1).refCnt(), is(2)); assertThat(buf.component(2).refCnt(), is(3)); c3.release(2); c2.release(); }
@Test public void testRemoveLastComponent() { CompositeByteBuf buf = freeLater(compositeBuffer()); buf.addComponent(wrappedBuffer(new byte[] {1, 2})); assertEquals(1, buf.numComponents()); buf.removeComponent(0); assertEquals(0, buf.numComponents()); }
@Test public void testComponentMustBeSlice() { CompositeByteBuf buf = freeLater(compositeBuffer()); buf.addComponent(buffer(4).setIndex(1, 3)); assertThat(buf.component(0), is(instanceOf(SlicedByteBuf.class))); assertThat(buf.component(0).capacity(), is(2)); assertThat(buf.component(0).maxCapacity(), is(2)); }
@Test public void testReferenceCounts2() { ByteBuf c1 = buffer().writeByte(1); ByteBuf c2 = buffer().writeByte(2).retain(); ByteBuf c3 = buffer().writeByte(3).retain(2); CompositeByteBuf bufA = compositeBuffer(); bufA.addComponents(c1, c2, c3).writerIndex(3); CompositeByteBuf bufB = compositeBuffer(); bufB.addComponents(bufA); // Ensure that bufA.refCnt() did not change. assertThat(bufA.refCnt(), is(1)); // Ensure that c[123]'s refCnt did not change. assertThat(c1.refCnt(), is(1)); assertThat(c2.refCnt(), is(2)); assertThat(c3.refCnt(), is(3)); // This should decrease bufA.refCnt(). bufB.release(); assertThat(bufB.refCnt(), is(0)); // Ensure bufA.refCnt() changed. assertThat(bufA.refCnt(), is(0)); // Ensure that c[123]'s refCnt also changed due to the deallocation of bufA. assertThat(c1.refCnt(), is(0)); assertThat(c2.refCnt(), is(1)); assertThat(c3.refCnt(), is(2)); c3.release(2); c2.release(); }
@Test public void testRangedConsolidation() { CompositeByteBuf buf = freeLater(compositeBuffer(Integer.MAX_VALUE)); buf.addComponent(wrappedBuffer(new byte[] {1})); buf.addComponent(wrappedBuffer(new byte[] {2, 3})); buf.addComponent(wrappedBuffer(new byte[] {4, 5, 6})); buf.addComponent(wrappedBuffer(new byte[] {7, 8, 9, 10})); buf.consolidate(1, 2); assertEquals(3, buf.numComponents()); assertEquals(wrappedBuffer(new byte[] {1}), buf.component(0)); assertEquals(wrappedBuffer(new byte[] {2, 3, 4, 5, 6}), buf.component(1)); assertEquals(wrappedBuffer(new byte[] {7, 8, 9, 10}), buf.component(2)); }
@Test public void testNestedLayout() { CompositeByteBuf buf = freeLater(compositeBuffer()); buf.addComponent( compositeBuffer() .addComponent(wrappedBuffer(new byte[] {1, 2})) .addComponent(wrappedBuffer(new byte[] {3, 4})) .slice(1, 2)); ByteBuffer[] nioBuffers = buf.nioBuffers(0, 2); assertThat(nioBuffers.length, is(2)); assertThat(nioBuffers[0].remaining(), is(1)); assertThat(nioBuffers[0].get(), is((byte) 2)); assertThat(nioBuffers[1].remaining(), is(1)); assertThat(nioBuffers[1].get(), is((byte) 3)); }
@Test public void testFullConsolidation() { CompositeByteBuf buf = freeLater(compositeBuffer(Integer.MAX_VALUE)); buf.addComponent(wrappedBuffer(new byte[] {1})); buf.addComponent(wrappedBuffer(new byte[] {2, 3})); buf.addComponent(wrappedBuffer(new byte[] {4, 5, 6})); buf.consolidate(); assertEquals(1, buf.numComponents()); assertTrue(buf.hasArray()); assertNotNull(buf.array()); assertEquals(0, buf.arrayOffset()); }
@Test public void testAutoConsolidation() { CompositeByteBuf buf = compositeBuffer(2); buf.addComponent(wrappedBuffer(new byte[] {1})); assertEquals(1, buf.numComponents()); buf.addComponent(wrappedBuffer(new byte[] {2, 3})); assertEquals(2, buf.numComponents()); buf.addComponent(wrappedBuffer(new byte[] {4, 5, 6})); assertEquals(1, buf.numComponents()); assertTrue(buf.hasArray()); assertNotNull(buf.array()); assertEquals(0, buf.arrayOffset()); }
/** Tests the "getBufferFor" method */ @Test public void testComponentAtOffset() { CompositeByteBuf buf = (CompositeByteBuf) wrappedBuffer(new byte[] {1, 2, 3, 4, 5}, new byte[] {4, 5, 6, 7, 8, 9, 26}); // Ensure that a random place will be fine assertEquals(5, buf.componentAtOffset(2).capacity()); // Loop through each byte byte index = 0; while (index < buf.capacity()) { ByteBuf _buf = buf.componentAtOffset(index++); assertNotNull(_buf); assertTrue(_buf.capacity() > 0); assertNotNull(_buf.getByte(0)); assertNotNull(_buf.getByte(_buf.readableBytes() - 1)); } }