Fix all leaks reported during tests

- One notable leak is from WebSocketFrameAggregator
- All other leaks are from tests
This commit is contained in:
Norman Maurer 2013-12-06 12:40:11 +01:00 committed by Trustin Lee
parent 51428004b3
commit b3d8c81557
32 changed files with 294 additions and 132 deletions

View File

@ -1532,7 +1532,7 @@ public abstract class AbstractByteBufTest {
@Test @Test
public void testToString() { public void testToString() {
buffer.clear(); buffer.clear();
buffer.writeBytes(copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1)); buffer.writeBytes(releaseLater(copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1)));
assertEquals("Hello, World!", buffer.toString(CharsetUtil.ISO_8859_1)); assertEquals("Hello, World!", buffer.toString(CharsetUtil.ISO_8859_1));
} }

View File

@ -101,8 +101,8 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
*/ */
@Test @Test
public void testComponentAtOffset() { public void testComponentAtOffset() {
CompositeByteBuf buf = (CompositeByteBuf) wrappedBuffer(new byte[]{1, 2, 3, 4, 5}, CompositeByteBuf buf = releaseLater((CompositeByteBuf) wrappedBuffer(new byte[]{1, 2, 3, 4, 5},
new byte[]{4, 5, 6, 7, 8, 9, 26}); new byte[]{4, 5, 6, 7, 8, 9, 26}));
//Ensure that a random place will be fine //Ensure that a random place will be fine
assertEquals(5, buf.componentAtOffset(2).capacity()); assertEquals(5, buf.componentAtOffset(2).capacity());
@ -161,7 +161,7 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
@Test @Test
public void testAutoConsolidation() { public void testAutoConsolidation() {
CompositeByteBuf buf = compositeBuffer(2); CompositeByteBuf buf = releaseLater(compositeBuffer(2));
buf.addComponent(wrappedBuffer(new byte[] { 1 })); buf.addComponent(wrappedBuffer(new byte[] { 1 }));
assertEquals(1, buf.numComponents()); assertEquals(1, buf.numComponents());
@ -179,7 +179,7 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
@Test @Test
public void testCompositeToSingleBuffer() { public void testCompositeToSingleBuffer() {
CompositeByteBuf buf = compositeBuffer(3); CompositeByteBuf buf = releaseLater(compositeBuffer(3));
buf.addComponent(wrappedBuffer(new byte[] {1, 2, 3})); buf.addComponent(wrappedBuffer(new byte[] {1, 2, 3}));
assertEquals(1, buf.numComponents()); assertEquals(1, buf.numComponents());
@ -229,13 +229,13 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
@Test @Test
public void testCompositeWrappedBuffer() { public void testCompositeWrappedBuffer() {
ByteBuf header = buffer(12).order(order); ByteBuf header = releaseLater(buffer(12)).order(order);
ByteBuf payload = buffer(512).order(order); ByteBuf payload = releaseLater(buffer(512)).order(order);
header.writeBytes(new byte[12]); header.writeBytes(new byte[12]);
payload.writeBytes(new byte[512]); payload.writeBytes(new byte[512]);
ByteBuf buffer = wrappedBuffer(header, payload); ByteBuf buffer = releaseLater(wrappedBuffer(header, payload));
assertEquals(12, header.readableBytes()); assertEquals(12, header.readableBytes());
assertEquals(512, payload.readableBytes()); assertEquals(512, payload.readableBytes());
@ -362,77 +362,81 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
//XXX Same tests than testEquals with written AggregateChannelBuffers //XXX Same tests than testEquals with written AggregateChannelBuffers
ByteBuf a, b; ByteBuf a, b;
// Different length. // Different length.
a = wrappedBuffer(new byte[] { 1 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1 })).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }, new byte[1]).order(order)); b = releaseLater(wrappedBuffer(wrappedBuffer(new byte[] { 1 }, new byte[1])).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes(wrappedBuffer(new byte[] { 2 }).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 2 })).order(order));
assertFalse(ByteBufUtil.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, short length. // Same content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3 })).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }, new byte[2]).order(order)); b = releaseLater(wrappedBuffer(releaseLater(wrappedBuffer(new byte[] { 1 }, new byte[2]))).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 2); b.writerIndex(b.writerIndex() - 2);
b.writeBytes(wrappedBuffer(new byte[] { 2 }).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 2 })).order(order));
b.writeBytes(wrappedBuffer(new byte[] { 3 }).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 3 })).order(order));
assertTrue(ByteBufUtil.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, short length. // Same content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3 })).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3).order(order)); b = releaseLater(wrappedBuffer(releaseLater(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3))).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 3, 1).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 3, 1)).order(order));
assertTrue(ByteBufUtil.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, short length. // Different content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3 })).order(order);
b = releaseLater(wrappedBuffer(wrappedBuffer(new byte[] { 1, 2 }, new byte[1]).order(order))); b = releaseLater(wrappedBuffer(releaseLater(wrappedBuffer(new byte[] { 1, 2 }, new byte[1])).order(order)));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes(wrappedBuffer(new byte[] { 4 }).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 4 })).order(order));
assertFalse(ByteBufUtil.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, short length. // Different content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3 })).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3).order(order)); b = releaseLater(wrappedBuffer(releaseLater(wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3))).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes(wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 3, 1).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 3, 1)).order(order));
assertFalse(ByteBufUtil.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, long length. // Same content, same firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })).order(order);
b = releaseLater(wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }, new byte[7])).order(order)); b = releaseLater(wrappedBuffer(releaseLater(wrappedBuffer(new byte[] { 1, 2, 3 }, new byte[7]))).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 7); b.writerIndex(b.writerIndex() - 7);
b.writeBytes(wrappedBuffer(new byte[] { 4, 5, 6 }).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 4, 5, 6 })).order(order));
b.writeBytes(wrappedBuffer(new byte[] { 7, 8, 9, 10 }).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 7, 8, 9, 10 })).order(order));
assertTrue(ByteBufUtil.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, long length. // Same content, different firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10).order(order)); b = releaseLater(wrappedBuffer(releaseLater(
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10))).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 5); b.writerIndex(b.writerIndex() - 5);
b.writeBytes(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5).order(order)); b.writeBytes(releaseLater(
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5)).order(order));
assertTrue(ByteBufUtil.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, long length. // Different content, same firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })).order(order);
b = releaseLater(wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3, 4, 6 }, new byte[5])).order(order)); b = releaseLater(wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3, 4, 6 }, new byte[5])).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 5); b.writerIndex(b.writerIndex() - 5);
b.writeBytes(wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 }).order(order)); b.writeBytes(releaseLater(wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 })).order(order));
assertFalse(ByteBufUtil.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, long length. // Different content, different firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = releaseLater(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10).order(order)); b = releaseLater(wrappedBuffer(releaseLater(
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10))).order(order));
// to enable writeBytes // to enable writeBytes
b.writerIndex(b.writerIndex() - 5); b.writerIndex(b.writerIndex() - 5);
b.writeBytes(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5).order(order)); b.writeBytes(releaseLater(
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5)).order(order));
assertFalse(ByteBufUtil.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
} }

