Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LOG4J2-3472 (master) make disruptor WaitStrategy configurable in Log4…
…j configuration (#830) LOG4J2-3472 (master) make disruptor WaitStrategy configurable in Log4j configuration (#830)
- Loading branch information
Showing
21 changed files
with
647 additions
and
60 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
.../org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfigGlobalLoggersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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 com.lmax.disruptor.YieldingWaitStrategy; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.core.test.categories.AsyncLoggers; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.config.ConfigurationFactory; | ||
import org.apache.logging.log4j.core.util.Constants; | ||
import org.apache.logging.log4j.util.Strings; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@Category(AsyncLoggers.class) | ||
public class AsyncWaitStrategyFactoryConfigGlobalLoggersTest { | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, | ||
AsyncLoggerContextSelector.class.getName()); | ||
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, | ||
"AsyncWaitStrategyFactoryConfigGlobalLoggerTest.xml"); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() { | ||
System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR); | ||
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); | ||
} | ||
|
||
@Ignore("This test succeeds when run individually but fails when run by Surefire with all other tests") | ||
@Test | ||
public void testConfigWaitStrategyAndFactory() throws Exception { | ||
final AsyncLogger logger = (AsyncLogger) LogManager.getLogger("com.foo.Bar"); | ||
|
||
final LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
assertTrue("context is AsyncLoggerContext", context instanceof AsyncLoggerContext); | ||
|
||
AsyncWaitStrategyFactory asyncWaitStrategyFactory = context.getConfiguration().getAsyncWaitStrategyFactory(); | ||
assertEquals(AsyncWaitStrategyFactoryConfigTest.YieldingWaitStrategyFactory.class, asyncWaitStrategyFactory.getClass()); | ||
assertTrue("factory is YieldingWaitStrategyFactory", asyncWaitStrategyFactory instanceof AsyncWaitStrategyFactoryConfigTest.YieldingWaitStrategyFactory); | ||
|
||
AsyncLoggerDisruptor delegate = logger.getAsyncLoggerDisruptor(); | ||
|
||
assertEquals(YieldingWaitStrategy.class, delegate.getWaitStrategy().getClass()); | ||
assertTrue("waitstrategy is YieldingWaitStrategy", delegate.getWaitStrategy() instanceof YieldingWaitStrategy); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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 com.lmax.disruptor.TimeoutBlockingWaitStrategy; | ||
import com.lmax.disruptor.WaitStrategy; | ||
import com.lmax.disruptor.YieldingWaitStrategy; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.test.categories.AsyncLoggers; | ||
import org.apache.logging.log4j.core.test.junit.LoggerContextSource; | ||
import org.apache.logging.log4j.core.test.junit.Named; | ||
import org.apache.logging.log4j.core.test.appender.ListAppender; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@Tag("async") | ||
public class AsyncWaitStrategyFactoryConfigTest { | ||
|
||
@Test | ||
@LoggerContextSource("AsyncWaitStrategyFactoryConfigTest.xml") | ||
public void testConfigWaitStrategyFactory(final LoggerContext context) throws Exception { | ||
AsyncWaitStrategyFactory asyncWaitStrategyFactory = context.getConfiguration().getAsyncWaitStrategyFactory(); | ||
assertThat(asyncWaitStrategyFactory.getClass()).isEqualTo(YieldingWaitStrategyFactory.class); | ||
assertThat(asyncWaitStrategyFactory instanceof YieldingWaitStrategyFactory); //"factory is YieldingWaitStrategyFactory" | ||
} | ||
|
||
@Test | ||
@LoggerContextSource("AsyncWaitStrategyFactoryConfigTest.xml") | ||
public void testWaitStrategy(final LoggerContext context) throws Exception { | ||
|
||
org.apache.logging.log4j.Logger logger = context.getRootLogger(); | ||
|
||
AsyncLoggerConfig loggerConfig = (AsyncLoggerConfig) ((org.apache.logging.log4j.core.Logger) logger).get(); | ||
AsyncLoggerConfigDisruptor delegate = (AsyncLoggerConfigDisruptor) loggerConfig.getAsyncLoggerConfigDelegate(); | ||
assertThat(delegate.getWaitStrategy().getClass()).isEqualTo(YieldingWaitStrategy.class); | ||
assertThat(delegate.getWaitStrategy() instanceof com.lmax.disruptor.YieldingWaitStrategy);// "waitstrategy is YieldingWaitStrategy"); | ||
} | ||
|
||
@Test | ||
@LoggerContextSource("AsyncWaitStrategyIncorrectFactoryConfigTest.xml") | ||
public void testIncorrectConfigWaitStrategyFactory(final LoggerContext context) throws Exception { | ||
AsyncWaitStrategyFactory asyncWaitStrategyFactory = context.getConfiguration().getAsyncWaitStrategyFactory(); | ||
assertThat(asyncWaitStrategyFactory).isNull(); // because invalid configuration | ||
} | ||
|
||
@Test | ||
@LoggerContextSource("AsyncWaitStrategyIncorrectFactoryConfigTest.xml") | ||
public void testIncorrectWaitStrategyFallsBackToDefault( | ||
@Named("WaitStrategyAppenderList") final ListAppender list1, | ||
final LoggerContext context) throws Exception { | ||
org.apache.logging.log4j.Logger logger = context.getRootLogger(); | ||
|
||
AsyncLoggerConfig loggerConfig = (AsyncLoggerConfig) ((org.apache.logging.log4j.core.Logger) logger).get(); | ||
AsyncLoggerConfigDisruptor delegate = (AsyncLoggerConfigDisruptor) loggerConfig.getAsyncLoggerConfigDelegate(); | ||
assertThat(delegate.getWaitStrategy().getClass()).isEqualTo(TimeoutBlockingWaitStrategy.class); | ||
assertThat(delegate.getWaitStrategy() instanceof TimeoutBlockingWaitStrategy); //"waitstrategy is TimeoutBlockingWaitStrategy" | ||
} | ||
|
||
public static class YieldingWaitStrategyFactory implements AsyncWaitStrategyFactory { | ||
@Override | ||
public WaitStrategy createWaitStrategy() { | ||
return new YieldingWaitStrategy(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...he/logging/log4j/core/async/AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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 com.lmax.disruptor.TimeoutBlockingWaitStrategy; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.core.test.categories.AsyncLoggers; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.config.ConfigurationFactory; | ||
import org.apache.logging.log4j.core.util.Constants; | ||
import org.apache.logging.log4j.util.Strings; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@Category(AsyncLoggers.class) | ||
public class AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggersTest { | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, | ||
AsyncLoggerContextSelector.class.getName()); | ||
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, | ||
"AsyncWaitStrategyIncorrectFactoryConfigGlobalLoggerTest.xml"); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() { | ||
System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR); | ||
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); | ||
} | ||
|
||
|
||
@Test | ||
public void testIncorrectConfigWaitStrategyFactory() throws Exception { | ||
final LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
assertTrue("context is AsyncLoggerContext", context instanceof AsyncLoggerContext); | ||
|
||
AsyncWaitStrategyFactory asyncWaitStrategyFactory = context.getConfiguration().getAsyncWaitStrategyFactory(); | ||
assertNull(asyncWaitStrategyFactory); | ||
|
||
AsyncLogger logger = (AsyncLogger) context.getRootLogger(); | ||
AsyncLoggerDisruptor delegate = logger.getAsyncLoggerDisruptor(); | ||
assertEquals(TimeoutBlockingWaitStrategy.class, delegate.getWaitStrategy().getClass()); | ||
assertTrue("waitstrategy is TimeoutBlockingWaitStrategy", delegate.getWaitStrategy() instanceof TimeoutBlockingWaitStrategy); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
log4j-core-test/src/test/resources/AsyncWaitStrategyFactoryConfigGlobalLoggerTest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration name="asyncwait-global1" > | ||
<!-- packages="org.apache.logging.log4j.core.async" --> | ||
|
||
<AsyncWaitStrategyFactory | ||
class="org.apache.logging.log4j.core.async.AsyncWaitStrategyFactoryConfigTest$YieldingWaitStrategyFactory" /> | ||
|
||
<Appenders> | ||
<List name="WaitStrategyAppenderList" /> | ||
</Appenders> | ||
|
||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="WaitStrategyAppenderList"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
17 changes: 17 additions & 0 deletions
17
log4j-core-test/src/test/resources/AsyncWaitStrategyFactoryConfigTest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration name="async-wait-1" > | ||
<!-- packages="org.apache.logging.log4j.core.async" --> | ||
|
||
<AsyncWaitStrategyFactory | ||
class="org.apache.logging.log4j.core.async.AsyncWaitStrategyFactoryConfigTest$YieldingWaitStrategyFactory" /> | ||
|
||
<Appenders> | ||
<List name="WaitStrategyAppenderList" /> | ||
</Appenders> | ||
|
||
<Loggers> | ||
<AsyncRoot level="info"> | ||
<AppenderRef ref="WaitStrategyAppenderList"/> | ||
</AsyncRoot> | ||
</Loggers> | ||
</Configuration> |
16 changes: 16 additions & 0 deletions
16
...-core-test/src/test/resources/AsyncWaitStrategyIncorrectFactoryConfigGlobalLoggerTest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration name="asyncwait-global2"> | ||
|
||
<AsyncWaitStrategyFactory | ||
class="nonexisting.Factory" /> | ||
|
||
<Appenders> | ||
<List name="WaitStrategyAppenderList" /> | ||
</Appenders> | ||
|
||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="WaitStrategyAppenderList"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
16 changes: 16 additions & 0 deletions
16
log4j-core-test/src/test/resources/AsyncWaitStrategyIncorrectFactoryConfigTest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration name="async-wait-2"> | ||
|
||
<AsyncWaitStrategyFactory | ||
class="nonexisting.Factory" /> | ||
|
||
<Appenders> | ||
<List name="WaitStrategyAppenderList" /> | ||
</Appenders> | ||
|
||
<Loggers> | ||
<AsyncRoot level="info"> | ||
<AppenderRef ref="WaitStrategyAppenderList"/> | ||
</AsyncRoot> | ||
</Loggers> | ||
</Configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.