Fix buffer leaks in BinaryMemcacheDecoderTest

This commit is contained in:
Trustin Lee 2013-12-07 15:06:30 +09:00
parent f03f1eaaba
commit 5e86ccace9

View File

@ -21,6 +21,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.memcache.LastMemcacheContent; import io.netty.handler.codec.memcache.LastMemcacheContent;
import io.netty.handler.codec.memcache.MemcacheContent; import io.netty.handler.codec.memcache.MemcacheContent;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCounted;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -71,6 +73,11 @@ public class BinaryMemcacheDecoderTest {
channel = new EmbeddedChannel(new BinaryMemcacheRequestDecoder()); channel = new EmbeddedChannel(new BinaryMemcacheRequestDecoder());
} }
@After
public void teardown() throws Exception {
channel.finish();
}
/** /**
* This tests a simple GET request with a key as the value. * This tests a simple GET request with a key as the value.
*/ */
@ -151,6 +158,8 @@ public class BinaryMemcacheDecoderTest {
assertThat(request.getKey(), notNullValue()); assertThat(request.getKey(), notNullValue());
assertThat(request.getExtras(), nullValue()); assertThat(request.getExtras(), nullValue());
request.release();
MemcacheContent content1 = (MemcacheContent) channel.readInbound(); MemcacheContent content1 = (MemcacheContent) channel.readInbound();
MemcacheContent content2 = (MemcacheContent) channel.readInbound(); MemcacheContent content2 = (MemcacheContent) channel.readInbound();
@ -159,6 +168,9 @@ public class BinaryMemcacheDecoderTest {
assertThat(content1.content().readableBytes(), is(3)); assertThat(content1.content().readableBytes(), is(3));
assertThat(content2.content().readableBytes(), is(5)); assertThat(content2.content().readableBytes(), is(5));
content1.release();
content2.release();
} }
/** /**
@ -172,12 +184,20 @@ public class BinaryMemcacheDecoderTest {
BinaryMemcacheRequest request = (BinaryMemcacheRequest) channel.readInbound(); BinaryMemcacheRequest request = (BinaryMemcacheRequest) channel.readInbound();
assertThat(request, instanceOf(BinaryMemcacheRequest.class)); assertThat(request, instanceOf(BinaryMemcacheRequest.class));
assertThat(request, notNullValue()); assertThat(request, notNullValue());
assertThat(channel.readInbound(), instanceOf(LastMemcacheContent.class)); request.release();
Object lastContent = channel.readInbound();
assertThat(lastContent, instanceOf(LastMemcacheContent.class));
((ReferenceCounted) lastContent).release();
request = (BinaryMemcacheRequest) channel.readInbound(); request = (BinaryMemcacheRequest) channel.readInbound();
assertThat(request, instanceOf(BinaryMemcacheRequest.class)); assertThat(request, instanceOf(BinaryMemcacheRequest.class));
assertThat(request, notNullValue()); assertThat(request, notNullValue());
assertThat(channel.readInbound(), instanceOf(LastMemcacheContent.class)); request.release();
lastContent = channel.readInbound();
assertThat(lastContent, instanceOf(LastMemcacheContent.class));
((ReferenceCounted) lastContent).release();
} }
@Test @Test
@ -192,36 +212,42 @@ public class BinaryMemcacheDecoderTest {
BinaryMemcacheResponse response = (BinaryMemcacheResponse) channel.readInbound(); BinaryMemcacheResponse response = (BinaryMemcacheResponse) channel.readInbound();
assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT)); assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length())); assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length()));
response.release();
// First message first content chunk // First message first content chunk
MemcacheContent content = (MemcacheContent) channel.readInbound(); MemcacheContent content = (MemcacheContent) channel.readInbound();
assertThat(content, instanceOf(LastMemcacheContent.class)); assertThat(content, instanceOf(LastMemcacheContent.class));
assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody)); assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody));
content.release();
// Second message // Second message
response = (BinaryMemcacheResponse) channel.readInbound(); response = (BinaryMemcacheResponse) channel.readInbound();
assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT)); assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length())); assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length()));
response.release();
// Second message first content chunk // Second message first content chunk
content = (MemcacheContent) channel.readInbound(); content = (MemcacheContent) channel.readInbound();
assertThat(content, instanceOf(MemcacheContent.class)); assertThat(content, instanceOf(MemcacheContent.class));
assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody.substring(0, 7))); assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody.substring(0, 7)));
content.release();
// Second message second content chunk // Second message second content chunk
content = (MemcacheContent) channel.readInbound(); content = (MemcacheContent) channel.readInbound();
assertThat(content, instanceOf(LastMemcacheContent.class)); assertThat(content, instanceOf(LastMemcacheContent.class));
assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody.substring(7, 9))); assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody.substring(7, 9)));
content.release();
// Third message // Third message
response = (BinaryMemcacheResponse) channel.readInbound(); response = (BinaryMemcacheResponse) channel.readInbound();
assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT)); assertThat(response.getHeader().getStatus(), is(BinaryMemcacheResponseStatus.KEY_ENOENT));
assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length())); assertThat(response.getHeader().getTotalBodyLength(), is(msgBody.length()));
response.release();
// Third message first content chunk // Third message first content chunk
content = (MemcacheContent) channel.readInbound(); content = (MemcacheContent) channel.readInbound();
assertThat(content, instanceOf(LastMemcacheContent.class)); assertThat(content, instanceOf(LastMemcacheContent.class));
assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody)); assertThat(content.content().toString(CharsetUtil.UTF_8), is(msgBody));
content.release();
} }
} }