Fixed various buffer leaks in FixedCompositeByteBufTest
This commit is contained in:
parent
fab56bae94
commit
86c4166c24
@ -16,7 +16,6 @@
|
|||||||
package io.netty.buffer;
|
package io.netty.buffer;
|
||||||
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@ -24,87 +23,63 @@ import java.io.IOException;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ReadOnlyBufferException;
|
import java.nio.ReadOnlyBufferException;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.util.ArrayDeque;
|
|
||||||
import java.util.Queue;
|
|
||||||
|
|
||||||
import static io.netty.buffer.Unpooled.buffer;
|
import static io.netty.buffer.Unpooled.*;
|
||||||
import static io.netty.buffer.Unpooled.compositeBuffer;
|
import static io.netty.util.ReferenceCountUtil.*;
|
||||||
import static io.netty.buffer.Unpooled.directBuffer;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
|
||||||
|
|
||||||
public class FixedCompositeByteBufTest {
|
public class FixedCompositeByteBufTest {
|
||||||
|
|
||||||
private static final Queue<ByteBuf> freeLaterQueue = new ArrayDeque<ByteBuf>();
|
private static ByteBuf newBuffer(ByteBuf... buffers) {
|
||||||
|
return releaseLater(new FixedCompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, buffers));
|
||||||
protected static <T extends ByteBuf> T freeLater(T buf) {
|
|
||||||
freeLaterQueue.add(buf);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void dispose() {
|
|
||||||
for (;;) {
|
|
||||||
ByteBuf buf = freeLaterQueue.poll();
|
|
||||||
if (buf == null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buf.refCnt() > 0) {
|
|
||||||
buf.release(buf.refCnt());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ByteBuf newBuffer(ByteBuf... buffers) {
|
|
||||||
return new FixedCompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, buffers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBoolean() {
|
public void testSetBoolean() {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setBoolean(0, true);
|
buf.setBoolean(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetByte() {
|
public void testSetByte() {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setByte(0, 1);
|
buf.setByte(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBytesWithByteBuf() {
|
public void testSetBytesWithByteBuf() {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setBytes(0, Unpooled.wrappedBuffer(new byte[4]));
|
buf.setBytes(0, wrappedBuffer(new byte[4]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBytesWithByteBuffer() {
|
public void testSetBytesWithByteBuffer() {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setBytes(0, ByteBuffer.wrap(new byte[4]));
|
buf.setBytes(0, ByteBuffer.wrap(new byte[4]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBytesWithInputStream() throws IOException {
|
public void testSetBytesWithInputStream() throws IOException {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setBytes(0, new ByteArrayInputStream(new byte[4]), 4);
|
buf.setBytes(0, new ByteArrayInputStream(new byte[4]), 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBytesWithChannel() throws IOException {
|
public void testSetBytesWithChannel() throws IOException {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setBytes(0, new ScatteringByteChannel() {
|
buf.setBytes(0, new ScatteringByteChannel() {
|
||||||
@Override
|
@Override
|
||||||
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
|
public long read(ByteBuffer[] dsts, int offset, int length) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long read(ByteBuffer[] dsts) throws IOException {
|
public long read(ByteBuffer[] dsts) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(ByteBuffer dst) throws IOException {
|
public int read(ByteBuffer dst) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,44 +89,44 @@ public class FixedCompositeByteBufTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() {
|
||||||
}
|
}
|
||||||
}, 4);
|
}, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetChar() throws IOException {
|
public void testSetChar() throws IOException {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setChar(0, 'b');
|
buf.setChar(0, 'b');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetDouble() throws IOException {
|
public void testSetDouble() throws IOException {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setDouble(0, 1);
|
buf.setDouble(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetFloat() throws IOException {
|
public void testSetFloat() throws IOException {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setFloat(0, 1);
|
buf.setFloat(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetInt() throws IOException {
|
public void testSetInt() throws IOException {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setInt(0, 1);
|
buf.setInt(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetLong() {
|
public void testSetLong() {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setLong(0, 1);
|
buf.setLong(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetMedium() throws IOException {
|
public void testSetMedium() throws IOException {
|
||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
|
||||||
buf.setMedium(0, 1);
|
buf.setMedium(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,18 +164,19 @@ public class FixedCompositeByteBufTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void testGatheringWrites(ByteBuf buf1, ByteBuf buf2) throws Exception {
|
private static void testGatheringWrites(ByteBuf buf1, ByteBuf buf2) throws Exception {
|
||||||
CompositeByteBuf buf = freeLater(compositeBuffer());
|
CompositeByteBuf buf = compositeBuffer();
|
||||||
buf.addComponent(buf1.writeBytes(new byte[]{1, 2}));
|
buf.addComponent(buf1.writeBytes(new byte[]{1, 2}));
|
||||||
buf.addComponent(buf2.writeBytes(new byte[]{1, 2}));
|
buf.addComponent(buf2.writeBytes(new byte[]{1, 2}));
|
||||||
buf.writerIndex(3);
|
buf.writerIndex(3);
|
||||||
buf.readerIndex(1);
|
buf.readerIndex(1);
|
||||||
|
|
||||||
AbstractByteBufTest.TestGatheringByteChannel channel = new AbstractByteBufTest.TestGatheringByteChannel();
|
AbstractByteBufTest.TestGatheringByteChannel channel = new AbstractByteBufTest.TestGatheringByteChannel();
|
||||||
|
|
||||||
buf.readBytes(channel, 2);
|
buf.readBytes(channel, 2);
|
||||||
|
|
||||||
byte[] data = new byte[2];
|
byte[] data = new byte[2];
|
||||||
buf.getBytes(1, data);
|
buf.getBytes(1, data);
|
||||||
|
buf.release();
|
||||||
|
|
||||||
assertArrayEquals(data, channel.writtenBytes());
|
assertArrayEquals(data, channel.writtenBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,7 +213,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
PooledByteBufAllocator.DEFAULT.directBuffer());
|
PooledByteBufAllocator.DEFAULT.directBuffer());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testGatheringWritesPartial(ByteBuf buf1, ByteBuf buf2) throws Exception {
|
private static void testGatheringWritesPartial(ByteBuf buf1, ByteBuf buf2) throws Exception {
|
||||||
buf1.writeBytes(new byte[]{1, 2, 3, 4});
|
buf1.writeBytes(new byte[]{1, 2, 3, 4});
|
||||||
buf2.writeBytes(new byte[]{1, 2, 3, 4});
|
buf2.writeBytes(new byte[]{1, 2, 3, 4});
|
||||||
ByteBuf buf = newBuffer(buf1, buf2);
|
ByteBuf buf = newBuffer(buf1, buf2);
|
||||||
@ -250,6 +226,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
byte[] data = new byte[8];
|
byte[] data = new byte[8];
|
||||||
buf.getBytes(0, data);
|
buf.getBytes(0, data);
|
||||||
assertArrayEquals(data, channel.writtenBytes());
|
assertArrayEquals(data, channel.writtenBytes());
|
||||||
|
buf.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -262,7 +239,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
testGatheringWritesSingleBuf(directBuffer());
|
testGatheringWritesSingleBuf(directBuffer());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testGatheringWritesSingleBuf(ByteBuf buf1) throws Exception {
|
private static void testGatheringWritesSingleBuf(ByteBuf buf1) throws Exception {
|
||||||
ByteBuf buf = newBuffer(buf1.writeBytes(new byte[]{1, 2, 3, 4}));
|
ByteBuf buf = newBuffer(buf1.writeBytes(new byte[]{1, 2, 3, 4}));
|
||||||
buf.readerIndex(1);
|
buf.readerIndex(1);
|
||||||
|
|
||||||
@ -272,6 +249,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
byte[] data = new byte[2];
|
byte[] data = new byte[2];
|
||||||
buf.getBytes(1, data);
|
buf.getBytes(1, data);
|
||||||
assertArrayEquals(data, channel.writtenBytes());
|
assertArrayEquals(data, channel.writtenBytes());
|
||||||
}
|
|
||||||
|
|
||||||
|
buf.release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user