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
public void testToString() {
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));
}

View File

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

View File

@ -19,12 +19,14 @@ package io.netty.buffer;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*;
public class ByteBufProcessorTest {
@Test
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();
assertEquals(3, buf.forEachByte(0, length, ByteBufProcessor.FIND_CRLF));
@ -42,7 +44,8 @@ public class ByteBufProcessorTest {
@Test
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();
assertEquals(27, buf.forEachByteDesc(0, length, ByteBufProcessor.FIND_LINEAR_WHITESPACE));

View File

@ -393,7 +393,7 @@ public class HttpPostRequestDecoder {
checkDestroyed();
// 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
ByteBuf buf = content.content();
if (undecodedChunk == null) {

View File

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

View File

@ -22,6 +22,8 @@ import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
public class HttpClientCodecTest {
@ -42,6 +44,21 @@ public class HttpClientCodecTest {
ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
ch.writeInbound(Unpooled.copiedBuffer(RESPONSE, CharsetUtil.ISO_8859_1));
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
@ -52,6 +69,20 @@ public class HttpClientCodecTest {
ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
ch.writeInbound(Unpooled.copiedBuffer(CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1));
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
@ -61,8 +92,7 @@ public class HttpClientCodecTest {
assertTrue(ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
"http://localhost/")));
assertNotNull(ch.readOutbound());
assertNotNull(releaseLater(ch.readOutbound()));
try {
ch.finish();
fail();
@ -76,8 +106,16 @@ public class HttpClientCodecTest {
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
EmbeddedChannel ch = new EmbeddedChannel(codec);
ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
ch.writeInbound(Unpooled.copiedBuffer(INCOMPLETE_CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1));
ch.writeOutbound(releaseLater(
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 {
ch.finish();

View File

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

View File

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

View File

@ -15,7 +15,10 @@
*/
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.UnpooledByteBufAllocator;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
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.HttpMethod;
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.util.CharsetUtil;
import org.junit.Test;
import java.util.Arrays;
import static io.netty.util.ReferenceCountUtil.*;
import static org.junit.Assert.*;
/** {@link HttpPostRequestDecoder} test case. */
@ -78,8 +79,8 @@ public class HttpPostRequestDecoderTest {
// Create decoder instance to test.
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
decoder.offer(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8)));
decoder.offer(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8))));
decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)));
// Validate it's enough chunks to decode upload.
assertTrue(decoder.hasNext());
@ -90,6 +91,8 @@ public class HttpPostRequestDecoderTest {
// Validate data has been parsed correctly as it was passed into request.
assertEquals("Invalid decoded data [data=" + data.replaceAll("\r", "\\\\r") + ", upload=" + upload + ']',
data, upload.getString(CharsetUtil.UTF_8));
upload.release();
decoder.destroy();
}
}
@ -122,6 +125,7 @@ public class HttpPostRequestDecoderTest {
// Create decoder instance to test.
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
assertFalse(decoder.getBodyHttpDatas().isEmpty());
decoder.destroy();
}
// See https://github.com/netty/netty/issues/1848
@ -166,19 +170,20 @@ public class HttpPostRequestDecoderTest {
aSmallBuf.writeBytes(aBytes, 0, split);
aLargeBuf.writeBytes(aBytes, split, aBytes.length - split);
aDecoder.offer(new DefaultHttpContent(aSmallBuf));
aDecoder.offer(new DefaultHttpContent(aLargeBuf));
aDecoder.offer(releaseLater(new DefaultHttpContent(aSmallBuf)));
aDecoder.offer(releaseLater(new DefaultHttpContent(aLargeBuf)));
aDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
assertTrue("Should have a piece of data", aDecoder.hasNext());
InterfaceHttpData aDecodedData = aDecoder.next();
assertEquals(InterfaceHttpData.HttpDataType.Attribute, aDecodedData.getHttpDataType());
Attribute aAttr = (Attribute) aDecodedData;
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.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert;
import org.junit.Test;
public class WebSocketFrameAggregatorTest {
private final ByteBuf content1 = Unpooled.copiedBuffer("Content1", CharsetUtil.UTF_8);
private final ByteBuf content2 = Unpooled.copiedBuffer("Content2", CharsetUtil.UTF_8);
private final ByteBuf content3 = Unpooled.copiedBuffer("Content3", CharsetUtil.UTF_8);
private final ByteBuf aggregatedContent = Unpooled.buffer().writeBytes(content1.duplicate())
.writeBytes(content2.duplicate()).writeBytes(content3.duplicate());
private final ByteBuf content1 = ReferenceCountUtil.releaseLater(
Unpooled.copiedBuffer("Content1", CharsetUtil.UTF_8));
private final ByteBuf content2 = ReferenceCountUtil.releaseLater(
Unpooled.copiedBuffer("Content2", CharsetUtil.UTF_8));
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
public void testAggregationBinary() {
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(Integer.MAX_VALUE));
@ -46,21 +51,25 @@ public class WebSocketFrameAggregatorTest {
Assert.assertTrue(frame.isFinalFragment());
Assert.assertEquals(1, frame.rsv());
Assert.assertEquals(content1, frame.content());
frame.release();
PingWebSocketFrame frame2 = (PingWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame2.isFinalFragment());
Assert.assertEquals(0, frame2.rsv());
Assert.assertEquals(content1, frame2.content());
frame2.release();
PongWebSocketFrame frame3 = (PongWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame3.isFinalFragment());
Assert.assertEquals(0, frame3.rsv());
Assert.assertEquals(content1, frame3.content());
frame3.release();
BinaryWebSocketFrame frame4 = (BinaryWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame4.isFinalFragment());
Assert.assertEquals(0, frame4.rsv());
Assert.assertEquals(aggregatedContent, frame4.content());
frame4.release();
Assert.assertNull(channel.readInbound());
}
@ -80,28 +89,32 @@ public class WebSocketFrameAggregatorTest {
TextWebSocketFrame frame = (TextWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame.isFinalFragment());
Assert.assertEquals(1, frame.rsv());
Assert.assertEquals(content1.duplicate(), frame.content());
Assert.assertEquals(content1, frame.content());
frame.release();
PingWebSocketFrame frame2 = (PingWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame2.isFinalFragment());
Assert.assertEquals(0, frame2.rsv());
Assert.assertEquals(content1, frame2.content());
frame2.release();
PongWebSocketFrame frame3 = (PongWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame3.isFinalFragment());
Assert.assertEquals(0, frame3.rsv());
Assert.assertEquals(content1, frame3.content());
frame3.release();
TextWebSocketFrame frame4 = (TextWebSocketFrame) channel.readInbound();
Assert.assertTrue(frame4.isFinalFragment());
Assert.assertEquals(0, frame4.rsv());
Assert.assertEquals(aggregatedContent, frame4.content());
frame4.release();
Assert.assertNull(channel.readInbound());
}
@Test
public void textFrameTooBig() {
public void textFrameTooBig() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(8));
channel.writeInbound(new BinaryWebSocketFrame(true, 1, 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(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.LastHttpContent;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert;
import org.junit.Test;
@ -41,8 +42,8 @@ public class WebSocketServerHandshaker00Test {
EmbeddedChannel ch = new EmbeddedChannel(
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
FullHttpRequest req = new DefaultFullHttpRequest(
HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII));
FullHttpRequest req = ReferenceCountUtil.releaseLater(new DefaultFullHttpRequest(
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.UPGRADE, WEBSOCKET.toLowerCase());
@ -64,5 +65,6 @@ public class WebSocketServerHandshaker00Test {
LastHttpContent content = (LastHttpContent) ch2.readInbound();
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.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert;
import org.junit.Test;
@ -39,7 +40,8 @@ public class WebSocketServerHandshaker08Test {
EmbeddedChannel ch = new EmbeddedChannel(
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.UPGRADE, WEBSOCKET.toLowerCase());
req.headers().set(Names.CONNECTION, "Upgrade");
@ -60,5 +62,6 @@ public class WebSocketServerHandshaker08Test {
Assert.assertEquals(
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
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.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.ReferenceCountUtil;
import org.junit.Assert;
import org.junit.Test;
@ -39,7 +40,8 @@ public class WebSocketServerHandshaker13Test {
EmbeddedChannel ch = new EmbeddedChannel(
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.UPGRADE, WEBSOCKET.toLowerCase());
req.headers().set(Names.CONNECTION, "Upgrade");
@ -60,5 +62,6 @@ public class WebSocketServerHandshaker13Test {
Assert.assertEquals(
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
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.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.ReferenceCountUtil;
import org.junit.Before;
import org.junit.Test;
@ -53,7 +54,7 @@ public class WebSocketServerProtocolHandlerTest {
EmbeddedChannel ch = createChannel(new MockOutboundHandler());
ChannelHandlerContext handshakerCtx = ch.pipeline().context(WebSocketServerProtocolHandshakeHandler.class);
writeUpgradeRequest(ch);
assertEquals(SWITCHING_PROTOCOLS, responses.remove().getStatus());
assertEquals(SWITCHING_PROTOCOLS, ReferenceCountUtil.releaseLater(responses.remove()).getStatus());
assertNotNull(WebSocketServerProtocolHandler.getHandshaker(handshakerCtx));
}
@ -62,10 +63,10 @@ public class WebSocketServerProtocolHandlerTest {
EmbeddedChannel ch = createChannel();
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"));
assertEquals(FORBIDDEN, responses.remove().getStatus());
assertEquals(FORBIDDEN, ReferenceCountUtil.releaseLater(responses.remove()).getStatus());
}
@Test
@ -81,7 +82,7 @@ public class WebSocketServerProtocolHandlerTest {
ch.writeInbound(httpRequestWithEntity);
FullHttpResponse response = responses.remove();
FullHttpResponse response = ReferenceCountUtil.releaseLater(responses.remove());
assertEquals(BAD_REQUEST, response.getStatus());
assertEquals("not a WebSocket handshake request: missing upgrade", getResponseMessage(response));
}
@ -100,7 +101,7 @@ public class WebSocketServerProtocolHandlerTest {
ch.writeInbound(httpRequest);
FullHttpResponse response = responses.remove();
FullHttpResponse response = ReferenceCountUtil.releaseLater(responses.remove());
assertEquals(BAD_REQUEST, response.getStatus());
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 {
assertNull(content);
content = "processed: " + ((TextWebSocketFrame) msg).text();
ReferenceCountUtil.release(msg);
}
String getContent() {

View File

@ -44,6 +44,8 @@ public class ByteToMessageDecoderTest {
channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound();
Assert.assertEquals(b, buf.skipBytes(1));
b.release();
buf.release();
}
@Test
@ -67,5 +69,7 @@ public class ByteToMessageDecoderTest {
channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound();
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.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;
import java.nio.charset.Charset;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*;
public class DelimiterBasedFrameDecoderTest {
@ -32,9 +34,10 @@ public class DelimiterBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, true,
Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("TestLine", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound());
ch.finish();
}
@Test
@ -44,9 +47,10 @@ public class DelimiterBasedFrameDecoderTest {
ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset()));
assertNull(ch.readInbound());
ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("TestLine", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound());
ch.finish();
}
@Test
@ -54,9 +58,10 @@ public class DelimiterBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, false,
Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("TestLine\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound());
ch.finish();
}
@Test
@ -66,9 +71,10 @@ public class DelimiterBasedFrameDecoderTest {
ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset()));
assertNull(ch.readInbound());
ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g\r\n", ((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("TestLine\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound());
ch.finish();
}
@Test
@ -77,8 +83,11 @@ public class DelimiterBasedFrameDecoderTest {
new DelimiterBasedFrameDecoder(8192, true, Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("first", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
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.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
public class LengthFieldBasedFrameDecoderTest {
@Test
@ -48,11 +50,12 @@ public class LengthFieldBasedFrameDecoderTest {
b.release();
Assert.assertNull(channel.readInbound());
channel.finish();
}
@Test
public void testDiscardTooLongFrame2() {
ByteBuf buf = Unpooled.buffer();
ByteBuf buf = releaseLater(Unpooled.buffer());
buf.writeInt(32);
for (int i = 0; i < 32; i++) {
buf.writeByte(i);
@ -77,5 +80,6 @@ public class LengthFieldBasedFrameDecoderTest {
b.release();
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.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;
import static io.netty.buffer.Unpooled.*;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@ -30,9 +32,12 @@ public class LineBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(8192, true, false));
ch.writeInbound(copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("first", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertNull(ch.readInbound());
ch.finish();
ReferenceCountUtil.release(ch.readInbound());
}
@Test
@ -40,9 +45,11 @@ public class LineBasedFrameDecoderTest {
EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(8192, false, false));
ch.writeInbound(copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first\r\n", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second\n", ((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("first\r\n", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second\n", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertNull(ch.readInbound());
ch.finish();
ReferenceCountUtil.release(ch.readInbound());
}
@Test
@ -56,7 +63,8 @@ public class LineBasedFrameDecoderTest {
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));
}
@ -72,7 +80,8 @@ public class LineBasedFrameDecoderTest {
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));
}
@ -89,7 +98,8 @@ public class LineBasedFrameDecoderTest {
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((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));
}
}

View File

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

View File

@ -24,6 +24,7 @@ import org.junit.Test;
import java.util.List;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*;
public class ReplayingDecoderTest {
@ -73,7 +74,8 @@ public class ReplayingDecoderTest {
// "C\n" should be appended to "AB" so that LineDecoder decodes it correctly.
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();
assertNull(ch.readInbound());
@ -95,12 +97,12 @@ public class ReplayingDecoderTest {
// "C\n" should be appended to "AB" so that LineDecoder decodes it correctly.
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());
ch.read();
ch.finish();
assertEquals(Unpooled.wrappedBuffer(new byte[] {'B' }), ch.readInbound());
assertEquals(releaseLater(Unpooled.wrappedBuffer(new byte[] {'B' })), releaseLater(ch.readInbound()));
assertNull(ch.readInbound());
}
@ -122,6 +124,8 @@ public class ReplayingDecoderTest {
channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound();
assertEquals(b, buf.skipBytes(1));
b.release();
buf.release();
}
@Test
@ -145,6 +149,8 @@ public class ReplayingDecoderTest {
ByteBuf b = (ByteBuf) channel.readInbound();
assertEquals("Expect to have still all bytes in the buffer", b, buf);
b.release();
buf.release();
}
@Test
@ -168,5 +174,7 @@ public class ReplayingDecoderTest {
channel.writeInbound(buf.copy());
ByteBuf b = (ByteBuf) channel.readInbound();
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.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*;
public class SnappyFramedDecoderTest {
@ -108,7 +109,7 @@ public class SnappyFramedDecoderTest {
channel.writeInbound(in);
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' });
assertEquals(expected, channel.readInbound());
assertEquals(releaseLater(expected), releaseLater(channel.readInbound()));
}
@Test
@ -124,7 +125,7 @@ public class SnappyFramedDecoderTest {
channel.writeInbound(in);
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

View File

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

View File

@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;
import java.util.Random;
@ -138,10 +139,27 @@ public class SnappyIntegrationTest {
}
assertEquals(in, decompressed);
decompressed.release();
in.release();
} finally {
// Avoids memory leak through AbstractChannel.allChannels
encoder.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.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ThreadLocalRandom;
import org.junit.Test;
@ -93,6 +94,13 @@ public abstract class ZlibTest {
// Closing an encoder channel will generate a footer.
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.
assertFalse(chDecoderZlib.finish());

View File

@ -25,6 +25,7 @@ import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*;
public class DelimiterBasedFrameDecoderTest {
@ -44,7 +45,7 @@ public class DelimiterBasedFrameDecoderTest {
}
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));
}
}
@ -63,7 +64,7 @@ public class DelimiterBasedFrameDecoderTest {
}
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));
}
}

View File

@ -24,6 +24,7 @@ import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.*;
public class LengthFieldBasedFrameDecoderTest {
@ -42,7 +43,7 @@ public class LengthFieldBasedFrameDecoderTest {
}
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));
buf.release();
}
@ -62,7 +63,7 @@ public class LengthFieldBasedFrameDecoderTest {
}
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));
buf.release();
}

View File

@ -21,6 +21,7 @@ import org.junit.Before;
import org.junit.Test;
import static io.netty.buffer.Unpooled.*;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*;
@ -43,8 +44,8 @@ public class ProtobufVarint32FrameDecoderTest {
assertThat(ch.readInbound(), is(nullValue()));
ch.writeInbound(wrappedBuffer(b, 3, b.length - 3));
assertThat(
(ByteBuf) ch.readInbound(),
is(wrappedBuffer(new byte[] { 1, 1, 1, 1 })));
releaseLater((ByteBuf) ch.readInbound()),
is(releaseLater(wrappedBuffer(new byte[] { 1, 1, 1, 1 }))));
}
@Test
@ -60,6 +61,6 @@ public class ProtobufVarint32FrameDecoderTest {
ch.writeInbound(wrappedBuffer(b, 127, 600));
assertThat(ch.readInbound(), is(nullValue()));
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 static io.netty.buffer.Unpooled.*;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
@ -37,7 +38,7 @@ public class ProtobufVarint32LengthFieldPrependerTest {
public void testTinyEncode() {
byte[] b = { 4, 1, 1, 1, 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
@ -49,6 +50,6 @@ public class ProtobufVarint32LengthFieldPrependerTest {
b[0] = -2;
b[1] = 15;
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);
Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data);
Assert.assertNull(channel.readOutbound());
buf.release();
}
}

View File

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

View File

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

View File

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