LOG4J2-2031 added tests
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/3f5d58cd Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/3f5d58cd Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/3f5d58cd Branch: refs/heads/master Commit: 3f5d58cd32ad96ccb4a644fc32be11ffca0e5922 Parents: 4410073 Author: rpopma <[email protected]> Authored: Thu Sep 21 00:33:06 2017 +0900 Committer: rpopma <[email protected]> Committed: Thu Sep 21 00:33:06 2017 +0900 ---------------------------------------------------------------------- .../core/async/QueueFullAsyncAppenderTest.java | 93 ++++++++++++++++ .../core/async/QueueFullAsyncAppenderTest2.java | 70 ++++++++++++ ...syncLoggerConfigLoggingFromToStringTest.java | 10 +- ...yncLoggerConfigLoggingFromToStringTest2.java | 73 +++++++++++++ .../async/QueueFullAsyncLoggerConfigTest.java | 6 ++ .../async/QueueFullAsyncLoggerConfigTest2.java | 71 ++++++++++++ ...eFullAsyncLoggerLoggingFromToStringTest.java | 10 +- ...FullAsyncLoggerLoggingFromToStringTest2.java | 108 +++++++++++++++++++ .../core/async/QueueFullAsyncLoggerTest.java | 7 +- .../core/async/QueueFullAsyncLoggerTest2.java | 83 ++++++++++++++ .../resources/log4j2-queueFullAsyncAppender.xml | 31 ++++++ 11 files changed, 557 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest.java new file mode 100644 index 0000000..dae5095 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest.java @@ -0,0 +1,93 @@ +/* + * 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.logging.log4j.core.async; + +import java.util.Stack; +import java.util.concurrent.CountDownLatch; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.AsyncLoggers; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.message.ParameterizedMessage; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +import static org.junit.Assert.*; + +/** + * Tests queue full scenarios with AsyncAppender. + */ +@RunWith(BlockJUnit4ClassRunner.class) +@Category(AsyncLoggers.class) +public class QueueFullAsyncAppenderTest extends QueueFullAbstractTest { + + @BeforeClass + public static void beforeClass() { + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, + "log4j2-queueFullAsyncAppender.xml"); + } + + @Rule + public LoggerContextRule context = new LoggerContextRule( + "log4j2-queueFullAsyncAppender.xml"); + + @Before + public void before() throws Exception { + blockingAppender = context.getRequiredAppender("Blocking", BlockingAppender.class); + } + + + @Test(timeout = 5000) + public void testNormalQueueFullKeepsMessagesInOrder() throws InterruptedException { + final Logger logger = LogManager.getLogger(this.getClass()); + + blockingAppender.countDownLatch = new CountDownLatch(1); + unlocker = new Unlocker(new CountDownLatch(129)); + unlocker.start(); + + asyncAppenderTest(logger, unlocker, blockingAppender); + } + + static void asyncAppenderTest(final Logger logger, + final Unlocker unlocker, + final BlockingAppender blockingAppender) { + for (int i = 0; i < 130; i++) { + TRACE("Test logging message " + i + ". Remaining capacity=" + asyncRemainingCapacity(logger)); + TRACE("Test decrementing unlocker countdown latch. Count=" + unlocker.countDownLatch.getCount()); + unlocker.countDownLatch.countDown(); + final String param = "I'm innocent"; + logger.info(new ParameterizedMessage("logging innocent object #{} {}", i, param)); + } + TRACE("Before stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size()); + //CoreLoggerContexts.stopLoggerContext(false); // stop async thread + while (blockingAppender.logEvents.size() < 130) { Thread.yield(); } + TRACE("After stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size()); + + final Stack<String> actual = transform(blockingAppender.logEvents); + for (int i = 0; i < 130; i++) { + assertEquals("logging innocent object #" + i + " I'm innocent", actual.pop()); + } + assertTrue(actual.isEmpty()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest2.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest2.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest2.java new file mode 100644 index 0000000..d8ce85c --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncAppenderTest2.java @@ -0,0 +1,70 @@ +/* + * 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.logging.log4j.core.async; + +import java.util.concurrent.CountDownLatch; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.AsyncLoggers; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * Tests queue full scenarios with AsyncAppender. + */ +@RunWith(BlockJUnit4ClassRunner.class) +@Category(AsyncLoggers.class) +public class QueueFullAsyncAppenderTest2 extends QueueFullAbstractTest { + + @BeforeClass + public static void beforeClass() { + //FORMAT_MESSAGES_IN_BACKGROUND + System.setProperty("log4j.format.msg.async", "true"); + + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, + "log4j2-queueFullAsyncAppender.xml"); + } + + @Rule + public LoggerContextRule context = new LoggerContextRule( + "log4j2-queueFullAsyncAppender.xml"); + + @Before + public void before() throws Exception { + blockingAppender = context.getRequiredAppender("Blocking", BlockingAppender.class); + } + + + @Test(timeout = 5000) + public void testNormalQueueFullKeepsMessagesInOrder() throws InterruptedException { + final Logger logger = LogManager.getLogger(this.getClass()); + + blockingAppender.countDownLatch = new CountDownLatch(1); + unlocker = new Unlocker(new CountDownLatch(129)); + unlocker.start(); + + QueueFullAsyncAppenderTest.asyncAppenderTest(logger, unlocker, blockingAppender); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest.java index 0af0647..8aef3e9 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest.java @@ -45,7 +45,6 @@ public class QueueFullAsyncLoggerConfigLoggingFromToStringTest extends QueueFull public static void beforeClass() { System.setProperty("log4j2.enable.threadlocals", "true"); System.setProperty("log4j2.is.webapp", "false"); -// System.setProperty("AsyncLogger.RingBufferSize", "128"); // minimum ringbuffer size System.setProperty("AsyncLoggerConfig.RingBufferSize", "128"); System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2-queueFullAsyncLoggerConfig.xml"); @@ -69,11 +68,18 @@ public class QueueFullAsyncLoggerConfigLoggingFromToStringTest extends QueueFull unlocker = new Unlocker(new CountDownLatch(129)); // count slightly different from "pure" async loggers unlocker.start(); + asyncLoggerConfigRecursiveTest(logger, unlocker, blockingAppender, this); + } + + static void asyncLoggerConfigRecursiveTest(final Logger logger, + final Unlocker unlocker, + final BlockingAppender blockingAppender, + final QueueFullAbstractTest factory) { for (int i = 0; i < 1; i++) { TRACE("Test logging message " + i + ". Remaining capacity=" + asyncRemainingCapacity(logger)); TRACE("Test decrementing unlocker countdown latch. Count=" + unlocker.countDownLatch.getCount()); unlocker.countDownLatch.countDown(); - final DomainObject obj = new DomainObject(129); + final DomainObject obj = factory.new DomainObject(129); logger.info("logging naughty object #{} {}", i, obj); } TRACE("Before stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size()); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest2.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest2.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest2.java new file mode 100644 index 0000000..ee11629 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigLoggingFromToStringTest2.java @@ -0,0 +1,73 @@ +/* + * 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.logging.log4j.core.async; + +import java.util.concurrent.CountDownLatch; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.AsyncLoggers; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * Tests queue full scenarios with AsyncLoggers in configuration. + */ +@RunWith(BlockJUnit4ClassRunner.class) +@Category(AsyncLoggers.class) +public class QueueFullAsyncLoggerConfigLoggingFromToStringTest2 extends QueueFullAbstractTest { + + @BeforeClass + public static void beforeClass() { + //FORMAT_MESSAGES_IN_BACKGROUND + System.setProperty("log4j.format.msg.async", "true"); + + System.setProperty("log4j2.enable.threadlocals", "true"); + System.setProperty("log4j2.is.webapp", "false"); + System.setProperty("AsyncLoggerConfig.RingBufferSize", "128"); + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, + "log4j2-queueFullAsyncLoggerConfig.xml"); + } + + @Rule + public LoggerContextRule context = new LoggerContextRule( + "log4j2-queueFullAsyncLoggerConfig.xml"); + + @Before + public void before() throws Exception { + blockingAppender = context.getRequiredAppender("Blocking", BlockingAppender.class); + } + + @Test(timeout = 5000) + public void testLoggingFromToStringCausesOutOfOrderMessages() throws InterruptedException { + //TRACE = true; + final Logger logger = LogManager.getLogger(this.getClass()); + + blockingAppender.countDownLatch = new CountDownLatch(1); + unlocker = new Unlocker(new CountDownLatch(129)); // count slightly different from "pure" async loggers + unlocker.start(); + + QueueFullAsyncLoggerConfigLoggingFromToStringTest.asyncLoggerConfigRecursiveTest(logger, unlocker, blockingAppender, this); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest.java index a7ef8ed..7d68582 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest.java @@ -67,6 +67,12 @@ public class QueueFullAsyncLoggerConfigTest extends QueueFullAbstractTest { unlocker = new Unlocker(new CountDownLatch(129)); unlocker.start(); + asyncLoggerConfigTest(logger, unlocker, blockingAppender); + } + + static void asyncLoggerConfigTest(final Logger logger, + final Unlocker unlocker, + final BlockingAppender blockingAppender) { for (int i = 0; i < 130; i++) { TRACE("Test logging message " + i + ". Remaining capacity=" + asyncRemainingCapacity(logger)); TRACE("Test decrementing unlocker countdown latch. Count=" + unlocker.countDownLatch.getCount()); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest2.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest2.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest2.java new file mode 100644 index 0000000..ff5ce7c --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerConfigTest2.java @@ -0,0 +1,71 @@ +/* + * 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.logging.log4j.core.async; + +import java.util.concurrent.CountDownLatch; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.AsyncLoggers; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * Tests queue full scenarios with AsyncLoggers in configuration. + */ +@RunWith(BlockJUnit4ClassRunner.class) +@Category(AsyncLoggers.class) +public class QueueFullAsyncLoggerConfigTest2 extends QueueFullAbstractTest { + + @BeforeClass + public static void beforeClass() { + //FORMAT_MESSAGES_IN_BACKGROUND + System.setProperty("log4j.format.msg.async", "true"); + + System.setProperty("AsyncLoggerConfig.RingBufferSize", "128"); // minimum ringbuffer size + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, + "log4j2-queueFullAsyncLoggerConfig.xml"); + } + + @Rule + public LoggerContextRule context = new LoggerContextRule( + "log4j2-queueFullAsyncLoggerConfig.xml"); + + @Before + public void before() throws Exception { + blockingAppender = context.getRequiredAppender("Blocking", BlockingAppender.class); + } + + + @Test(timeout = 5000) + public void testNormalQueueFullKeepsMessagesInOrder() throws InterruptedException { + final Logger logger = LogManager.getLogger(this.getClass()); + + blockingAppender.countDownLatch = new CountDownLatch(1); + unlocker = new Unlocker(new CountDownLatch(129)); + unlocker.start(); + + QueueFullAsyncLoggerConfigTest.asyncLoggerConfigTest(logger, unlocker, blockingAppender); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest.java index 6aedc0e..7039bca 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest.java @@ -49,7 +49,6 @@ public class QueueFullAsyncLoggerLoggingFromToStringTest extends QueueFullAbstra @BeforeClass public static void beforeClass() { System.setProperty("AsyncLogger.RingBufferSize", "128"); // minimum ringbuffer size -// System.setProperty("Log4jContextSelector", AsyncLoggerContextSelector.class.getName()); System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2-queueFull.xml"); } @@ -76,11 +75,18 @@ public class QueueFullAsyncLoggerLoggingFromToStringTest extends QueueFullAbstra unlocker = new Unlocker(new CountDownLatch(129)); unlocker.start(); + asyncLoggerRecursiveTest(logger, unlocker, blockingAppender, this); + } + + static void asyncLoggerRecursiveTest(final Logger logger, + final Unlocker unlocker, + final BlockingAppender blockingAppender, + final QueueFullAbstractTest factory) { for (int i = 0; i < 1; i++) { TRACE("Test logging message " + i + ". Remaining capacity=" + asyncRemainingCapacity(logger)); TRACE("Test decrementing unlocker countdown latch. Count=" + unlocker.countDownLatch.getCount()); unlocker.countDownLatch.countDown(); - final DomainObject obj = new DomainObject(129); + final DomainObject obj = factory.new DomainObject(129); logger.info(new ParameterizedMessage("logging naughty object #{} {}", i, obj)); } TRACE("Before stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size()); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest2.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest2.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest2.java new file mode 100644 index 0000000..1691838 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerLoggingFromToStringTest2.java @@ -0,0 +1,108 @@ +/* + * 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.logging.log4j.core.async; + +import java.util.Stack; +import java.util.concurrent.CountDownLatch; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.AsyncLoggers; +import org.apache.logging.log4j.core.CoreLoggerContexts; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.message.ParameterizedMessage; +import org.apache.logging.log4j.util.Strings; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +import static org.junit.Assert.*; + +/** + * Tests queue full scenarios with pure AsyncLoggers (all loggers async). + */ +@Ignore +@RunWith(BlockJUnit4ClassRunner.class) +@Category(AsyncLoggers.class) +public class QueueFullAsyncLoggerLoggingFromToStringTest2 extends QueueFullAbstractTest { + + @BeforeClass + public static void beforeClass() { + //FORMAT_MESSAGES_IN_BACKGROUND + System.setProperty("log4j.format.msg.async", "true"); + + System.setProperty("AsyncLogger.RingBufferSize", "128"); // minimum ringbuffer size + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, + "log4j2-queueFull.xml"); + } + + @AfterClass + public static void afterClass() { + System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY); + } + + @Rule + public LoggerContextRule context = new LoggerContextRule( + "log4j2-queueFull.xml", AsyncLoggerContextSelector.class); + + @Before + public void before() throws Exception { + blockingAppender = context.getRequiredAppender("Blocking", BlockingAppender.class); + TRACE = true; + } + + @Ignore + @Test(timeout = 5000) + public void testLoggingFromToStringCausesOutOfOrderMessages() throws InterruptedException { + final Logger logger = LogManager.getLogger(this.getClass()); + + blockingAppender.countDownLatch = new CountDownLatch(1); + unlocker = new Unlocker(new CountDownLatch(129)); + unlocker.start(); + + for (int i = 0; i < 1; i++) { + TRACE("Test logging message " + i + ". Remaining capacity=" + asyncRemainingCapacity(logger)); + TRACE("Test decrementing unlocker countdown latch. Count=" + unlocker.countDownLatch.getCount()); + unlocker.countDownLatch.countDown(); + final DomainObject obj = new DomainObject(129); + logger.info(new ParameterizedMessage("logging naughty object #{} {}", i, obj)); + } + TRACE("Before stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size()); + CoreLoggerContexts.stopLoggerContext(false); // stop async thread + while (blockingAppender.logEvents.size() < 129) { Thread.yield(); } + TRACE("After stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size()); + + final Stack<String> actual = transform(blockingAppender.logEvents); + assertEquals("Jumped the queue: test(2)+domain1(65)+domain2(61)=128: queue full", + "Logging in toString() #127 (Log4j2 logged this message out of order to prevent deadlock caused by domain objects logging from their toString method when the async queue is full - LOG4J2-2031)", actual.pop()); + assertEquals("Logging in toString() #128 (Log4j2 logged this message out of order to prevent deadlock caused by domain objects logging from their toString method when the async queue is full - LOG4J2-2031)", actual.pop()); + assertEquals("logging naughty object #0 Who's bad?!", actual.pop()); + + for (int i = 0; i < 127; i++) { + assertEquals("First batch", "Logging in toString() #" + i, actual.pop()); + } + assertTrue(actual.isEmpty()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest.java index 9dad517..e5ba78d 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest.java @@ -48,7 +48,6 @@ public class QueueFullAsyncLoggerTest extends QueueFullAbstractTest { @BeforeClass public static void beforeClass() { System.setProperty("AsyncLogger.RingBufferSize", "128"); // minimum ringbuffer size -// System.setProperty("Log4jContextSelector", AsyncLoggerContextSelector.class.getName()); System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2-queueFull.xml"); } @@ -76,6 +75,12 @@ public class QueueFullAsyncLoggerTest extends QueueFullAbstractTest { unlocker = new Unlocker(new CountDownLatch(129)); unlocker.start(); + asyncLoggerTest(logger, unlocker, blockingAppender); + } + + static void asyncLoggerTest(final Logger logger, + final Unlocker unlocker, + final BlockingAppender blockingAppender) { for (int i = 0; i < 130; i++) { TRACE("Test logging message " + i + ". Remaining capacity=" + asyncRemainingCapacity(logger)); TRACE("Test decrementing unlocker countdown latch. Count=" + unlocker.countDownLatch.getCount()); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest2.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest2.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest2.java new file mode 100644 index 0000000..5b508cb --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest2.java @@ -0,0 +1,83 @@ +/* + * 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.logging.log4j.core.async; + +import java.util.Stack; +import java.util.concurrent.CountDownLatch; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.AsyncLoggers; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.message.ParameterizedMessage; +import org.apache.logging.log4j.util.Strings; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +import static org.junit.Assert.*; + +/** + * Tests queue full scenarios with pure AsyncLoggers (all loggers async). + */ +@RunWith(BlockJUnit4ClassRunner.class) +@Category(AsyncLoggers.class) +public class QueueFullAsyncLoggerTest2 extends QueueFullAbstractTest { + + @BeforeClass + public static void beforeClass() { + //FORMAT_MESSAGES_IN_BACKGROUND + System.setProperty("log4j.format.msg.async", "true"); + + System.setProperty("AsyncLogger.RingBufferSize", "128"); // minimum ringbuffer size + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, + "log4j2-queueFull.xml"); + } + + @AfterClass + public static void afterClass() { + System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY); + } + + @Rule + public LoggerContextRule context = new LoggerContextRule( + "log4j2-queueFull.xml", AsyncLoggerContextSelector.class); + + @Before + public void before() throws Exception { + blockingAppender = context.getRequiredAppender("Blocking", BlockingAppender.class); + } + + + @Test(timeout = 5000) + public void testNormalQueueFullKeepsMessagesInOrder() throws InterruptedException { + final Logger logger = LogManager.getLogger(this.getClass()); + + blockingAppender.countDownLatch = new CountDownLatch(1); + unlocker = new Unlocker(new CountDownLatch(129)); + unlocker.start(); + + QueueFullAsyncLoggerTest.asyncLoggerTest(logger, unlocker, blockingAppender); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f5d58cd/log4j-core/src/test/resources/log4j2-queueFullAsyncAppender.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j2-queueFullAsyncAppender.xml b/log4j-core/src/test/resources/log4j2-queueFullAsyncAppender.xml new file mode 100644 index 0000000..953e402 --- /dev/null +++ b/log4j-core/src/test/resources/log4j2-queueFullAsyncAppender.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<Configuration status="WARN"> + <Appenders> + <Blocking name="Blocking"> + </Blocking> + <Async name="async" bufferSize="128"> + <AppenderRef ref="Blocking"/> + </Async> + </Appenders> + <Loggers> + <Root level="debug"> + <AppenderRef ref="async" /> + </Root> + </Loggers> +</Configuration>