View File

@ -19,12 +19,14 @@ package io.netty.buffer;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class ByteBufProcessorTest { public class ByteBufProcessorTest {
@Test @Test
public void testForward() { public void testForward() {
final ByteBuf buf = Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1); final ByteBuf buf = releaseLater(
Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1));
final int length = buf.readableBytes(); final int length = buf.readableBytes();
assertEquals(3, buf.forEachByte(0, length, ByteBufProcessor.FIND_CRLF)); assertEquals(3, buf.forEachByte(0, length, ByteBufProcessor.FIND_CRLF));
@ -42,7 +44,8 @@ public class ByteBufProcessorTest {
@Test @Test
public void testBackward() { public void testBackward() {
final ByteBuf buf = Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1); final ByteBuf buf = releaseLater(
Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1));
final int length = buf.readableBytes(); final int length = buf.readableBytes();
assertEquals(27, buf.forEachByteDesc(0, length, ByteBufProcessor.FIND_LINEAR_WHITESPACE)); assertEquals(27, buf.forEachByteDesc(0, length, ByteBufProcessor.FIND_LINEAR_WHITESPACE));

View File

@ -393,7 +393,7 @@ public class HttpPostRequestDecoder {
checkDestroyed(); checkDestroyed();
// Maybe we should better not copy here for performance reasons but this will need // Maybe we should better not copy here for performance reasons but this will need
// more care by teh caller to release the content in a correct manner later // more care by the caller to release the content in a correct manner later
// So maybe something to optimize on a later stage // So maybe something to optimize on a later stage
ByteBuf buf = content.content(); ByteBuf buf = content.content();
if (undecodedChunk == null) { if (undecodedChunk == null) {

View File

@ -63,6 +63,7 @@ public class WebSocketFrameAggregator extends MessageToMessageDecoder<WebSocketF
} else if (msg instanceof BinaryWebSocketFrame) { } else if (msg instanceof BinaryWebSocketFrame) {
currentFrame = new BinaryWebSocketFrame(true, msg.rsv(), buf); currentFrame = new BinaryWebSocketFrame(true, msg.rsv(), buf);
} else { } else {
buf.release();
throw new IllegalStateException( throw new IllegalStateException(
"WebSocket frame was not of type TextWebSocketFrame or BinaryWebSocketFrame"); "WebSocket frame was not of type TextWebSocketFrame or BinaryWebSocketFrame");
} }
@ -77,6 +78,8 @@ public class WebSocketFrameAggregator extends MessageToMessageDecoder<WebSocketF
} }
CompositeByteBuf content = (CompositeByteBuf) currentFrame.content(); CompositeByteBuf content = (CompositeByteBuf) currentFrame.content();
if (content.readableBytes() > maxFrameSize - msg.content().readableBytes()) { if (content.readableBytes() > maxFrameSize - msg.content().readableBytes()) {
// release the current frame
currentFrame.release();
tooLongFrameFound = true; tooLongFrameFound = true;
throw new TooLongFrameException( throw new TooLongFrameException(
"WebSocketFrame length exceeded " + content + "WebSocketFrame length exceeded " + content +
@ -102,7 +105,6 @@ public class WebSocketFrameAggregator extends MessageToMessageDecoder<WebSocketF
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx); super.channelInactive(ctx);
// release current frame if it is not null as it may be a left-over // release current frame if it is not null as it may be a left-over
if (currentFrame != null) { if (currentFrame != null) {
currentFrame.release(); currentFrame.release();
@ -113,7 +115,7 @@ public class WebSocketFrameAggregator extends MessageToMessageDecoder<WebSocketF
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
super.handlerRemoved(ctx); super.handlerRemoved(ctx);
// release current frane if it is not null as it may be a left-over as there is not much more we can do in // release current frame if it is not null as it may be a left-over as there is not much more we can do in
// this case // this case
if (currentFrame != null) { if (currentFrame != null) {
currentFrame.release(); currentFrame.release();

View File

@ -22,6 +22,8 @@ import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class HttpClientCodecTest { public class HttpClientCodecTest {
@ -42,6 +44,21 @@ public class HttpClientCodecTest {
ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/")); ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
ch.writeInbound(Unpooled.copiedBuffer(RESPONSE, CharsetUtil.ISO_8859_1)); ch.writeInbound(Unpooled.copiedBuffer(RESPONSE, CharsetUtil.ISO_8859_1));
ch.finish(); ch.finish();
for (;;) {
Object msg = ch.readOutbound();
if (msg == null) {
break;
}
release(msg);
}
for (;;) {
Object msg = ch.readInbound();
if (msg == null) {
break;
}
release(msg);
}
} }
@Test @Test
@ -52,6 +69,20 @@ public class HttpClientCodecTest {
ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/")); ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
ch.writeInbound(Unpooled.copiedBuffer(CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1)); ch.writeInbound(Unpooled.copiedBuffer(CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1));
ch.finish(); ch.finish();
for (;;) {
Object msg = ch.readOutbound();
if (msg == null) {
break;
}
release(msg);
}
for (;;) {
Object msg = ch.readInbound();
if (msg == null) {
break;
}
release(msg);
}
} }
@Test @Test
@ -61,8 +92,7 @@ public class HttpClientCodecTest {
assertTrue(ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, assertTrue(ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
"http://localhost/"))); "http://localhost/")));
assertNotNull(ch.readOutbound()); assertNotNull(releaseLater(ch.readOutbound()));
try { try {
ch.finish(); ch.finish();
fail(); fail();
@ -76,8 +106,16 @@ public class HttpClientCodecTest {
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true); HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
EmbeddedChannel ch = new EmbeddedChannel(codec); EmbeddedChannel ch = new EmbeddedChannel(codec);
ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/")); ch.writeOutbound(releaseLater(
ch.writeInbound(Unpooled.copiedBuffer(INCOMPLETE_CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1)); new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/")));
assertNotNull(releaseLater(ch.readOutbound()));
assertNull(ch.readInbound());
ch.writeInbound(releaseLater(
Unpooled.copiedBuffer(INCOMPLETE_CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1)));
assertThat(releaseLater(ch.readInbound()), instanceOf(HttpResponse.class));
assertThat(releaseLater(ch.readInbound()), instanceOf(HttpContent.class)); // Chunk 'first'
assertThat(releaseLater(ch.readInbound()), instanceOf(HttpContent.class)); // Chunk 'second'
assertNull(ch.readInbound());
try { try {
ch.finish(); ch.finish();

View File

@ -59,10 +59,15 @@ public class HttpContentEncoderTest {
HttpContent chunk; HttpContent chunk;
chunk = (HttpContent) ch.readOutbound(); chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("3")); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("3"));
chunk.release();
chunk = (HttpContent) ch.readOutbound(); chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("2")); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("2"));
chunk.release();
chunk = (HttpContent) ch.readOutbound(); chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1")); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1"));
chunk.release();
chunk = (HttpContent) ch.readOutbound(); chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().isReadable(), is(false)); assertThat(chunk.content().isReadable(), is(false));
@ -98,8 +103,9 @@ public class HttpContentEncoderTest {
chunk = (HttpContent) ch.readOutbound(); chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1")); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1"));
assertThat(chunk, is(instanceOf(HttpContent.class))); assertThat(chunk, is(instanceOf(HttpContent.class)));
chunk.release();
chunk = (HttpContent) ch.readOutbound(); chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().isReadable(), is(false)); assertThat(chunk.content().isReadable(), is(false));
assertThat(chunk, is(instanceOf(LastHttpContent.class))); assertThat(chunk, is(instanceOf(LastHttpContent.class)));

View File

@ -27,6 +27,7 @@ import org.junit.Test;
import java.util.List; import java.util.List;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class HttpObjectAggregatorTest { public class HttpObjectAggregatorTest {
@ -99,7 +100,6 @@ public class HttpObjectAggregatorTest {
assertEquals(aggratedMessage.headers().get("X-Test"), Boolean.TRUE.toString()); assertEquals(aggratedMessage.headers().get("X-Test"), Boolean.TRUE.toString());
assertEquals(aggratedMessage.headers().get("X-Trailer"), Boolean.TRUE.toString()); assertEquals(aggratedMessage.headers().get("X-Trailer"), Boolean.TRUE.toString());
checkContentBuffer(aggratedMessage); checkContentBuffer(aggratedMessage);
assertNull(embedder.readInbound()); assertNull(embedder.readInbound());
} }
@ -109,9 +109,9 @@ public class HttpObjectAggregatorTest {
EmbeddedChannel embedder = new EmbeddedChannel(aggr); EmbeddedChannel embedder = new EmbeddedChannel(aggr);
HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.GET, "http://localhost"); HttpMethod.GET, "http://localhost");
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpContent chunk1 = releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)));
HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); HttpContent chunk2 = releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)));
HttpContent chunk3 = new DefaultHttpContent(Unpooled.copiedBuffer("test3", CharsetUtil.US_ASCII)); HttpContent chunk3 = releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer("test3", CharsetUtil.US_ASCII)));
HttpContent chunk4 = LastHttpContent.EMPTY_LAST_CONTENT; HttpContent chunk4 = LastHttpContent.EMPTY_LAST_CONTENT;
assertFalse(embedder.writeInbound(message)); assertFalse(embedder.writeInbound(message));
@ -135,6 +135,7 @@ public class HttpObjectAggregatorTest {
} }
assertFalse(embedder.writeInbound(chunk3.copy())); assertFalse(embedder.writeInbound(chunk3.copy()));
assertFalse(embedder.writeInbound(chunk4.copy())); assertFalse(embedder.writeInbound(chunk4.copy()));
embedder.finish();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)

