Skip to content

Commit f5af863

Browse files
committed
Move async logger tests to log4j-async-logger
We move all the tests using `AsyncLogger` and `AsyncLoggerConfig` from `log4j-core-test` to a new artifact `log4j-async-logger` in preparation for splitting async support out of `log4j-core`.
1 parent 44613d0 commit f5af863

File tree

83 files changed

+609
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+609
-191
lines changed

log4j-async-logger/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<parent>
21+
<groupId>org.apache.logging.log4j</groupId>
22+
<artifactId>log4j</artifactId>
23+
<version>${revision}</version>
24+
<relativePath>../log4j-parent</relativePath>
25+
</parent>
26+
27+
<artifactId>log4j-async-logger</artifactId>
28+
<name>Log4j Async Logger</name>
29+
<description>Alternative implementation of logger that uses LMAX Disruptor.</description>
30+
31+
<dependencies>
32+
33+
<dependency>
34+
<groupId>org.apache.logging.log4j</groupId>
35+
<artifactId>log4j-core</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>com.lmax</groupId>
40+
<artifactId>disruptor</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.apache.logging.log4j</groupId>
45+
<artifactId>log4j-core-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.awaitility</groupId>
51+
<artifactId>awaitility</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.junit.jupiter</groupId>
57+
<artifactId>junit-jupiter-api</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.mockito</groupId>
63+
<artifactId>mockito-core</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
</dependencies>
68+
</project>
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,24 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertNotNull;
2020

21-
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
21+
import org.apache.logging.log4j.LogManager;
22+
import org.apache.logging.log4j.Logger;
23+
import org.apache.logging.log4j.test.junit.SetTestProperty;
2224
import org.junit.jupiter.api.Tag;
2325
import org.junit.jupiter.api.Test;
2426
import org.junit.jupiter.api.Timeout;
25-
import org.junitpioneer.jupiter.SetSystemProperty;
2627

