SnappyFrameDecoderTest ByteBuf leak (#9854)

Motivation:
SnappyFrameDecoderTest has a few tests which fail to close the EmbeddedChannel
and therefore may leak ByteBuf objects.

Modifications:
- Make sure EmbeddedChannel#finishAndReleaseAll() is called in all tests

Result:
No more leaks from SnappyFrameDecoderTest.
This commit is contained in:
Scott Mitchell 2019-12-06 16:27:03 -08:00 committed by Norman Maurer
parent bcb6d38026
commit bf0bd9aac9

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.compression;
import io.netty.buffer.ByteBuf; 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 org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -31,14 +32,18 @@ public class SnappyFrameDecoderTest {
channel = new EmbeddedChannel(new SnappyFrameDecoder()); channel = new EmbeddedChannel(new SnappyFrameDecoder());
} }
@After
public void tearDown() {
assertFalse(channel.finishAndReleaseAll());
}
@Test(expected = DecompressionException.class) @Test(expected = DecompressionException.class)
public void testReservedUnskippableChunkTypeCausesError() throws Exception { public void testReservedUnskippableChunkTypeCausesError() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x03, 0x01, 0x00, 0x00, 0x00 0x03, 0x01, 0x00, 0x00, 0x00
}); });
assertFalse(channel.writeInbound(in)); channel.writeInbound(in);
assertFalse(channel.finish());
} }
@Test(expected = DecompressionException.class) @Test(expected = DecompressionException.class)
@ -47,8 +52,7 @@ public class SnappyFrameDecoderTest {
-0x80, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' -0x80, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
assertFalse(channel.writeInbound(in)); channel.writeInbound(in);
assertFalse(channel.finish());
} }
@Test(expected = DecompressionException.class) @Test(expected = DecompressionException.class)
@ -57,8 +61,7 @@ public class SnappyFrameDecoderTest {
(byte) 0xff, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y' (byte) 0xff, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y'
}); });
assertFalse(channel.writeInbound(in)); channel.writeInbound(in);
assertFalse(channel.finish());
} }
@Test(expected = DecompressionException.class) @Test(expected = DecompressionException.class)
@ -76,8 +79,7 @@ public class SnappyFrameDecoderTest {
0x01, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' 0x01, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
assertFalse(channel.writeInbound(in)); channel.writeInbound(in);
assertFalse(channel.finish());
} }
@Test(expected = DecompressionException.class) @Test(expected = DecompressionException.class)
@ -86,8 +88,7 @@ public class SnappyFrameDecoderTest {
0x00, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y' 0x00, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
assertFalse(channel.writeInbound(in)); channel.writeInbound(in);
assertFalse(channel.finish());
} }
@Test @Test
@ -101,7 +102,6 @@ public class SnappyFrameDecoderTest {
assertNull(channel.readInbound()); assertNull(channel.readInbound());
assertFalse(in.isReadable()); assertFalse(in.isReadable());
assertFalse(channel.finish());
} }
@Test @Test
@ -119,7 +119,6 @@ public class SnappyFrameDecoderTest {
expected.release(); expected.release();
actual.release(); actual.release();
assertFalse(channel.finish());
} }
@Test @Test
@ -141,7 +140,6 @@ public class SnappyFrameDecoderTest {
expected.release(); expected.release();
actual.release(); actual.release();
assertFalse(channel.finish());
} }
// 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
@ -150,23 +148,25 @@ public class SnappyFrameDecoderTest {
@Test(expected = DecompressionException.class) @Test(expected = DecompressionException.class)
public void testInvalidChecksumThrowsException() throws Exception { public void testInvalidChecksumThrowsException() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true)); EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));
try {
// checksum here is presented as 0 // checksum here is presented as 0
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[]{
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y' 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
}); });
assertFalse(channel.writeInbound(in)); channel.writeInbound(in);
assertFalse(channel.finish()); } finally {
channel.finishAndReleaseAll();
}
} }
@Test @Test
public void testInvalidChecksumDoesNotThrowException() throws Exception { public void testInvalidChecksumDoesNotThrowException() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true)); EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));
try {
// checksum here is presented as a282986f (little endian) // checksum here is presented as a282986f (little endian)
ByteBuf in = Unpooled.wrappedBuffer(new byte[] { ByteBuf in = Unpooled.wrappedBuffer(new byte[]{
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
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'
}); });
@ -178,6 +178,8 @@ public class SnappyFrameDecoderTest {
expected.release(); expected.release();
actual.release(); actual.release();
assertFalse(channel.finish()); } finally {
channel.finishAndReleaseAll();
}
} }
} }