View File

@ -15,7 +15,10 @@
*/ */
package io.netty.handler.codec.http.multipart; package io.netty.handler.codec.http.multipart;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultHttpContent; import io.netty.handler.codec.http.DefaultHttpContent;
@ -23,15 +26,13 @@ import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.handler.codec.http.LastHttpContent; import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import static io.netty.util.ReferenceCountUtil.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** {@link HttpPostRequestDecoder} test case. */ /** {@link HttpPostRequestDecoder} test case. */
@ -78,8 +79,8 @@ public class HttpPostRequestDecoderTest {
// Create decoder instance to test. // Create decoder instance to test.
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req); final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
decoder.offer(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8))); decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8))));
decoder.offer(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)); decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)));
// Validate it's enough chunks to decode upload. // Validate it's enough chunks to decode upload.
assertTrue(decoder.hasNext()); assertTrue(decoder.hasNext());
@ -90,6 +91,8 @@ public class HttpPostRequestDecoderTest {
// Validate data has been parsed correctly as it was passed into request. // Validate data has been parsed correctly as it was passed into request.
assertEquals("Invalid decoded data [data=" + data.replaceAll("\r", "\\\\r") + ", upload=" + upload + ']', assertEquals("Invalid decoded data [data=" + data.replaceAll("\r", "\\\\r") + ", upload=" + upload + ']',
data, upload.getString(CharsetUtil.UTF_8)); data, upload.getString(CharsetUtil.UTF_8));
upload.release();
decoder.destroy();
} }
} }
@ -122,6 +125,7 @@ public class HttpPostRequestDecoderTest {
// Create decoder instance to test. // Create decoder instance to test.
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req); final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
assertFalse(decoder.getBodyHttpDatas().isEmpty()); assertFalse(decoder.getBodyHttpDatas().isEmpty());
decoder.destroy();
} }
// See https://github.com/netty/netty/issues/1848 // See https://github.com/netty/netty/issues/1848
@ -166,19 +170,20 @@ public class HttpPostRequestDecoderTest {
aSmallBuf.writeBytes(aBytes, 0, split); aSmallBuf.writeBytes(aBytes, 0, split);
aLargeBuf.writeBytes(aBytes, split, aBytes.length - split); aLargeBuf.writeBytes(aBytes, split, aBytes.length - split);
aDecoder.offer(new DefaultHttpContent(aSmallBuf)); aDecoder.offer(releaseLater(new DefaultHttpContent(aSmallBuf)));
aDecoder.offer(new DefaultHttpContent(aLargeBuf)); aDecoder.offer(releaseLater(new DefaultHttpContent(aLargeBuf)));
aDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT); aDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
assertTrue("Should have a piece of data", aDecoder.hasNext()); assertTrue("Should have a piece of data", aDecoder.hasNext());
InterfaceHttpData aDecodedData = aDecoder.next(); InterfaceHttpData aDecodedData = aDecoder.next();
assertEquals(InterfaceHttpData.HttpDataType.Attribute, aDecodedData.getHttpDataType()); assertEquals(InterfaceHttpData.HttpDataType.Attribute, aDecodedData.getHttpDataType());
Attribute aAttr = (Attribute) aDecodedData; Attribute aAttr = (Attribute) aDecodedData;
assertEquals(aData, aAttr.getValue()); assertEquals(aData, aAttr.getValue());
aDecodedData.release();
aDecoder.destroy();
} }
} }

View File