2728
/**
2829
* Test class loading deadlock condition from the LOG4J2-1457
2930
*/
3031
@Tag("async")
31-
@SetSystemProperty(
32-
key = Log4jPropertyKey.Constant.CONTEXT_SELECTOR_CLASS_NAME,
32+
@SetTestProperty(
33+
key = "LoggerContext.selector",
3334
value = "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector")
34-
@SetSystemProperty(key = Log4jPropertyKey.Constant.ASYNC_LOGGER_RING_BUFFER_SIZE, value = "128")
35-
@SetSystemProperty(key = Log4jPropertyKey.Constant.CONFIG_LOCATION, value = "AsyncLoggerConsoleTest.xml")
35+
@SetTestProperty(key = "AsyncLogger.ringBufferSize", value = "128")
36+
@SetTestProperty(
37+
key = "Configuration.file",
38+
value = "org/apache/logging/log4j/core/async/AsyncLoggerClassLoadDeadlockTest.xml")
3639
public class AsyncLoggerClassLoadDeadlockTest {
3740

3841
static final int RING_BUFFER_SIZE = 128;
@@ -41,7 +44,19 @@ public class AsyncLoggerClassLoadDeadlockTest {
4144
@Timeout(value = 30)
4245
public void testClassLoaderDeadlock() throws Exception {
4346
// touch the class so static init will be called
44-
final AsyncLoggerClassLoadDeadlock temp = new AsyncLoggerClassLoadDeadlock();
47+
final DeadLock temp = new DeadLock();
4548
assertNotNull(temp);
4649
}
50+
51+
static final class DeadLock {
52+
static {
53+
final Logger log = LogManager.getLogger("com.foo.bar.deadlock");
54+
final Exception e = new Exception();
55+
// the key to reproducing the problem is to fill up the ring buffer so that
56+
// log.info call will block on ring buffer as well
57+
for (int i = 0; i < AsyncLoggerClassLoadDeadlockTest.RING_BUFFER_SIZE * 2; ++i) {
58+
log.info("clinit", e);
59+
}
60+
}
61+
}
4762
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigTest2.java renamed to log4j-async-logger/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfig2Test.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,37 @@
2222
import java.io.BufferedReader;
2323
import java.io.File;
2424
import java.io.FileReader;
25-
import org.apache.logging.log4j.LogManager;
25+
import java.nio.file.Path;
2626
import org.apache.logging.log4j.Logger;
2727
import org.apache.logging.log4j.core.LoggerContext;
28-
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
29-
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
28+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
29+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
30+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
3031
import org.junit.jupiter.api.Tag;
3132
import org.junit.jupiter.api.Test;
32-
import org.junitpioneer.jupiter.SetSystemProperty;
3333

3434
@Tag("async")
35-
@SetSystemProperty(key = Log4jPropertyKey.Constant.CONFIG_LOCATION, value = "AsyncLoggerConfigTest2.xml")
36-
public class AsyncLoggerConfigTest2 {
35+
@UsingStatusListener
36+
public class AsyncLoggerConfig2Test {
37+
38+
@TempLoggingDir
39+
private static Path loggingPath;
3740

3841
@Test
39-
public void testConsecutiveReconfigure() throws Exception {
40-
final File file = new File("target", "AsyncLoggerConfigTest2.log");
41-
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
42+
@LoggerContextSource
43+
public void testConsecutiveReconfigure(final LoggerContext ctx) throws Exception {
44+
final File file = loggingPath.resolve("AsyncLoggerConfigTest2.log").toFile();
4245

43-
final Logger log = LogManager.getLogger("com.foo.Bar");
46+
final Logger log = ctx.getLogger("com.foo.Bar");
4447
final String msg = "Message before reconfig";
4548
log.info(msg);
4649

47-
final LoggerContext ctx = LoggerContext.getContext(false);
4850
ctx.reconfigure();
4951
ctx.reconfigure();
5052

5153
final String msg2 = "Message after reconfig";
5254
log.info(msg2);
53-
CoreLoggerContexts.stopLoggerContext(file); // stop async thread
55+
ctx.stop(); // stop async thread
5456

5557
final BufferedReader reader = new BufferedReader(new FileReader(file));
5658
final String line1 = reader.readLine();

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigTest3.java renamed to log4j-async-logger/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfig3Test.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19+
import java.nio.file.Path;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122
import org.apache.logging.log4j.Level;
@@ -24,17 +25,19 @@
2425
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2526
import org.apache.logging.log4j.message.Message;
2627
import org.apache.logging.log4j.message.ParameterizedMessage;
27-
import org.apache.logging.log4j.test.junit.CleanUpFiles;
28+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
2829
import org.junit.jupiter.api.Tag;
2930
import org.junit.jupiter.api.Test;
3031

3132
@Tag("async")
3233
@Tag("sleepy")
33-
public class AsyncLoggerConfigTest3 {
34+
public class AsyncLoggerConfig3Test {
35+
36+
@TempLoggingDir
37+
private static Path loggingPath;
3438

3539
@Test
36-
@CleanUpFiles("target/AsyncLoggerConfigTest2.log")
37-
@LoggerContextSource("AsyncLoggerConfigTest2.xml")
40+
@LoggerContextSource
3841
public void testNoConcurrentModificationException(final Logger log) throws Exception {
3942
log.info("initial message");
4043
Thread.sleep(500);

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAutoFlushTest.java renamed to log4j-async-logger/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAutoFlushTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,30 @@
2020

2121
import java.io.File;
2222
import java.nio.file.Files;
23-
import org.apache.logging.log4j.LogManager;
23+
import java.nio.file.Path;
2424
import org.apache.logging.log4j.Logger;
25-
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
26-
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
27-
import org.apache.logging.log4j.test.junit.CleanUpFiles;
25+
import org.apache.logging.log4j.core.LoggerContext;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
2828
import org.junit.jupiter.api.Tag;
2929
import org.junit.jupiter.api.Test;
30-
import org.junitpioneer.jupiter.SetSystemProperty;
3130

3231
@Tag("async")
33-
@SetSystemProperty(key = Log4jPropertyKey.Constant.CONFIG_LOCATION, value = "AsyncLoggerConfigAutoFlushTest.xml")
3432
public class AsyncLoggerConfigAutoFlushTest {
3533

34+
@TempLoggingDir
35+
private static Path loggingPath;
36+
3637
@Test
37-
@CleanUpFiles("target/AsyncLoggerConfigAutoFlushTest.log")
38-
public void testFlushAtEndOfBatch() throws Exception {
39-
final File file = new File("target", "AsyncLoggerConfigAutoFlushTest.log");
40-
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
38+
@LoggerContextSource
39+
public void testFlushAtEndOfBatch(final LoggerContext ctx) throws Exception {
40+
final File file =
41+
loggingPath.resolve("AsyncLoggerConfigAutoFlushTest.log").toFile();
4142

42-
final Logger log = LogManager.getLogger("com.foo.Bar");
43+
final Logger log = ctx.getLogger("com.foo.Bar");
4344
final String msg = "Message flushed with immediate flush=false";
4445
log.info(msg);
45-
CoreLoggerContexts.stopLoggerContext(file); // stop async thread
46+
ctx.stop(); // stop async thread
4647
final String contents = Files.readString(file.toPath());
4748
assertTrue(contents.contains(msg), "line1 correct");
4849
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigErrorOnFormat.java renamed to log4j-async-logger/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigErrorOnFormat.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,34 @@
2323

2424
import java.io.File;
2525
import java.nio.file.Files;
26+
import java.nio.file.Path;
2627
import java.util.List;
2728
import org.apache.logging.log4j.LogManager;
2829
import org.apache.logging.log4j.Logger;
2930
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
3031
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
32+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
3133
import org.apache.logging.log4j.message.AsynchronouslyFormattable;
3234
import org.apache.logging.log4j.message.Message;
33-
import org.apache.logging.log4j.test.junit.CleanUpFiles;
35+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
3436
import org.junit.jupiter.api.Tag;
3537
import org.junit.jupiter.api.Test;
3638
import org.junitpioneer.jupiter.SetSystemProperty;
3739

3840
@Tag("async")
39-
@SetSystemProperty(key = Log4jPropertyKey.Constant.CONFIG_LOCATION, value = "AsyncLoggerConfigErrorOnFormat.xml")
4041
@SetSystemProperty(
4142
key = Log4jPropertyKey.Constant.LOG_EVENT_FACTORY_CLASS_NAME,
4243
value = "org.apache.logging.log4j.core.impl.DefaultLogEventFactory")
4344
public class AsyncLoggerConfigErrorOnFormat {
4445

46+
@TempLoggingDir
47+
private static Path loggingPath;
48+
4549
@Test
46-
@CleanUpFiles("target/AsyncLoggerConfigErrorOnFormat.log")
50+
@LoggerContextSource
4751
public void testError() throws Exception {
48-
final File file = new File("target", "AsyncLoggerConfigErrorOnFormat.log");
52+
final File file =
53+
loggingPath.resolve("AsyncLoggerConfigErrorOnFormat.log").toFile();
4954
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
5055

5156
final Logger log = LogManager.getLogger("com.foo.Bar");
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,31 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19+
import java.nio.file.Path;
1920
import org.apache.logging.log4j.Level;
20-
import org.apache.logging.log4j.LogManager;
2121
import org.apache.logging.log4j.Logger;
22-
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
23-
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
22+
import org.apache.logging.log4j.core.LoggerContext;
23+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2424
import org.apache.logging.log4j.message.SimpleMessage;
25-
import org.apache.logging.log4j.spi.AbstractLogger;
25+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
2626
import org.junit.jupiter.api.Tag;
2727
import org.junit.jupiter.api.Test;
28-
import org.junitpioneer.jupiter.SetSystemProperty;
2928

3029
@Tag("async")
31-
@SetSystemProperty(key = Log4jPropertyKey.Constant.CONFIG_LOCATION, value = "AsyncLoggerConfigTest.xml")
3230
public class AsyncLoggerConfigUseAfterShutdownTest {
3331

32+
@TempLoggingDir
33+
private static Path loggingPath;
34+
3435
@Test
35-
public void testNoErrorIfLogAfterShutdown() throws Exception {
36-
final Logger log = LogManager.getLogger("com.foo.Bar");
36+
@LoggerContextSource
37+
public void testNoErrorIfLogAfterShutdown(final LoggerContext ctx) throws Exception {
38+
final Logger log = ctx.getLogger("com.foo.Bar");
3739
log.info("some message");
38-
CoreLoggerContexts.stopLoggerContext(); // stop async thread
40+
ctx.stop(); // stop async thread
3941

4042
// call the #logMessage() method to bypass the isEnabled check:
4143
// before the LOG4J2-639 fix this would throw a NPE
42-
((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null);
44+
log.logMessage(Level.INFO, null, "com.foo.Bar", null, new SimpleMessage("msg"), null);
4345
}
4446
}

0 commit comments

Comments
 (0)