Repository: camel Updated Branches: refs/heads/master 762010031 -> f5dccb880
CAMEL-10409 Added memory leak check to camel-netty4 component Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7a93699d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7a93699d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7a93699d Branch: refs/heads/master Commit: 7a93699dccf956d6574775a4d4288bbe9be21592 Parents: 7620100 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Tue Nov 15 12:46:54 2016 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Tue Nov 15 12:46:54 2016 +0800 ---------------------------------------------------------------------- .../camel/component/netty4/BaseNettyTest.java | 25 ++++++++ .../component/netty4/LogCaptureAppender.java | 65 ++++++++++++++++++++ .../camel/component/netty4/LogCaptureTest.java | 35 +++++++++++ .../src/test/resources/log4j2.properties | 6 ++ 4 files changed, 131 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7a93699d/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/BaseNettyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/BaseNettyTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/BaseNettyTest.java index c64465e..aeb5378 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/BaseNettyTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/BaseNettyTest.java @@ -18,6 +18,7 @@ package org.apache.camel.component.netty4; import java.io.File; import java.io.FileOutputStream; +import java.util.Collection; import java.util.Properties; import org.apache.camel.CamelContext; @@ -26,9 +27,13 @@ import org.apache.camel.converter.IOConverter; import org.apache.camel.impl.JndiRegistry; import org.apache.camel.test.AvailablePortFinder; import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.logging.log4j.core.LogEvent; import org.junit.AfterClass; import org.junit.BeforeClass; +import io.netty.buffer.ByteBufAllocator; +import io.netty.util.ResourceLeakDetector; + /** * */ @@ -65,6 +70,26 @@ public class BaseNettyTest extends CamelTestSupport { } } + @BeforeClass + public static void startLeakDetection() { + System.setProperty("io.netty.leakDetection.maxRecords", "100"); + ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID); + } + + @AfterClass + public static void verifyNoLeaks() throws Exception { + //Force GC to bring up leaks + System.gc(); + //Kick leak detection logging + ByteBufAllocator.DEFAULT.buffer(1).release(); + Collection<LogEvent> events = LogCaptureAppender.getEvents(); + if (!events.isEmpty()) { + String message = "Leaks detected while running tests: " + events; + LogCaptureAppender.reset(); + throw new AssertionError(message); + } + } + @Override protected CamelContext createCamelContext() throws Exception { CamelContext context = super.createCamelContext(); http://git-wip-us.apache.org/repos/asf/camel/blob/7a93699d/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureAppender.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureAppender.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureAppender.java new file mode 100644 index 0000000..e53fb32 --- /dev/null +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureAppender.java @@ -0,0 +1,65 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty4; + +import java.io.Serializable; +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Deque; + +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginElement; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; + +/** + */ +@Plugin(name = "LogCaptureAppender", category = "Core", elementType = "appender", printObject = true) +public class LogCaptureAppender extends AbstractAppender { + private static final Deque<LogEvent> LOG_EVENTS = new ArrayDeque<>(); + + public LogCaptureAppender(String name, Filter filter, Layout<? extends Serializable> layout) { + super(name, filter, layout); + } + + public LogCaptureAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) { + super(name, filter, layout, ignoreExceptions); + } + + @PluginFactory + public static LogCaptureAppender createAppender(@PluginAttribute("name") final String name, + @PluginElement("Filter") final Filter filter) { + return new LogCaptureAppender(name, filter, null); + } + + @Override + public void append(LogEvent logEvent) { + LOG_EVENTS.add(logEvent); + } + + public static void reset() { + LOG_EVENTS.clear(); + } + + public static Collection<LogEvent> getEvents() { + return LOG_EVENTS; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/7a93699d/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureTest.java new file mode 100644 index 0000000..8ffcb62 --- /dev/null +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/LogCaptureTest.java @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty4; + +import org.junit.Assert; +import org.junit.Test; + +import io.netty.util.ResourceLeakDetector; +import io.netty.util.internal.logging.InternalLoggerFactory; + +/** + * This test ensures LogCaptureAppender is configured properly + */ +public class LogCaptureTest { + @Test + public void testCapture() { + InternalLoggerFactory.getInstance(ResourceLeakDetector.class).error("testError"); + Assert.assertFalse(LogCaptureAppender.getEvents().isEmpty()); + LogCaptureAppender.reset(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/7a93699d/components/camel-netty4/src/test/resources/log4j2.properties ---------------------------------------------------------------------- diff --git a/components/camel-netty4/src/test/resources/log4j2.properties b/components/camel-netty4/src/test/resources/log4j2.properties index 1a86f09..2b0fc5f 100644 --- a/components/camel-netty4/src/test/resources/log4j2.properties +++ b/components/camel-netty4/src/test/resources/log4j2.properties @@ -15,6 +15,7 @@ ## limitations under the License. ## --------------------------------------------------------------------------- +configuration.packages=org.apache.camel.component.netty4 appender.file.type = File appender.file.name = file appender.file.fileName = target/camel-netty4-test.log @@ -24,5 +25,10 @@ appender.out.type = Console appender.out.name = out appender.out.layout.type = PatternLayout appender.out.layout.pattern = %d [%-35.35t] %-5p %-30.30c{1} - %m%n +appender.capture.type=LogCaptureAppender +appender.capture.name=capture + +logger.leak.name = io.netty.util.ResourceLeakDetector +logger.leak.appenderRef.capture.ref = capture rootLogger.level = INFO rootLogger.appenderRef.file.ref = file