@ -20,16 +20,21 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class WebSocketFrameAggregatorTest { public class WebSocketFrameAggregatorTest {
private final ByteBuf content1 = Unpooled.copiedBuffer("Content1", CharsetUtil.UTF_8); private final ByteBuf content1 = ReferenceCountUtil.releaseLater(
private final ByteBuf content2 = Unpooled.copiedBuffer("Content2", CharsetUtil.UTF_8); Unpooled.copiedBuffer("Content1", CharsetUtil.UTF_8));
private final ByteBuf content3 = Unpooled.copiedBuffer("Content3", CharsetUtil.UTF_8); private final ByteBuf content2 = ReferenceCountUtil.releaseLater(
private final ByteBuf aggregatedContent = Unpooled.buffer().writeBytes(content1.duplicate()) Unpooled.copiedBuffer("Content2", CharsetUtil.UTF_8));
.writeBytes(content2.duplicate()).writeBytes(content3.duplicate()); private final ByteBuf content3 = ReferenceCountUtil.releaseLater(
Unpooled.copiedBuffer("Content3", CharsetUtil.UTF_8));
private final ByteBuf aggregatedContent = ReferenceCountUtil.releaseLater(
Unpooled.buffer().writeBytes(content1.duplicate())
.writeBytes(content2.duplicate()).writeBytes(content3.duplicate()));
@Test @Test
public void testAggregationBinary() { public void testAggregationBinary() {
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(Integer.MAX_VALUE)); EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(Integer.MAX_VALUE));
@ -46,21 +51,25 @@ public class WebSocketFrameAggregatorTest {
Assert.assertTrue(frame.isFinalFragment()); Assert.assertTrue(frame.isFinalFragment());
Assert.assertEquals(1, frame.rsv()); Assert.assertEquals(1, frame.rsv());
Assert.assertEquals(content1, frame.content()); Assert.assertEquals(content1, frame.content());
frame.release();
PingWebSocketFrame frame2 = (PingWebSocketFrame) channel.readInbound(); PingWebSocketFrame frame2 = (PingWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame2.isFinalFragment()); Assert.assertTrue(frame2.isFinalFragment());
Assert.assertEquals(0, frame2.rsv()); Assert.assertEquals(0, frame2.rsv());
Assert.assertEquals(content1, frame2.content()); Assert.assertEquals(content1, frame2.content());
frame2.release();
PongWebSocketFrame frame3 = (PongWebSocketFrame) channel.readInbound(); PongWebSocketFrame frame3 = (PongWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame3.isFinalFragment()); Assert.assertTrue(frame3.isFinalFragment());
Assert.assertEquals(0, frame3.rsv()); Assert.assertEquals(0, frame3.rsv());
Assert.assertEquals(content1, frame3.content()); Assert.assertEquals(content1, frame3.content());
frame3.release();
BinaryWebSocketFrame frame4 = (BinaryWebSocketFrame) channel.readInbound(); BinaryWebSocketFrame frame4 = (BinaryWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame4.isFinalFragment()); Assert.assertTrue(frame4.isFinalFragment());
Assert.assertEquals(0, frame4.rsv()); Assert.assertEquals(0, frame4.rsv());
Assert.assertEquals(aggregatedContent, frame4.content()); Assert.assertEquals(aggregatedContent, frame4.content());
frame4.release();
Assert.assertNull(channel.readInbound()); Assert.assertNull(channel.readInbound());
} }
@ -80,28 +89,32 @@ public class WebSocketFrameAggregatorTest {
TextWebSocketFrame frame = (TextWebSocketFrame) channel.readInbound(); TextWebSocketFrame frame = (TextWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame.isFinalFragment()); Assert.assertTrue(frame.isFinalFragment());
Assert.assertEquals(1, frame.rsv()); Assert.assertEquals(1, frame.rsv());
Assert.assertEquals(content1.duplicate(), frame.content()); Assert.assertEquals(content1, frame.content());
frame.release();
PingWebSocketFrame frame2 = (PingWebSocketFrame) channel.readInbound(); PingWebSocketFrame frame2 = (PingWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame2.isFinalFragment()); Assert.assertTrue(frame2.isFinalFragment());
Assert.assertEquals(0, frame2.rsv()); Assert.assertEquals(0, frame2.rsv());
Assert.assertEquals(content1, frame2.content()); Assert.assertEquals(content1, frame2.content());
frame2.release();
PongWebSocketFrame frame3 = (PongWebSocketFrame) channel.readInbound(); PongWebSocketFrame frame3 = (PongWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame3.isFinalFragment()); Assert.assertTrue(frame3.isFinalFragment());
Assert.assertEquals(0, frame3.rsv()); Assert.assertEquals(0, frame3.rsv());
Assert.assertEquals(content1, frame3.content()); Assert.assertEquals(content1, frame3.content());
frame3.release();
TextWebSocketFrame frame4 = (TextWebSocketFrame) channel.readInbound(); TextWebSocketFrame frame4 = (TextWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame4.isFinalFragment()); Assert.assertTrue(frame4.isFinalFragment());
Assert.assertEquals(0, frame4.rsv()); Assert.assertEquals(0, frame4.rsv());
Assert.assertEquals(aggregatedContent, frame4.content()); Assert.assertEquals(aggregatedContent, frame4.content());
frame4.release();
Assert.assertNull(channel.readInbound()); Assert.assertNull(channel.readInbound());
} }
@Test @Test
public void textFrameTooBig() { public void textFrameTooBig() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(8)); EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(8));
channel.writeInbound(new BinaryWebSocketFrame(true, 1, content1.copy())); channel.writeInbound(new BinaryWebSocketFrame(true, 1, content1.copy()));
channel.writeInbound(new BinaryWebSocketFrame(false, 0, content1.copy())); channel.writeInbound(new BinaryWebSocketFrame(false, 0, content1.copy()));
@ -124,5 +137,13 @@ public class WebSocketFrameAggregatorTest {
} }
channel.writeInbound(new ContinuationWebSocketFrame(false, 0, content2.copy())); channel.writeInbound(new ContinuationWebSocketFrame(false, 0, content2.copy()));
channel.writeInbound(new ContinuationWebSocketFrame(true, 0, content2.copy())); channel.writeInbound(new ContinuationWebSocketFrame(true, 0, content2.copy()));
for (;;) {
Object msg = channel.readInbound();
if (msg == null) {
break;
}
ReferenceCountUtil.release(msg);
}
channel.finish();
} }
} }

View File

@ -28,6 +28,7 @@ import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.LastHttpContent; import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -41,8 +42,8 @@ public class WebSocketServerHandshaker00Test {
EmbeddedChannel ch = new EmbeddedChannel( EmbeddedChannel ch = new EmbeddedChannel(
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder()); new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
FullHttpRequest req = new DefaultFullHttpRequest( FullHttpRequest req = ReferenceCountUtil.releaseLater(new DefaultFullHttpRequest(
HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII)); HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII)));
req.headers().set(Names.HOST, "server.example.com"); req.headers().set(Names.HOST, "server.example.com");
req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase()); req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase());
@ -64,5 +65,6 @@ public class WebSocketServerHandshaker00Test {
LastHttpContent content = (LastHttpContent) ch2.readInbound(); LastHttpContent content = (LastHttpContent) ch2.readInbound();
Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII)); Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
content.release();
} }
} }

