You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/09/22 12:11:07 UTC

[logging-log4j2] branch 2.x updated: Add diagnostics to GC-free tests

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 ebc9f3878c Add diagnostics to GC-free tests
ebc9f3878c is described below

commit ebc9f3878cb6b7dd169503862d08ec707d51d9df
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Fri Sep 22 14:08:18 2023 +0200

    Add diagnostics to GC-free tests
    
    These tests often fail on Windows.
---
 .../log4j/core/test/GcFreeLoggingTestUtil.java     | 45 +++++++---------------
 ...t.java => GcFreeMixedSyncAsyncLoggingTest.java} |  4 +-
 2 files changed, 15 insertions(+), 34 deletions(-)

diff --git a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/GcFreeLoggingTestUtil.java b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/GcFreeLoggingTestUtil.java
index 5d5a8b35a8..d00ad42845 100644
--- a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/GcFreeLoggingTestUtil.java
+++ b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/GcFreeLoggingTestUtil.java
@@ -23,6 +23,7 @@ import java.nio.file.Files;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.regex.Pattern;
+import java.util.stream.Stream;
 
 import com.google.monitoring.runtime.instrumentation.AllocationRecorder;
 import com.google.monitoring.runtime.instrumentation.Sampler;
@@ -34,13 +35,14 @@ import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.util.Constants;
 import org.apache.logging.log4j.message.StringMapMessage;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * Utility methods for the GC-free logging tests.
  */
-public enum GcFreeLoggingTestUtil {;
+public class GcFreeLoggingTestUtil {
 
     public static void executeLogging(final String configurationFile,
                                       final Class<?> testClass) throws Exception {
@@ -82,8 +84,7 @@ public enum GcFreeLoggingTestUtil {;
         // BlockingWaitStrategy uses ReentrantLock which allocates Node objects. Ignore this.
         final String[] exclude = new String[] {
                 "java/util/concurrent/locks/AbstractQueuedSynchronizer$Node", //
-                "com/google/monitoring/runtime/instrumentation/Sampler", //
-                "com/lmax/disruptor/InsufficientCapacityException"
+                "com/google/monitoring/runtime/instrumentation/Sampler"
         };
         final AtomicBoolean samplingEnabled = new AtomicBoolean(true);
         final Sampler sampler = (count, desc, newObj, size) -> {
@@ -225,35 +226,15 @@ public enum GcFreeLoggingTestUtil {;
         process.exitValue();
 
         final AtomicInteger lineCounter = new AtomicInteger(0);
-        Files.lines(tempFile.toPath(), Charset.defaultCharset()).forEach(line -> {
-
-            // Trim the line.
-            line = line.trim();
-
-            // Check the first line.
-            final int lineNumber = lineCounter.incrementAndGet();
-            if (lineNumber == 1) {
-                final String className = cls.getSimpleName();
-                final String firstLinePattern = String.format(
-                        "^FATAL .*\\.%s %s",
-                        className,
-                        Pattern.quote("[main] value1 {aKey=value1, " +
-                                "key2=value2, prop1=value1, prop2=value2} " +
-                                "This message is logged to the console"));
-                assertTrue(
-                        line.matches(firstLinePattern),
-                        "pattern mismatch at line 1: " + line);
-            }
-
-            // Check the rest of the lines.
-            else {
-                assertFalse(
-                        line.contains("allocated") || line.contains("array"),
-                        "(allocated|array) pattern matches at line " + lineNumber + ": " + line);
-            }
-
-        });
-
+        try (final Stream<String> lines = Files.lines(tempFile.toPath(), Charset.defaultCharset())) {
+            final Pattern pattern = Pattern.compile(String.format("^FATAL .*\\.%s [main].*",
+                    Pattern.quote(cls.getSimpleName())));
+            assertThat(lines.flatMap(l -> {
+                final int lineNumber = lineCounter.incrementAndGet();
+                final String line = l.trim();
+                return pattern.matcher(line).matches() ? Stream.of(lineNumber + ": " + line) : Stream.empty();
+            })).isEmpty();
+        }
     }
 
     private static File agentJar() {
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/GcFreeMixedSyncAyncLoggingTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/GcFreeMixedSyncAsyncLoggingTest.java
similarity index 94%
rename from log4j-core-test/src/test/java/org/apache/logging/log4j/core/GcFreeMixedSyncAyncLoggingTest.java
rename to log4j-core-test/src/test/java/org/apache/logging/log4j/core/GcFreeMixedSyncAsyncLoggingTest.java
index ad8e866da7..e74d5553e8 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/GcFreeMixedSyncAyncLoggingTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/GcFreeMixedSyncAsyncLoggingTest.java
@@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test;
  */
 @Tag("allocation")
 @Tag("functional")
-public class GcFreeMixedSyncAyncLoggingTest {
+public class GcFreeMixedSyncAsyncLoggingTest {
 
     @Test
     public void testNoAllocationDuringSteadyStateLogging() throws Throwable {
@@ -40,6 +40,6 @@ public class GcFreeMixedSyncAyncLoggingTest {
     public static void main(final String[] args) throws Exception {
         System.setProperty("log4j2.garbagefree.threadContextMap", "true");
         System.setProperty("AsyncLoggerConfig.RingBufferSize", "128"); // minimum ringbuffer size
-        GcFreeLoggingTestUtil.executeLogging("gcFreeMixedSyncAsyncLogging.xml", GcFreeMixedSyncAyncLoggingTest.class);
+        GcFreeLoggingTestUtil.executeLogging("gcFreeMixedSyncAsyncLogging.xml", GcFreeMixedSyncAsyncLoggingTest.class);
     }
 }