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 547012bcff
commit f608a139cf

View File

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