View File

@ -26,6 +26,7 @@ import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -39,7 +40,8 @@ public class WebSocketServerHandshaker08Test {
EmbeddedChannel ch = new EmbeddedChannel( EmbeddedChannel ch = new EmbeddedChannel(
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder()); new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat"); FullHttpRequest req = ReferenceCountUtil.releaseLater(
new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat"));
req.headers().set(Names.HOST, "server.example.com"); req.headers().set(Names.HOST, "server.example.com");
req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase()); req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase());
req.headers().set(Names.CONNECTION, "Upgrade"); req.headers().set(Names.CONNECTION, "Upgrade");
@ -60,5 +62,6 @@ public class WebSocketServerHandshaker08Test {
Assert.assertEquals( Assert.assertEquals(
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT)); "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
ReferenceCountUtil.release(res);
} }
} }

View File

@ -26,6 +26,7 @@ import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -39,7 +40,8 @@ public class WebSocketServerHandshaker13Test {
EmbeddedChannel ch = new EmbeddedChannel( EmbeddedChannel ch = new EmbeddedChannel(
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder()); new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat"); FullHttpRequest req = ReferenceCountUtil.releaseLater(
new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat"));
req.headers().set(Names.HOST, "server.example.com"); req.headers().set(Names.HOST, "server.example.com");
req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase()); req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase());
req.headers().set(Names.CONNECTION, "Upgrade"); req.headers().set(Names.CONNECTION, "Upgrade");
@ -60,5 +62,6 @@ public class WebSocketServerHandshaker13Test {
Assert.assertEquals( Assert.assertEquals(
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT)); "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
ReferenceCountUtil.release(res);
} }
} }

View File

@ -28,6 +28,7 @@ import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.ReferenceCountUtil;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -53,7 +54,7 @@ public class WebSocketServerProtocolHandlerTest {
EmbeddedChannel ch = createChannel(new MockOutboundHandler()); EmbeddedChannel ch = createChannel(new MockOutboundHandler());
ChannelHandlerContext handshakerCtx = ch.pipeline().context(WebSocketServerProtocolHandshakeHandler.class); ChannelHandlerContext handshakerCtx = ch.pipeline().context(WebSocketServerProtocolHandshakeHandler.class);
writeUpgradeRequest(ch); writeUpgradeRequest(ch);
assertEquals(SWITCHING_PROTOCOLS, responses.remove().getStatus()); assertEquals(SWITCHING_PROTOCOLS, ReferenceCountUtil.releaseLater(responses.remove()).getStatus());
assertNotNull(WebSocketServerProtocolHandler.getHandshaker(handshakerCtx)); assertNotNull(WebSocketServerProtocolHandler.getHandshaker(handshakerCtx));
} }
@ -62,10 +63,10 @@ public class WebSocketServerProtocolHandlerTest {
EmbeddedChannel ch = createChannel(); EmbeddedChannel ch = createChannel();
writeUpgradeRequest(ch); writeUpgradeRequest(ch);
assertEquals(SWITCHING_PROTOCOLS, responses.remove().getStatus()); assertEquals(SWITCHING_PROTOCOLS, ReferenceCountUtil.releaseLater(responses.remove()).getStatus());
ch.writeInbound(new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/test")); ch.writeInbound(new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/test"));
assertEquals(FORBIDDEN, responses.remove().getStatus()); assertEquals(FORBIDDEN, ReferenceCountUtil.releaseLater(responses.remove()).getStatus());
} }
@Test @Test
@ -81,7 +82,7 @@ public class WebSocketServerProtocolHandlerTest {
ch.writeInbound(httpRequestWithEntity); ch.writeInbound(httpRequestWithEntity);
FullHttpResponse response = responses.remove(); FullHttpResponse response = ReferenceCountUtil.releaseLater(responses.remove());
assertEquals(BAD_REQUEST, response.getStatus()); assertEquals(BAD_REQUEST, response.getStatus());
assertEquals("not a WebSocket handshake request: missing upgrade", getResponseMessage(response)); assertEquals("not a WebSocket handshake request: missing upgrade", getResponseMessage(response));
} }
@ -100,7 +101,7 @@ public class WebSocketServerProtocolHandlerTest {
ch.writeInbound(httpRequest); ch.writeInbound(httpRequest);
FullHttpResponse response = responses.remove(); FullHttpResponse response = ReferenceCountUtil.releaseLater(responses.remove());
assertEquals(BAD_REQUEST, response.getStatus()); assertEquals(BAD_REQUEST, response.getStatus());
assertEquals("not a WebSocket request: missing key", getResponseMessage(response)); assertEquals("not a WebSocket request: missing key", getResponseMessage(response));
} }
@ -163,6 +164,7 @@ public class WebSocketServerProtocolHandlerTest {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
assertNull(content); assertNull(content);
content = "processed: " + ((TextWebSocketFrame) msg).text(); content = "processed: " + ((TextWebSocketFrame) msg).text();
ReferenceCountUtil.release(msg);
} }
String getContent() { String getContent() {

View File

@ -44,6 +44,8 @@ public class ByteToMessageDecoderTest {
channel.writeInbound(buf.copy()); channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound(); ByteBuf b = (ByteBuf) channel.readInbound();
Assert.assertEquals(b, buf.skipBytes(1)); Assert.assertEquals(b, buf.skipBytes(1));
b.release();
buf.release();
} }
@Test @Test
@ -67,5 +69,7 @@ public class ByteToMessageDecoderTest {
channel.writeInbound(buf.copy()); channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound(); ByteBuf b = (ByteBuf) channel.readInbound();
Assert.assertEquals(b, Unpooled.wrappedBuffer(new byte[] { 'b', 'c'})); Assert.assertEquals(b, Unpooled.wrappedBuffer(new byte[] { 'b', 'c'}));
buf.release();
b.release();
} }
} }

View File

@ -19,10 +19,12 @@ 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.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.Test;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class DelimiterBasedFrameDecoderTest { public class DelimiterBasedFrameDecoderTest {
@ -32,9 +34,10 @@ public class DelimiterBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, true, EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, true,
Delimiters.lineDelimiter())); Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset())); ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("TestLine", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("g", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.finish();
} }
@Test @Test
@ -44,9 +47,10 @@ public class DelimiterBasedFrameDecoderTest {
ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset())); ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset()));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset())); ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("TestLine", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("g", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.finish();
} }
@Test @Test
@ -54,9 +58,10 @@ public class DelimiterBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, false, EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, false,
Delimiters.lineDelimiter())); Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset())); ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("TestLine\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("g\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.finish();
} }
@Test @Test
@ -66,9 +71,10 @@ public class DelimiterBasedFrameDecoderTest {
ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset())); ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset()));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset())); ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("TestLine\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("g\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.finish();
} }
@Test @Test
@ -77,8 +83,11 @@ public class DelimiterBasedFrameDecoderTest {
new DelimiterBasedFrameDecoder(8192, true, Delimiters.lineDelimiter())); new DelimiterBasedFrameDecoder(8192, true, Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII)); ch.writeInbound(Unpooled.copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII)); assertEquals("first", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII)); assertEquals("second", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.finish();
ReferenceCountUtil.release(ch.readInbound());
} }
} }

