Free up all buffers after the tests

This commit is contained in:
Norman Maurer 2013-08-28 11:00:08 +02:00
parent 7a38163743
commit 51a536fd30
3 changed files with 61 additions and 109 deletions

View File

@ -15,15 +15,6 @@
*/
package io.netty.handler.codec.compression;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
public class JZlibTest extends ZlibTest {
@Override
@ -35,61 +26,4 @@ public class JZlibTest extends ZlibTest {
protected ZlibDecoder createDecoder(ZlibWrapper wrapper) {
return new JZlibDecoder(wrapper);
}
@Test
public void testZLIB_OR_NONE() throws Exception {
ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
EmbeddedChannel chEncoder = new EmbeddedChannel(new JZlibEncoder(ZlibWrapper.NONE));
chEncoder.writeOutbound(data.copy());
assertTrue(chEncoder.finish());
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
EmbeddedChannel chDecoderZlibOrNone = new EmbeddedChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE));
chDecoderZlibOrNone.writeInbound(deflatedData);
assertTrue(chDecoderZlibOrNone.finish());
assertEquals(data, chDecoderZlibOrNone.readInbound());
}
@Test
public void testZLIB_OR_NONE2() throws Exception {
ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
EmbeddedChannel chEncoder = new EmbeddedChannel(new JZlibEncoder(ZlibWrapper.ZLIB));
chEncoder.writeOutbound(data.copy());
assertTrue(chEncoder.finish());
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
EmbeddedChannel chDecoderZlibOrNone = new EmbeddedChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE));
chDecoderZlibOrNone.writeInbound(deflatedData);
assertTrue(chDecoderZlibOrNone.finish());
assertEquals(data, chDecoderZlibOrNone.readInbound());
}
@Test
public void testZLIB_OR_NONE3() throws Exception {
ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
EmbeddedChannel chEncoder = new EmbeddedChannel(new JZlibEncoder(ZlibWrapper.GZIP));
chEncoder.writeOutbound(data.copy());
assertTrue(chEncoder.finish());
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
EmbeddedChannel chDecoderZlibOrNone = new EmbeddedChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE));
chDecoderZlibOrNone.writeInbound(deflatedData);
assertTrue(chDecoderZlibOrNone.finish());
assertEquals(data, chDecoderZlibOrNone.readInbound());
}
}

View File

@ -30,8 +30,21 @@ public class JdkZlibTest extends ZlibTest {
return new JdkZlibDecoder(wrapper);
}
@Override
@Test(expected = IllegalArgumentException.class)
public void testZLIB_OR_NONE() throws Exception {
new JdkZlibDecoder(ZlibWrapper.ZLIB_OR_NONE);
}
@Override
@Test(expected = IllegalArgumentException.class)
public void testZLIB_OR_NONE2() throws Exception {
new JdkZlibDecoder(ZlibWrapper.ZLIB_OR_NONE);
}
@Override
@Test(expected = IllegalArgumentException.class)
public void testZLIB_OR_NONE3() throws Exception {
new JdkZlibDecoder(ZlibWrapper.ZLIB_OR_NONE);
}
}

View File

@ -26,6 +26,7 @@ import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public abstract class ZlibTest {
@ -41,64 +42,68 @@ public abstract class ZlibTest {
EmbeddedChannel chDecoderGZip = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP));
chDecoderGZip.writeInbound(deflatedData.copy());
assertTrue(chDecoderGZip.finish());
assertEquals(chDecoderGZip.readInbound(), data);
ByteBuf buf = (ByteBuf) chDecoderGZip.readInbound();
assertEquals(buf, data);
assertNull(chDecoderGZip.readInbound());
data.release();
deflatedData.release();
buf.release();
}
protected void testCompress(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper) throws Exception {
ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));
chEncoder.writeOutbound(data.copy());
assertTrue(chEncoder.finish());
EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
for (;;) {
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
if (deflatedData == null) {
break;
}
chDecoderZlib.writeInbound(deflatedData);
}
assertTrue(chDecoderZlib.finish());
ByteBuf buf = (ByteBuf) chDecoderZlib.readInbound();
assertEquals(data, buf);
assertNull(chDecoderZlib.readInbound());
data.release();
buf.release();
}
@Test
public void testZLIB() throws Exception {
ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.ZLIB));
chEncoder.writeOutbound(data.copy());
assertTrue(chEncoder.finish());
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(ZlibWrapper.ZLIB));
chDecoderZlib.writeInbound(deflatedData.copy());
assertTrue(chDecoderZlib.finish());
assertEquals(data, chDecoderZlib.readInbound());
testCompress(ZlibWrapper.ZLIB, ZlibWrapper.ZLIB);
}
@Test
public void testNONE() throws Exception {
ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.NONE));
chEncoder.writeOutbound(data.copy());
assertTrue(chEncoder.finish());
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
EmbeddedChannel chDecoderZlibNone = new EmbeddedChannel(createDecoder(ZlibWrapper.NONE));
chDecoderZlibNone.writeInbound(deflatedData.copy());
assertTrue(chDecoderZlibNone.finish());
assertEquals(data, chDecoderZlibNone.readInbound());
testCompress(ZlibWrapper.NONE, ZlibWrapper.NONE);
}
@Test
public void testGZIP() throws Exception {
ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
testCompress(ZlibWrapper.GZIP, ZlibWrapper.GZIP);
}
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.GZIP));
@Test
public void testZLIB_OR_NONE() throws Exception {
testCompress(ZlibWrapper.NONE, ZlibWrapper.ZLIB_OR_NONE);
}
chEncoder.writeOutbound(data.copy());
assertTrue(chEncoder.finish());
@Test
public void testZLIB_OR_NONE2() throws Exception {
testCompress(ZlibWrapper.ZLIB, ZlibWrapper.ZLIB_OR_NONE);
}
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
EmbeddedChannel chDecoderGZip = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP));
chDecoderGZip.writeInbound(deflatedData.copy());
assertTrue(chDecoderGZip.finish());
assertEquals(data, chDecoderGZip.readInbound());
@Test
public void testZLIB_OR_NONE3() throws Exception {
testCompress(ZlibWrapper.GZIP, ZlibWrapper.ZLIB_OR_NONE);
}
private static byte[] gzip(String message) throws IOException {