Skip to content

Commit

Permalink
Fix buffer leaks in tests
Browse files Browse the repository at this point in the history
Motivation:

While working on netty#6087 some buffer leaks showed up.

Modifications:

Correctly release buffers.

Result:

No more buffer leaks in memcache and stomp codec tests.
  • Loading branch information
normanmaurer committed Dec 3, 2016
1 parent efeb4cd commit 39aefdf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void setup() throws Exception {

@After
public void teardown() throws Exception {
channel.finish();
channel.finishAndReleaseAll();
}

/**
Expand Down Expand Up @@ -150,6 +150,7 @@ public void shouldHandleNonUniformNetworkBatches() {
while (incoming.isReadable()) {
channel.writeInbound(incoming.readBytes(5));
}
incoming.release();

BinaryMemcacheRequest request = channel.readInbound();

Expand Down Expand Up @@ -261,8 +262,13 @@ public void shouldRetainCurrentMessageWhenSendingItOut() {
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);

assertTrue(channel.writeOutbound(request));
assertTrue(channel.writeInbound(channel.outboundMessages().toArray()));

for (;;) {
ByteBuf buffer = channel.readOutbound();
if (buffer == null) {
break;
}
channel.writeInbound(buffer);
}
BinaryMemcacheRequest read = channel.readInbound();
read.release();
// tearDown will call "channel.finish()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setup() throws Exception {

@After
public void teardown() throws Exception {
channel.finish();
channel.finishAndReleaseAll();
}

@Test
Expand Down Expand Up @@ -92,8 +92,8 @@ public void shouldEncodeExtras() {

ByteBuf written = channel.readOutbound();
assertThat(written.readableBytes(), is(DEFAULT_HEADER_SIZE + extrasLength));
written.readBytes(DEFAULT_HEADER_SIZE);
assertThat(written.readBytes(extrasLength).toString(CharsetUtil.UTF_8), equalTo(extrasContent));
written.skipBytes(DEFAULT_HEADER_SIZE);
assertThat(written.readSlice(extrasLength).toString(CharsetUtil.UTF_8), equalTo(extrasContent));
written.release();
}

Expand All @@ -109,8 +109,8 @@ public void shouldEncodeKey() {

ByteBuf written = channel.readOutbound();
assertThat(written.readableBytes(), is(DEFAULT_HEADER_SIZE + keyLength));
written.readBytes(DEFAULT_HEADER_SIZE);
assertThat(written.readBytes(keyLength).toString(CharsetUtil.UTF_8), equalTo("netty"));
written.skipBytes(DEFAULT_HEADER_SIZE);
assertThat(written.readSlice(keyLength).toString(CharsetUtil.UTF_8), equalTo("netty"));
written.release();
}

Expand Down Expand Up @@ -139,15 +139,15 @@ public void shouldEncodeContent() {
written = channel.readOutbound();
assertThat(written.readableBytes(), is(content1.content().readableBytes()));
assertThat(
written.readBytes(content1.content().readableBytes()).toString(CharsetUtil.UTF_8),
written.readSlice(content1.content().readableBytes()).toString(CharsetUtil.UTF_8),
is("Netty")
);
written.release();

written = channel.readOutbound();
assertThat(written.readableBytes(), is(content2.content().readableBytes()));
assertThat(
written.readBytes(content2.content().readableBytes()).toString(CharsetUtil.UTF_8),
written.readSlice(content2.content().readableBytes()).toString(CharsetUtil.UTF_8),
is(" Rocks!")
);
written.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void shouldAggregateChunksOnDecode() {

assertThat(channel.readInbound(), nullValue());

channel.finish();
assertFalse(channel.finish());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ public void testFrameAndContentEncoding() {
ByteBuf byteBuf = channel.readOutbound();
assertNotNull(byteBuf);
aggregatedBuffer.writeBytes(byteBuf);
byteBuf.release();

byteBuf = channel.readOutbound();
assertNotNull(byteBuf);
aggregatedBuffer.writeBytes(byteBuf);
byteBuf.release();

aggregatedBuffer.resetReaderIndex();
String content = aggregatedBuffer.toString(CharsetUtil.UTF_8);
assertEquals(StompTestConstants.CONNECT_FRAME, content);
aggregatedBuffer.release();
}
}

0 comments on commit 39aefdf

Please sign in to comment.