View File

@ -21,6 +21,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
public class LengthFieldBasedFrameDecoderTest { public class LengthFieldBasedFrameDecoderTest {
@Test @Test
@ -48,11 +50,12 @@ public class LengthFieldBasedFrameDecoderTest {
b.release(); b.release();
Assert.assertNull(channel.readInbound()); Assert.assertNull(channel.readInbound());
channel.finish();
} }
@Test @Test
public void testDiscardTooLongFrame2() { public void testDiscardTooLongFrame2() {
ByteBuf buf = Unpooled.buffer(); ByteBuf buf = releaseLater(Unpooled.buffer());
buf.writeInt(32); buf.writeInt(32);
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
buf.writeByte(i); buf.writeByte(i);
@ -77,5 +80,6 @@ public class LengthFieldBasedFrameDecoderTest {
b.release(); b.release();
Assert.assertNull(channel.readInbound()); Assert.assertNull(channel.readInbound());
channel.finish();
} }
} }

View File

@ -18,9 +18,11 @@ package io.netty.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.Test;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -30,9 +32,12 @@ public class LineBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(8192, true, false)); EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(8192, true, false));
ch.writeInbound(copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII)); ch.writeInbound(copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII)); assertEquals("first", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII)); assertEquals("second", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.finish();
ReferenceCountUtil.release(ch.readInbound());
} }
@Test @Test
@ -40,9 +45,11 @@ public class LineBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(8192, false, false)); EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(8192, false, false));
ch.writeInbound(copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII)); ch.writeInbound(copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first\r\n", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII)); assertEquals("first\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second\n", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII)); assertEquals("second\n", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
ch.finish();
ReferenceCountUtil.release(ch.readInbound());
} }
@Test @Test
@ -56,7 +63,8 @@ public class LineBasedFrameDecoderTest {
assertThat(e, is(instanceOf(TooLongFrameException.class))); assertThat(e, is(instanceOf(TooLongFrameException.class)));
} }
assertThat((ByteBuf) ch.readInbound(), is(copiedBuffer("first\n", CharsetUtil.US_ASCII))); assertThat(releaseLater((ByteBuf) ch.readInbound()),
is(releaseLater(copiedBuffer("first\n", CharsetUtil.US_ASCII))));
assertThat(ch.finish(), is(false)); assertThat(ch.finish(), is(false));
} }
@ -72,7 +80,8 @@ public class LineBasedFrameDecoderTest {
assertThat(e, is(instanceOf(TooLongFrameException.class))); assertThat(e, is(instanceOf(TooLongFrameException.class)));
} }
assertThat((ByteBuf) ch.readInbound(), is(copiedBuffer("first\r\n", CharsetUtil.US_ASCII))); assertThat(releaseLater((ByteBuf) ch.readInbound()),
is(releaseLater(copiedBuffer("first\r\n", CharsetUtil.US_ASCII))));
assertThat(ch.finish(), is(false)); assertThat(ch.finish(), is(false));
} }
@ -89,7 +98,8 @@ public class LineBasedFrameDecoderTest {
assertThat(ch.writeInbound(copiedBuffer("890", CharsetUtil.US_ASCII)), is(false)); assertThat(ch.writeInbound(copiedBuffer("890", CharsetUtil.US_ASCII)), is(false));
assertThat(ch.writeInbound(copiedBuffer("123\r\nfirst\r\n", CharsetUtil.US_ASCII)), is(true)); assertThat(ch.writeInbound(copiedBuffer("123\r\nfirst\r\n", CharsetUtil.US_ASCII)), is(true));
assertThat((ByteBuf) ch.readInbound(), is(copiedBuffer("first\r\n", CharsetUtil.US_ASCII))); assertThat(releaseLater((ByteBuf) ch.readInbound()),
is(releaseLater(copiedBuffer("first\r\n", CharsetUtil.US_ASCII))));
assertThat(ch.finish(), is(false)); assertThat(ch.finish(), is(false));
} }
} }

View File

@ -21,6 +21,7 @@ import io.netty.util.CharsetUtil;
import io.netty.util.Signal; import io.netty.util.Signal;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class ReplayingDecoderBufferTest { public class ReplayingDecoderBufferTest {
@ -30,8 +31,8 @@ public class ReplayingDecoderBufferTest {
*/ */
@Test @Test
public void testGetUnsignedByte() { public void testGetUnsignedByte() {
ReplayingDecoderBuffer buffer = new ReplayingDecoderBuffer(Unpooled.copiedBuffer("TestBuffer", ReplayingDecoderBuffer buffer = new ReplayingDecoderBuffer(releaseLater(Unpooled.copiedBuffer("TestBuffer",
CharsetUtil.ISO_8859_1)); CharsetUtil.ISO_8859_1)));
boolean error; boolean error;
int i = 0; int i = 0;
@ -53,8 +54,8 @@ public class ReplayingDecoderBufferTest {
*/ */
@Test @Test
public void testGetByte() { public void testGetByte() {
ReplayingDecoderBuffer buffer = new ReplayingDecoderBuffer(Unpooled.copiedBuffer("TestBuffer", ReplayingDecoderBuffer buffer = new ReplayingDecoderBuffer(releaseLater(Unpooled.copiedBuffer("TestBuffer",
CharsetUtil.ISO_8859_1)); CharsetUtil.ISO_8859_1)));
boolean error; boolean error;
int i = 0; int i = 0;
@ -76,7 +77,7 @@ public class ReplayingDecoderBufferTest {
*/ */
@Test @Test
public void testGetBoolean() { public void testGetBoolean() {
ByteBuf buf = Unpooled.buffer(10); ByteBuf buf = releaseLater(Unpooled.buffer(10));
while (buf.isWritable()) { while (buf.isWritable()) {
buf.writeBoolean(true); buf.writeBoolean(true);
} }

View File

@ -24,6 +24,7 @@ import org.junit.Test;
import java.util.List; import java.util.List;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class ReplayingDecoderTest { public class ReplayingDecoderTest {
@ -73,7 +74,8 @@ public class ReplayingDecoderTest {
// "C\n" should be appended to "AB" so that LineDecoder decodes it correctly. // "C\n" should be appended to "AB" so that LineDecoder decodes it correctly.
ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{'C', '\n'})); ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{'C', '\n'}));
assertEquals(Unpooled.wrappedBuffer(new byte[] { 'A', 'B', 'C' }), ch.readInbound()); assertEquals(releaseLater(Unpooled.wrappedBuffer(new byte[] { 'A', 'B', 'C' })),
releaseLater(ch.readInbound()));
ch.finish(); ch.finish();
assertNull(ch.readInbound()); assertNull(ch.readInbound());
@ -95,12 +97,12 @@ public class ReplayingDecoderTest {
// "C\n" should be appended to "AB" so that LineDecoder decodes it correctly. // "C\n" should be appended to "AB" so that LineDecoder decodes it correctly.
ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{'C', '\n' , 'B', '\n'})); ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{'C', '\n' , 'B', '\n'}));
assertEquals(Unpooled.wrappedBuffer(new byte[] {'C' }), ch.readInbound()); assertEquals(releaseLater(Unpooled.wrappedBuffer(new byte[] {'C' })), releaseLater(ch.readInbound()));
assertNull("Must be null as it must only decode one frame", ch.readInbound()); assertNull("Must be null as it must only decode one frame", ch.readInbound());
ch.read(); ch.read();
ch.finish(); ch.finish();
assertEquals(Unpooled.wrappedBuffer(new byte[] {'B' }), ch.readInbound()); assertEquals(releaseLater(Unpooled.wrappedBuffer(new byte[] {'B' })), releaseLater(ch.readInbound()));
assertNull(ch.readInbound()); assertNull(ch.readInbound());
} }
@ -122,6 +124,8 @@ public class ReplayingDecoderTest {
channel.writeInbound(buf.copy()); channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound(); ByteBuf b = (ByteBuf) channel.readInbound();
assertEquals(b, buf.skipBytes(1)); assertEquals(b, buf.skipBytes(1));
b.release();
buf.release();
} }
@Test @Test
@ -145,6 +149,8 @@ public class ReplayingDecoderTest {
ByteBuf b = (ByteBuf) channel.readInbound(); ByteBuf b = (ByteBuf) channel.readInbound();
assertEquals("Expect to have still all bytes in the buffer", b, buf); assertEquals("Expect to have still all bytes in the buffer", b, buf);
b.release();
buf.release();
} }
@Test @Test
@ -168,5 +174,7 @@ public class ReplayingDecoderTest {
channel.writeInbound(buf.copy()); channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound(); ByteBuf b = (ByteBuf) channel.readInbound();
assertEquals(b, Unpooled.wrappedBuffer(new byte[] { 'b', 'c'})); assertEquals(b, Unpooled.wrappedBuffer(new byte[] { 'b', 'c'}));
b.release();
buf.release();
} }
} }

