CAMEL-9040: Fixed netty leak in unit test and a NPE
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/70164066 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/70164066 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/70164066 Branch: refs/heads/master Commit: 70164066813e49623ba1cab9176e33bbdc74eef2 Parents: 1cca6b7 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed May 4 10:05:36 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed May 4 14:07:56 2016 +0200 ---------------------------------------------------------------------- .../org/apache/camel/component/netty4/NettyConverter.java | 8 ++++++-- .../org/apache/camel/component/netty4/MyCustomCodec.java | 2 -- .../apache/camel/component/netty4/NettyConverterTest.java | 9 +++++++-- 3 files changed, 13 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/70164066/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConverter.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConverter.java b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConverter.java index 780e1fe..c3af8db 100644 --- a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConverter.java +++ b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConverter.java @@ -36,7 +36,6 @@ import io.netty.buffer.ByteBufInputStream; import org.apache.camel.Converter; import org.apache.camel.Exchange; - /** * A set of converter methods for working with Netty types * @@ -56,7 +55,12 @@ public final class NettyConverter { } byte[] bytes = new byte[buffer.readableBytes()]; int readerIndex = buffer.readerIndex(); - buffer.getBytes(readerIndex, bytes); + buffer.retain(); + try { + buffer.getBytes(readerIndex, bytes); + } finally { + buffer.release(); + } return bytes; } http://git-wip-us.apache.org/repos/asf/camel/blob/70164066/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/MyCustomCodec.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/MyCustomCodec.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/MyCustomCodec.java index 363d34d..a20069e 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/MyCustomCodec.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/MyCustomCodec.java @@ -58,8 +58,6 @@ public final class MyCustomCodec { int readerIndex = msg.readerIndex(); msg.getBytes(readerIndex, bytes); out.add(bytes); - } else { - out.add((byte[])null); } } http://git-wip-us.apache.org/repos/asf/camel/blob/70164066/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConverterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConverterTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConverterTest.java index bb4f8a1..7023b96 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConverterTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConverterTest.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.PooledByteBufAllocator; import org.apache.camel.impl.DefaultExchange; import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -31,7 +32,7 @@ public class NettyConverterTest extends CamelTestSupport { /** * Test payload to send. */ - private static final String PAYLOAD = "Test Message"; + private static final String PAYLOAD = "Test Message"; private ByteBuf buf; @@ -42,6 +43,11 @@ public class NettyConverterTest extends CamelTestSupport { buf.writeBytes(bytes); } + @After + public void tearDown() { + buf.release(); + } + @Test public void testConversionWithExchange() { String result = context.getTypeConverter().convertTo(String.class, new DefaultExchange(context), buf); @@ -49,7 +55,6 @@ public class NettyConverterTest extends CamelTestSupport { assertEquals(PAYLOAD, result); } - @Test public void testConversionWithoutExchange() { String result = context.getTypeConverter().convertTo(String.class, buf);