This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new 8e3e3b05c8 Try fixing `AsyncThreadContext*` tests
8e3e3b05c8 is described below
commit 8e3e3b05c80c27cf40f7a95ef9e37d88bfd413c4
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Tue Apr 16 15:20:45 2024 +0200
Try fixing `AsyncThreadContext*` tests
---
.../async/AbstractAsyncThreadContextTestBase.java | 34 ++++++++++++++++------
1 file changed, 25 insertions(+), 9 deletions(-)
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
index cab955f5e6..30802bffe0 100644
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
@@ -24,12 +24,14 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.ThreadContextTestAccess;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
@@ -62,7 +64,7 @@ public abstract class AbstractAsyncThreadContextTestBase {
props.setProperty("AsyncLoggerConfig.RingBufferSize", 128); // minimum
ringbuffer size
}
- enum Mode {
+ protected enum Mode {
ALL_ASYNC,
MIXED,
BOTH_ALL_ASYNC_AND_MIXED;
@@ -86,7 +88,7 @@ public abstract class AbstractAsyncThreadContextTestBase {
}
}
- enum ContextImpl {
+ protected enum ContextImpl {
WEBAPP,
GARBAGE_FREE,
COPY_ON_WRITE;
@@ -126,6 +128,24 @@ public abstract class AbstractAsyncThreadContextTestBase {
}
}
+ private LongSupplier remainingCapacity(final LoggerContext loggerContext,
final LoggerConfig loggerConfig) {
+ final LongSupplier contextSupplier;
+ if (loggerContext instanceof AsyncLoggerContext) {
+ final RingBufferAdmin ringBufferAdmin = ((AsyncLoggerContext)
loggerContext).createRingBufferAdmin();
+ contextSupplier = ringBufferAdmin::getRemainingCapacity;
+ } else {
+ contextSupplier = null;
+ }
+ if (loggerConfig instanceof AsyncLoggerConfig) {
+ final RingBufferAdmin ringBufferAdmin = ((AsyncLoggerConfig)
loggerConfig)
+
.createRingBufferAdmin(((org.apache.logging.log4j.core.LoggerContext)
loggerContext).getName());
+ return contextSupplier == null
+ ? ringBufferAdmin::getRemainingCapacity
+ : () -> Math.min(contextSupplier.getAsLong(),
ringBufferAdmin.getRemainingCapacity());
+ }
+ return contextSupplier != null ? contextSupplier : () ->
Long.MAX_VALUE;
+ }
+
protected void testAsyncLogWritesToLog(final ContextImpl contextImpl,
final Mode asyncMode, final Path loggingPath)
throws Exception {
final Path testLoggingPath = loggingPath.resolve(asyncMode.toString());
@@ -142,21 +162,17 @@ public abstract class AbstractAsyncThreadContextTestBase {
ThreadContext.put("KEY", "mapvalue");
final Logger log = LogManager.getLogger("com.foo.Bar");
+ final LoggerConfig loggerConfig =
((org.apache.logging.log4j.core.Logger) log).get();
final LoggerContext loggerContext = LogManager.getContext(false);
final String loggerContextName =
loggerContext.getClass().getSimpleName();
- RingBufferAdmin ring;
- if (loggerContext instanceof AsyncLoggerContext) {
- ring = ((AsyncLoggerContext)
loggerContext).createRingBufferAdmin();
- } else {
- ring = ((AsyncLoggerConfig)
((org.apache.logging.log4j.core.Logger) log).get()).createRingBufferAdmin("");
- }
+ final LongSupplier remainingCapacity =
remainingCapacity(loggerContext, loggerConfig);
for (int i = 0; i < LINE_COUNT; i++) {
// buffer may be full
if (i >= 128) {
waitAtMost(500, TimeUnit.MILLISECONDS)
.pollDelay(10, TimeUnit.MILLISECONDS)
- .until(() -> ring.getRemainingCapacity() > 0);
+ .until(() -> remainingCapacity.getAsLong() > 0);
}
if ((i & 1) == 1) {
ThreadContext.put("count", String.valueOf(i));