View File

@ -21,6 +21,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class SnappyFramedDecoderTest { public class SnappyFramedDecoderTest {
@ -108,7 +109,7 @@ public class SnappyFramedDecoderTest {
channel.writeInbound(in); channel.writeInbound(in);
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' }); ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' });
assertEquals(expected, channel.readInbound()); assertEquals(releaseLater(expected), releaseLater(channel.readInbound()));
} }
@Test @Test
@ -124,7 +125,7 @@ public class SnappyFramedDecoderTest {
channel.writeInbound(in); channel.writeInbound(in);
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' }); ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' });
assertEquals(expected, channel.readInbound()); assertEquals(releaseLater(expected), releaseLater(channel.readInbound()));
} }
// The following two tests differ in only the checksum provided for the literal // The following two tests differ in only the checksum provided for the literal

View File

@ -22,6 +22,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class SnappyFramedEncoderTest { public class SnappyFramedEncoderTest {
@ -46,7 +47,7 @@ public class SnappyFramedEncoderTest {
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y' 0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y'
}); });
assertEquals(expected, channel.readOutbound()); assertEquals(releaseLater(expected), releaseLater(channel.readOutbound()));
} }
@Test @Test
@ -67,7 +68,7 @@ public class SnappyFramedEncoderTest {
0x3a, 0x05, 0x00 0x3a, 0x05, 0x00
}); });
assertEquals(expected, channel.readOutbound()); assertEquals(releaseLater(expected), releaseLater(channel.readOutbound()));
} }
@Test @Test
@ -96,7 +97,7 @@ public class SnappyFramedEncoderTest {
actual.addComponent(m); actual.addComponent(m);
actual.writerIndex(actual.writerIndex() + m.readableBytes()); actual.writerIndex(actual.writerIndex() + m.readableBytes());
} }
assertEquals(expected, actual); assertEquals(releaseLater(expected), releaseLater(actual));
in.release(); in.release();
} }

View File

@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.CompositeByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.Test;
import java.util.Random; import java.util.Random;
@ -138,10 +139,27 @@ public class SnappyIntegrationTest {
} }
assertEquals(in, decompressed); assertEquals(in, decompressed);
decompressed.release(); decompressed.release();
in.release();
} finally { } finally {
// Avoids memory leak through AbstractChannel.allChannels // Avoids memory leak through AbstractChannel.allChannels
encoder.close(); encoder.close();
decoder.close(); decoder.close();
for (;;) {
Object msg = encoder.readOutbound();
if (msg == null) {
break;
}
ReferenceCountUtil.release(msg);
}
for (;;) {
Object msg = decoder.readInbound();
if (msg == null) {
break;
}
ReferenceCountUtil.release(msg);
}
} }
} }

View File

@ -19,6 +19,7 @@ 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.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ThreadLocalRandom; import io.netty.util.internal.ThreadLocalRandom;
import org.junit.Test; import org.junit.Test;
@ -93,6 +94,13 @@ public abstract class ZlibTest {
// Closing an encoder channel will generate a footer. // Closing an encoder channel will generate a footer.
assertTrue(chEncoder.finish()); assertTrue(chEncoder.finish());
for (;;) {
Object msg = chEncoder.readOutbound();
if (msg == null) {
break;
}
ReferenceCountUtil.release(msg);
}
// But, the footer will be decoded into nothing. It's only for validation. // But, the footer will be decoded into nothing. It's only for validation.
assertFalse(chDecoderZlib.finish()); assertFalse(chDecoderZlib.finish());

View File

@ -25,6 +25,7 @@ import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class DelimiterBasedFrameDecoderTest { public class DelimiterBasedFrameDecoderTest {
@ -44,7 +45,7 @@ public class DelimiterBasedFrameDecoderTest {
} }
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A', 0 })); ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A', 0 }));
ByteBuf buf = (ByteBuf) ch.readInbound(); ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
} }
} }
@ -63,7 +64,7 @@ public class DelimiterBasedFrameDecoderTest {
} }
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 'A', 0 })); ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 'A', 0 }));
ByteBuf buf = (ByteBuf) ch.readInbound(); ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
} }
} }

View File

