You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2020/08/29 23:13:58 UTC

[logging-log4j2] 02/02: Simplify concurrent test and improve Windows resiliency

This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit bccf597b67657f049ad1df0b7eef81fa00bbc52a
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sat Aug 29 18:13:28 2020 -0500

    Simplify concurrent test and improve Windows resiliency
    
    Signed-off-by: Matt Sicker <bo...@gmail.com>
---
 .../ConcurrentLoggingWithGelfLayoutTest.java       | 80 +++++++++-------------
 1 file changed, 32 insertions(+), 48 deletions(-)

diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java
index 7f16909..9bef502 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java
@@ -16,6 +16,13 @@
  */
 package org.apache.logging.log4j.core.layout;
 
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
 import java.io.IOException;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
@@ -23,20 +30,12 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.stream.Stream;
 
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.junit.LoggerContextSource;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.Tag;
-import org.junit.jupiter.api.Test;
-
-import static org.hamcrest.CoreMatchers.endsWith;
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.*;
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.MatcherAssert.assertThat;
 
 /**
  * Test for LOG4J2-1769, kind of.
@@ -44,24 +43,35 @@ import static org.hamcrest.MatcherAssert.*;
  * @since 2.8
  */
 @Tag("concurrency")
-@LoggerContextSource("log4j2-gelf-layout.xml")
 public class ConcurrentLoggingWithGelfLayoutTest {
     private static final Path PATH = Paths.get("target", "test-gelf-layout.log");
 
     @AfterAll
     static void after() throws IOException {
+        // on Windows, this will need to happen after the LoggerContext is stopped
         Files.deleteIfExists(PATH);
     }
 
     @Test
+    @LoggerContextSource("log4j2-gelf-layout.xml")
     public void testConcurrentLogging(final LoggerContext context) throws Throwable {
-        final Logger log = context.getLogger(ConcurrentLoggingWithGelfLayoutTest.class.getName());
-        final Set<Thread> threads = Collections.synchronizedSet(new HashSet<>());
+        final Logger log = context.getLogger(ConcurrentLoggingWithGelfLayoutTest.class);
+        final int threadCount = Runtime.getRuntime().availableProcessors() * 2;
+        final CountDownLatch latch = new CountDownLatch(threadCount);
         final List<Throwable> thrown = Collections.synchronizedList(new ArrayList<>());
 
-        for (int x = 0; x < Runtime.getRuntime().availableProcessors() * 2; x++) {
-            final Thread t = new LoggingThread(threads, log);
-            threads.add(t);
+        for (int x = 0; x < threadCount; x++) {
+            final Thread t = new Thread(() -> {
+                log.info(latch.getCount());
+                try {
+                    for (int i = 0; i < 64; i++) {
+                        log.info("First message.");
+                        log.info("Second message.");
+                    }
+                } finally {
+                    latch.countDown();
+                }
+            });
 
             // Appender is configured with ignoreExceptions="false";
             // any exceptions are propagated to the caller, so we can catch them here.
@@ -69,10 +79,7 @@ public class ConcurrentLoggingWithGelfLayoutTest {
             t.start();
         }
 
-        while (!threads.isEmpty()) {
-            log.info("not done going to sleep...");
-            Thread.sleep(10);
-        }
+        latch.await();
 
         // if any error occurred, fail this test
         if (!thrown.isEmpty()) {
@@ -81,34 +88,11 @@ public class ConcurrentLoggingWithGelfLayoutTest {
 
         // simple test to ensure content is not corrupted
         if (Files.exists(PATH)) {
-            final List<String> lines = Files.readAllLines(PATH, Charset.defaultCharset());
-            for (final String line : lines) {
-                assertThat(line, startsWith("{\"version\":\"1.1\",\"host\":\"myself\",\"timestamp\":"));
-                assertThat(line, endsWith("\"}"));
+            try (Stream<String> lines = Files.lines(PATH, Charset.defaultCharset())) {
+                lines.forEach(line -> assertThat(line,
+                        both(startsWith("{\"version\":\"1.1\",\"host\":\"myself\",\"timestamp\":")).and(endsWith("\"}"))));
             }
         }
     }
 
-    private static class LoggingThread extends Thread {
-        private final Set<Thread> threads;
-        private final Logger log;
-
-        LoggingThread(final Set<Thread> threads, final Logger log) {
-            this.threads = threads;
-            this.log = log;
-        }
-
-        @Override
-        public void run() {
-            log.info(threads.size());
-            try {
-                for (int i = 0; i < 64; i++) {
-                    log.info("First message.");
-                    log.info("Second message.");
-                }
-            } finally {
-                threads.remove(this);
-            }
-        }
-    }
 }