Make CoalescingBufferQueueTest properly release all buffers including the composites removed from the queue.

Motivation:
Test was leaving composite buffers taken from the queue unreleased.

Modifications:
Make the test release buffers.

Result:
Nagging about leaked buffers should stop.
This commit is contained in:
Louis Ryan 2015-07-15 15:04:22 -07:00 committed by Norman Maurer
parent 80c59864cf
commit e6ed3ce6b1

View File

@ -17,6 +17,7 @@ package io.netty.channel;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.ImmediateEventExecutor;
import org.junit.After;
@ -24,8 +25,6 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import java.nio.charset.Charset;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -38,7 +37,6 @@ public class CoalescingBufferQueueTest {
private ByteBuf cat;
private ByteBuf mouse;
private ByteBuf empty;
private ChannelPromise catPromise, emptyPromise;
private ChannelPromise voidPromise;
@ -65,16 +63,14 @@ public class CoalescingBufferQueueTest {
emptyPromise = newPromise();
voidPromise = channel.voidPromise();
cat = Unpooled.wrappedBuffer("cat".getBytes(Charset.defaultCharset()));
mouse = Unpooled.wrappedBuffer("mouse".getBytes(Charset.defaultCharset()));
empty = Unpooled.buffer(0, 1);
cat = Unpooled.wrappedBuffer("cat".getBytes(CharsetUtil.US_ASCII));
mouse = Unpooled.wrappedBuffer("mouse".getBytes(CharsetUtil.US_ASCII));
}
@After
public void tearDown() {
ReferenceCountUtil.safeRelease(cat);
ReferenceCountUtil.safeRelease(mouse);
ReferenceCountUtil.safeRelease(empty);
}
@Test
@ -84,7 +80,7 @@ public class CoalescingBufferQueueTest {
writeQueue.add(mouse, mouseListener);
assertQueueSize(8, false);
DefaultChannelPromise aggregatePromise = newPromise();
assertEquals("catmouse", writeQueue.remove(8, aggregatePromise).toString(Charset.defaultCharset()));
assertEquals("catmouse", dequeue(8, aggregatePromise));
assertQueueSize(0, true);
assertFalse(catPromise.isSuccess());
assertFalse(mouseDone);
@ -97,11 +93,10 @@ public class CoalescingBufferQueueTest {
public void testWithVoidPromise() {
writeQueue.add(cat, voidPromise);
writeQueue.add(mouse, voidPromise);
writeQueue.add(empty, voidPromise);
assertQueueSize(8, false);
assertEquals("catm", writeQueue.remove(4, newPromise()).toString(Charset.defaultCharset()));
assertEquals("catm", dequeue(4, newPromise()));
assertQueueSize(4, false);
assertEquals("ouse", writeQueue.remove(4, newPromise()).toString(Charset.defaultCharset()));
assertEquals("ouse", dequeue(4, newPromise()));
assertQueueSize(0, true);
}
@ -110,7 +105,7 @@ public class CoalescingBufferQueueTest {
writeQueue.add(cat, catPromise);
writeQueue.add(mouse, mouseListener);
DefaultChannelPromise aggregatePromise = newPromise();
assertEquals("catm", writeQueue.remove(4, aggregatePromise).toString(Charset.defaultCharset()));
assertEquals("catm", dequeue(4, aggregatePromise));
assertQueueSize(4, false);
assertFalse(catPromise.isSuccess());
assertFalse(mouseDone);
@ -119,7 +114,7 @@ public class CoalescingBufferQueueTest {
assertFalse(mouseDone);
aggregatePromise = newPromise();
assertEquals("ouse", writeQueue.remove(Integer.MAX_VALUE, aggregatePromise).toString(Charset.defaultCharset()));
assertEquals("ouse", dequeue(Integer.MAX_VALUE, aggregatePromise));
assertQueueSize(0, true);
assertFalse(mouseDone);
aggregatePromise.trySuccess();
@ -147,7 +142,7 @@ public class CoalescingBufferQueueTest {
public void testReadEmptyQueueReturnsEmptyBuffer() {
assertQueueSize(0, true);
DefaultChannelPromise aggregatePromise = newPromise();
assertEquals(0, writeQueue.remove(Integer.MAX_VALUE, aggregatePromise).readableBytes());
assertEquals("", dequeue(Integer.MAX_VALUE, aggregatePromise));
assertQueueSize(0, true);
}
@ -162,26 +157,25 @@ public class CoalescingBufferQueueTest {
assertEquals(0, cat.refCnt());
assertEquals(0, mouse.refCnt());
assertSame(cause, catPromise.cause());
assertEquals(0, writeQueue.remove(Integer.MAX_VALUE, aggregatePromise).readableBytes());
assertEquals("", dequeue(Integer.MAX_VALUE, aggregatePromise));
assertQueueSize(0, true);
}
@Test
public void testEmptyBuffersAreCoalesced() {
ByteBuf empty = Unpooled.buffer(0, 1);
assertQueueSize(0, true);
writeQueue.add(cat, catPromise);
writeQueue.add(empty, emptyPromise);
assertQueueSize(3, false);
DefaultChannelPromise aggregatePromise = newPromise();
ByteBuf removed = writeQueue.remove(3, aggregatePromise);
assertEquals("cat", dequeue(3, aggregatePromise));
assertQueueSize(0, true);
assertEquals("cat", removed.toString(Charset.defaultCharset()));
assertFalse(catPromise.isSuccess());
assertFalse(emptyPromise.isSuccess());
aggregatePromise.trySuccess();
assertTrue(catPromise.isSuccess());
assertTrue(emptyPromise.isSuccess());
removed.release();
assertEquals(0, cat.refCnt());
assertEquals(0, empty.refCnt());
}
@ -194,7 +188,7 @@ public class CoalescingBufferQueueTest {
otherQueue.copyTo(writeQueue);
assertQueueSize(8, false);
DefaultChannelPromise aggregatePromise = newPromise();
assertEquals("catmouse", writeQueue.remove(8, aggregatePromise).toString(Charset.defaultCharset()));
assertEquals("catmouse", dequeue(8, aggregatePromise));
assertQueueSize(0, true);
assertFalse(catPromise.isSuccess());
assertFalse(mouseDone);
@ -215,4 +209,11 @@ public class CoalescingBufferQueueTest {
assertFalse(writeQueue.isEmpty());
}
}
private String dequeue(int numBytes, ChannelPromise aggregatePromise) {
ByteBuf removed = writeQueue.remove(numBytes, aggregatePromise);
String result = removed.toString(CharsetUtil.US_ASCII);
ReferenceCountUtil.safeRelease(removed);
return result;
}
}