@ -24,6 +24,7 @@ import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class LengthFieldBasedFrameDecoderTest { public class LengthFieldBasedFrameDecoderTest {
@ -42,7 +43,7 @@ public class LengthFieldBasedFrameDecoderTest {
} }
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 1, 'A' })); ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 1, 'A' }));
ByteBuf buf = (ByteBuf) ch.readInbound(); ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
buf.release(); buf.release();
} }
@ -62,7 +63,7 @@ public class LengthFieldBasedFrameDecoderTest {
} }
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 0, 0, 1, 'A' })); ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 0, 0, 1, 'A' }));
ByteBuf buf = (ByteBuf) ch.readInbound(); ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
buf.release(); buf.release();
} }

View File

@ -21,6 +21,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.hamcrest.core.Is.*; import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*; import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -43,8 +44,8 @@ public class ProtobufVarint32FrameDecoderTest {
assertThat(ch.readInbound(), is(nullValue())); assertThat(ch.readInbound(), is(nullValue()));
ch.writeInbound(wrappedBuffer(b, 3, b.length - 3)); ch.writeInbound(wrappedBuffer(b, 3, b.length - 3));
assertThat( assertThat(
(ByteBuf) ch.readInbound(), releaseLater((ByteBuf) ch.readInbound()),
is(wrappedBuffer(new byte[] { 1, 1, 1, 1 }))); is(releaseLater(wrappedBuffer(new byte[] { 1, 1, 1, 1 }))));
} }
@Test @Test
@ -60,6 +61,6 @@ public class ProtobufVarint32FrameDecoderTest {
ch.writeInbound(wrappedBuffer(b, 127, 600)); ch.writeInbound(wrappedBuffer(b, 127, 600));
assertThat(ch.readInbound(), is(nullValue())); assertThat(ch.readInbound(), is(nullValue()));
ch.writeInbound(wrappedBuffer(b, 727, b.length - 727)); ch.writeInbound(wrappedBuffer(b, 727, b.length - 727));
assertThat((ByteBuf) ch.readInbound(), is(wrappedBuffer(b, 2, b.length - 2))); assertThat(releaseLater((ByteBuf) ch.readInbound()), is(releaseLater(wrappedBuffer(b, 2, b.length - 2))));
} }
} }

View File

@ -21,6 +21,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static io.netty.buffer.Unpooled.*; import static io.netty.buffer.Unpooled.*;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.hamcrest.core.Is.*; import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -37,7 +38,7 @@ public class ProtobufVarint32LengthFieldPrependerTest {
public void testTinyEncode() { public void testTinyEncode() {
byte[] b = { 4, 1, 1, 1, 1 }; byte[] b = { 4, 1, 1, 1, 1 };
ch.writeOutbound(wrappedBuffer(b, 1, b.length - 1)); ch.writeOutbound(wrappedBuffer(b, 1, b.length - 1));
assertThat((ByteBuf) ch.readOutbound(), is(wrappedBuffer(b))); assertThat(releaseLater((ByteBuf) ch.readOutbound()), is(releaseLater(wrappedBuffer(b))));
} }
@Test @Test
@ -49,6 +50,6 @@ public class ProtobufVarint32LengthFieldPrependerTest {
b[0] = -2; b[0] = -2;
b[1] = 15; b[1] = 15;
ch.writeOutbound(wrappedBuffer(b, 2, b.length - 2)); ch.writeOutbound(wrappedBuffer(b, 2, b.length - 2));
assertThat((ByteBuf) ch.readOutbound(), is(wrappedBuffer(b))); assertThat(releaseLater((ByteBuf) ch.readOutbound()), is(releaseLater(wrappedBuffer(b))));
} }
} }

View File

@ -34,5 +34,6 @@ public class StringEncoderTest {
buf.readBytes(data); buf.readBytes(data);
Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data); Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data);
Assert.assertNull(channel.readOutbound()); Assert.assertNull(channel.readOutbound());
buf.release();
} }
} }

View File

@ -30,6 +30,7 @@ import java.io.IOException;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class ChunkedWriteHandlerTest { public class ChunkedWriteHandlerTest {
@ -102,7 +103,7 @@ public class ChunkedWriteHandlerTest {
ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() { ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() {
private boolean done; private boolean done;
private final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1); private final ByteBuf buffer = releaseLater(Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1));
@Override @Override
public boolean isEndOfInput() throws Exception { public boolean isEndOfInput() throws Exception {
@ -141,7 +142,7 @@ public class ChunkedWriteHandlerTest {
// the listener should have been notified // the listener should have been notified
assertTrue(listenerNotified.get()); assertTrue(listenerNotified.get());
assertEquals(buffer, ch.readOutbound()); assertEquals(releaseLater(buffer), releaseLater(ch.readOutbound()));
assertNull(ch.readOutbound()); assertNull(ch.readOutbound());
} }
@ -203,6 +204,7 @@ public class ChunkedWriteHandlerTest {
i = 0; i = 0;
} }
} }
buffer.release();
} }
assertEquals(BYTES.length * inputs.length, read); assertEquals(BYTES.length * inputs.length, read);

View File

@ -64,6 +64,9 @@ public class SocketBufReleaseTest extends AbstractSocketTest {
serverHandler.check(); serverHandler.check();
clientHandler.check(); clientHandler.check();
serverHandler.release();
clientHandler.release();
} }
private static class BufWriterHandler extends SimpleChannelInboundHandler<Object> { private static class BufWriterHandler extends SimpleChannelInboundHandler<Object> {
@ -104,5 +107,9 @@ public class SocketBufReleaseTest extends AbstractSocketTest {
latch.await(); latch.await();
assertEquals(1, buf.refCnt()); assertEquals(1, buf.refCnt());
} }
void release() {
buf.release();
}
} }
} }

View File

@ -39,11 +39,7 @@ public class ChannelOutboundBufferTest {
assertNull(b); assertNull(b);
} }
assertEquals(0, buffer.nioBufferCount()); assertEquals(0, buffer.nioBufferCount());
for (;;) { release(buffer);
if (!buffer.remove()) {
break;
}
}
} }
@Test @Test
@ -78,11 +74,7 @@ public class ChannelOutboundBufferTest {
assertNull(buffers[i]); assertNull(buffers[i]);
} }
} }
for (;;) { release(buffer);
if (!buffer.remove()) {
break;
}
}
} }
@Test @Test
@ -107,11 +99,8 @@ public class ChannelOutboundBufferTest {
for (int i = 0; i < buffers.length; i++) { for (int i = 0; i < buffers.length; i++) {
assertEquals(buffers[i], buf.internalNioBuffer(0, buf.readableBytes())); assertEquals(buffers[i], buf.internalNioBuffer(0, buf.readableBytes()));
} }
for (;;) { release(buffer);
if (!buffer.remove()) { buf.release();
break;
}
}
} }
@Test @Test
@ -143,6 +132,11 @@ public class ChannelOutboundBufferTest {
assertNull(buffers[i]); assertNull(buffers[i]);
} }
} }
release(buffer);
buf.release();
}
private static void release(ChannelOutboundBuffer buffer) {
for (;;) { for (;;) {
if (!buffer.remove()) { if (!buffer.remove()) {
break; break;