You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/09/09 16:35:27 UTC

[1/2] logging-log4j2 git commit: LOG4J2-1349 fixed test and extended for AsyncLoggerConfig

Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-1349-gcfree-threadcontext 467142097 -> e8a25a97c


LOG4J2-1349 fixed test and extended for AsyncLoggerConfig


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/25615a46
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/25615a46
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/25615a46

Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext
Commit: 25615a468d0c267e20d21721f7f71a4874a12ee9
Parents: 4671420
Author: rpopma <rp...@apache.org>
Authored: Fri Sep 9 22:06:15 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Fri Sep 9 22:06:15 2016 +0900

----------------------------------------------------------------------
 ...ggerAllThreadContextImplementationsTest.java |  34 ++--
 ...nfigAllThreadContextImplementationsTest.java | 158 +++++++++++++++++++
 2 files changed, 177 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/25615a46/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
index 2bbee67..6cd89f4 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
@@ -30,8 +30,10 @@ import org.apache.logging.log4j.ThreadContextAccess;
 import org.apache.logging.log4j.ThreadContextTestAccess;
 import org.apache.logging.log4j.core.CoreLoggerContexts;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
 import org.apache.logging.log4j.core.util.Constants;
 import org.apache.logging.log4j.spi.DefaultThreadContextMap;
+import org.apache.logging.log4j.spi.LoggerContext;
 import org.apache.logging.log4j.util.Unbox;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -66,10 +68,10 @@ public class AsyncLoggerAllThreadContextImplementationsTest {
     }
 
     static enum Mode {
-        ALL_ASYNC, MIXED, BOTH;
+        ALL_ASYNC, MIXED, BOTH_ALL_ASYNC_AND_MIXED;
 
         void initSelector() {
-            if (this == ALL_ASYNC || this == BOTH) {
+            if (this == ALL_ASYNC || this == BOTH_ALL_ASYNC_AND_MIXED) {
                 System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR,  AsyncLoggerContextSelector.class.getName());
             } else {
                 System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR);
@@ -110,14 +112,11 @@ public class AsyncLoggerAllThreadContextImplementationsTest {
     public static Collection<Object[]> data() {
         return Arrays.asList(new Object[][] {
                 { ContextImpl.WEBAPP, Mode.ALL_ASYNC},
-                { ContextImpl.WEBAPP, Mode.MIXED },
-                { ContextImpl.WEBAPP, Mode.BOTH },
+                { ContextImpl.WEBAPP, Mode.BOTH_ALL_ASYNC_AND_MIXED},
                 { ContextImpl.GARBAGE_FREE, Mode.ALL_ASYNC},
-                { ContextImpl.GARBAGE_FREE, Mode.MIXED},
-                { ContextImpl.GARBAGE_FREE, Mode.BOTH},
+                { ContextImpl.GARBAGE_FREE, Mode.BOTH_ALL_ASYNC_AND_MIXED},
                 { ContextImpl.COPY_ON_WRITE, Mode.ALL_ASYNC},
-                { ContextImpl.COPY_ON_WRITE, Mode.MIXED},
-                { ContextImpl.COPY_ON_WRITE, Mode.BOTH}
+                { ContextImpl.COPY_ON_WRITE, Mode.BOTH_ALL_ASYNC_AND_MIXED}
         });
     }
 
@@ -144,28 +143,33 @@ public class AsyncLoggerAllThreadContextImplementationsTest {
         ThreadContext.put("KEY", "mapvalue");
 
         final Logger log = LogManager.getLogger("com.foo.Bar");
+        final LoggerContext loggerContext = LogManager.getContext(false);
+        final String loggerContextName = loggerContext.getClass().getSimpleName();
+        final RingBufferAdmin ring = ((AsyncLoggerContext) loggerContext).createRingBufferAdmin();
         for (int i = 0; i < LINE_COUNT; i++) {
+            while (i >= 128 && ring.getRemainingCapacity() == 0) { // buffer may be full
+                Thread.sleep(1);
+            }
             if ((i & 1) == 1) {
                 ThreadContext.put("count", String.valueOf(i));
             } else {
                 ThreadContext.remove("count");
             }
             final String contextmap = ThreadContextAccess.getThreadContextMap().getClass().getSimpleName();
-            log.info("{} {} i={}", contextImpl, contextmap, Unbox.box(i));
+            log.info("{} {} {} i={}", contextImpl, contextmap, loggerContextName, Unbox.box(i));
         }
         ThreadContext.pop();
         CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread
 
-        checkResult(file);
-        if (asyncMode == Mode.MIXED || asyncMode == Mode.BOTH) {
-            checkResult(sync);
+        checkResult(file, loggerContextName);
+        if (asyncMode == Mode.MIXED || asyncMode == Mode.BOTH_ALL_ASYNC_AND_MIXED) {
+            checkResult(sync, loggerContextName);
         }
         LogManager.shutdown();
     }
 
-    private void checkResult(final File file) throws IOException {
-        final String contextDesc = contextImpl + " " + contextImpl.implClassSimpleName() + " "
-                + LogManager.getContext(false).getClass().getSimpleName();
+    private void checkResult(final File file, final String loggerContextName) throws IOException {
+        final String contextDesc = contextImpl + " " + contextImpl.implClassSimpleName() + " " + loggerContextName;
         try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
             String expect = null;
             for (int i = 0; i < LINE_COUNT; i++) {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/25615a46/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java
new file mode 100644
index 0000000..74de230
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java
@@ -0,0 +1,158 @@
+/*
+ * 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 java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.ThreadContextAccess;
+import org.apache.logging.log4j.ThreadContextTestAccess;
+import org.apache.logging.log4j.core.CoreLoggerContexts;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
+import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.spi.DefaultThreadContextMap;
+import org.apache.logging.log4j.spi.LoggerContext;
+import org.apache.logging.log4j.util.Unbox;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.junit.Assert.*;
+
+@RunWith(Parameterized.class)
+public class AsyncLoggerConfigAllThreadContextImplementationsTest {
+
+    final static int LINE_COUNT = 130;
+    private ContextImpl contextImpl;
+
+    @BeforeClass
+    public static void beforeClass() {
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigThreadContextTest.xml");
+        System.setProperty("AsyncLoggerConfig.RingBufferSize", "128"); // minimum ringbuffer size
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        System.clearProperty("AsyncLogger.RingBufferSize");
+        System.clearProperty("AsyncLoggerConfig.RingBufferSize");
+        System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR);
+        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+        System.clearProperty("log4j2.garbagefree.threadContextMap");
+        System.clearProperty("log4j2.is.webapp");
+        System.clearProperty("log4j2.threadContextMap");
+    }
+
+    static enum ContextImpl {
+        WEBAPP, GARBAGE_FREE, COPY_ON_WRITE;
+
+        void init() {
+            System.clearProperty("log4j2.threadContextMap");
+            final String PACKAGE = "org.apache.logging.log4j.spi.";
+            System.setProperty("log4j2.threadContextMap", PACKAGE + implClassSimpleName());
+            ThreadContextTestAccess.init();
+        }
+
+        public String implClassSimpleName() {
+            switch (this) {
+                case WEBAPP:
+                    return DefaultThreadContextMap.class.getSimpleName();
+                case GARBAGE_FREE:
+                    return "GarbageFreeSortedArrayThreadContextMap";
+                case COPY_ON_WRITE:
+                    return "CopyOnWriteSortedArrayThreadContextMap";
+            }
+            throw new IllegalStateException("Unknown state " + this);
+        }
+    }
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+                { ContextImpl.WEBAPP, },
+                { ContextImpl.GARBAGE_FREE, },
+                { ContextImpl.COPY_ON_WRITE, }
+        });
+    }
+
+    public AsyncLoggerConfigAllThreadContextImplementationsTest(ContextImpl contextImpl) {
+        this.contextImpl = contextImpl;
+        contextImpl.init();
+    }
+
+    @Test
+    public void testAsyncLogWritesToLog() throws Exception {
+        final File file = new File("target", "SynchronousContextTest.log");
+        file.delete();
+
+        ThreadContext.push("stackvalue");
+        ThreadContext.put("KEY", "mapvalue");
+
+        final Logger log = LogManager.getLogger("com.foo.Bar");
+        final LoggerContext loggerContext = LogManager.getContext(false);
+        final String loggerContextName = loggerContext.getClass().getSimpleName();
+        final RingBufferAdmin ring = ((AsyncLoggerConfig) ((org.apache.logging.log4j.core.Logger) log).get())
+                .createRingBufferAdmin("");
+        for (int i = 0; i < LINE_COUNT; i++) {
+            while (i >= 128 && ring.getRemainingCapacity() == 0) { // buffer may be full
+                Thread.sleep(1);
+            }
+            if ((i & 1) == 1) {
+                ThreadContext.put("count", String.valueOf(i));
+            } else {
+                ThreadContext.remove("count");
+            }
+            final String contextmap = ThreadContextAccess.getThreadContextMap().getClass().getSimpleName();
+            log.info("{} {} {} i={}", contextImpl, contextmap, loggerContextName, Unbox.box(i));
+        }
+        ThreadContext.pop();
+        CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread
+
+        checkResult(file, loggerContextName);
+        LogManager.shutdown();
+    }
+
+    private void checkResult(final File file, final String loggerContextName) throws IOException {
+        final String contextDesc = contextImpl + " " + contextImpl.implClassSimpleName() + " " + loggerContextName;
+        try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
+            String expect = null;
+            for (int i = 0; i < LINE_COUNT; i++) {
+                String line = reader.readLine();
+                if ((i & 1) == 1) {
+                    expect = "INFO c.f.Bar mapvalue [stackvalue] {KEY=mapvalue, count=" + i + "} "
+                            + contextDesc + " i=" + i;
+                } else {
+                    expect = "INFO c.f.Bar mapvalue [stackvalue] {KEY=mapvalue} " + contextDesc + " i=" + i;
+                }
+                assertEquals("line " + i, expect, line);
+            }
+            final String noMoreLines = reader.readLine();
+            assertNull("done", noMoreLines);
+        } finally {
+            file.delete();
+        }
+    }
+}


[2/2] logging-log4j2 git commit: LOG4J2-1349 extend context data injector tests to cover all combinations default threadcontextmap, copyOnWriteThreadContextData and garbage-free threadcontextdata with all combinations of synchronous, async appender, asyn

Posted by rp...@apache.org.
LOG4J2-1349 extend context data injector tests to cover all combinations default threadcontextmap, copyOnWriteThreadContextData and garbage-free threadcontextdata with all combinations of synchronous, async appender, async logger (pure), async logger (mixed), asyncAppender + asyncLogger(pure), asyncAppender+asyncLogger(mixed) and asyncAppender+asyncLogger(pure)+asyncLogger(mixed)


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/e8a25a97
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/e8a25a97
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/e8a25a97

Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext
Commit: e8a25a97cb0c1746383c11f7d9b4b5db81ebf9c7
Parents: 25615a4
Author: rpopma <rp...@apache.org>
Authored: Sat Sep 10 01:35:27 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sat Sep 10 01:35:27 2016 +0900

----------------------------------------------------------------------
 ...ggerAllThreadContextImplementationsTest.java | 25 ++++++++++++--------
 ...nfigAllThreadContextImplementationsTest.java | 19 +++++++++++----
 .../AsyncLoggerConfigThreadContextTest.xml      | 20 ++++++++++++++++
 3 files changed, 49 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e8a25a97/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
index 6cd89f4..4283cbb 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerAllThreadContextImplementationsTest.java
@@ -132,12 +132,15 @@ public class AsyncLoggerAllThreadContextImplementationsTest {
 
     @Test
     public void testAsyncLogWritesToLog() throws Exception {
-        final File file = new File("target", "AsyncLoggerTest.log");
-        // System.out.println(f.getAbsolutePath());
-        file.delete();
-
-        final File sync = new File("target", "SynchronousContextTest.log");
-        sync.delete();
+        final File[] files = new File[] {
+                new File("target", "AsyncLoggerTest.log"), //
+                new File("target", "SynchronousContextTest.log"), //
+                new File("target", "AsyncLoggerAndAsyncAppenderTest.log"), //
+                new File("target", "AsyncAppenderContextTest.log"), //
+        };
+        for (final File f : files) {
+            f.delete();
+        }
 
         ThreadContext.push("stackvalue");
         ThreadContext.put("KEY", "mapvalue");
@@ -159,11 +162,13 @@ public class AsyncLoggerAllThreadContextImplementationsTest {
             log.info("{} {} {} i={}", contextImpl, contextmap, loggerContextName, Unbox.box(i));
         }
         ThreadContext.pop();
-        CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread
+        CoreLoggerContexts.stopLoggerContext(false, files[0]); // stop async thread
 
-        checkResult(file, loggerContextName);
+        checkResult(files[0], loggerContextName);
         if (asyncMode == Mode.MIXED || asyncMode == Mode.BOTH_ALL_ASYNC_AND_MIXED) {
-            checkResult(sync, loggerContextName);
+            for (int i = 1; i < files.length; i++) {
+                checkResult(files[i], loggerContextName);
+            }
         }
         LogManager.shutdown();
     }
@@ -180,7 +185,7 @@ public class AsyncLoggerAllThreadContextImplementationsTest {
                 } else {
                     expect = "INFO c.f.Bar mapvalue [stackvalue] {KEY=mapvalue} " + contextDesc + " i=" + i;
                 }
-                assertEquals("line " + i, expect, line);
+                assertEquals(file.getName() + ": line " + i, expect, line);
             }
             final String noMoreLines = reader.readLine();
             assertNull("done", noMoreLines);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e8a25a97/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java
index 74de230..3c3d69c 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAllThreadContextImplementationsTest.java
@@ -105,8 +105,15 @@ public class AsyncLoggerConfigAllThreadContextImplementationsTest {
 
     @Test
     public void testAsyncLogWritesToLog() throws Exception {
-        final File file = new File("target", "SynchronousContextTest.log");
-        file.delete();
+        final File[] files = new File[] {
+                new File("target", "AsyncLoggerTest.log"), //
+                new File("target", "SynchronousContextTest.log"), //
+                new File("target", "AsyncLoggerAndAsyncAppenderTest.log"), //
+                new File("target", "AsyncAppenderContextTest.log"), //
+        };
+        for (final File f : files) {
+            f.delete();
+        }
 
         ThreadContext.push("stackvalue");
         ThreadContext.put("KEY", "mapvalue");
@@ -129,9 +136,11 @@ public class AsyncLoggerConfigAllThreadContextImplementationsTest {
             log.info("{} {} {} i={}", contextImpl, contextmap, loggerContextName, Unbox.box(i));
         }
         ThreadContext.pop();
-        CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread
+        CoreLoggerContexts.stopLoggerContext(false, files[files.length - 1]); // stop async thread
 
-        checkResult(file, loggerContextName);
+        for (final File f : files) {
+            checkResult(f, loggerContextName);
+        }
         LogManager.shutdown();
     }
 
@@ -147,7 +156,7 @@ public class AsyncLoggerConfigAllThreadContextImplementationsTest {
                 } else {
                     expect = "INFO c.f.Bar mapvalue [stackvalue] {KEY=mapvalue} " + contextDesc + " i=" + i;
                 }
-                assertEquals("line " + i, expect, line);
+                assertEquals(file.getName() + ": line " + i, expect, line);
             }
             final String noMoreLines = reader.readLine();
             assertNull("done", noMoreLines);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e8a25a97/log4j-core/src/test/resources/AsyncLoggerConfigThreadContextTest.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/AsyncLoggerConfigThreadContextTest.xml b/log4j-core/src/test/resources/AsyncLoggerConfigThreadContextTest.xml
index 5419e20..1ce6faa 100644
--- a/log4j-core/src/test/resources/AsyncLoggerConfigThreadContextTest.xml
+++ b/log4j-core/src/test/resources/AsyncLoggerConfigThreadContextTest.xml
@@ -4,6 +4,24 @@
     <Property name="configProp">configValue</Property>
   </Properties>
   <Appenders>
+    <RandomAccessFile name="AsyncLoggerAndAsyncAppenderFile" fileName="target/AsyncLoggerAndAsyncAppenderTest.log"
+        immediateFlush="false" append="false">
+      <PatternLayout>
+        <Pattern>%p %c{1.} %X{KEY} %x %X %m%ex%n</Pattern>
+      </PatternLayout>
+    </RandomAccessFile>
+    <Async name="AsyncLoggerAndAsyncAppender">
+      <AppenderRef ref="AsyncLoggerAndAsyncAppenderFile" />
+    </Async>
+    <RandomAccessFile name="AsyncAppenderFile" fileName="target/AsyncAppenderContextTest.log"
+        immediateFlush="false" append="false">
+      <PatternLayout>
+        <Pattern>%p %c{1.} %X{KEY} %x %X %m%ex%n</Pattern>
+      </PatternLayout>
+    </RandomAccessFile>
+    <Async name="AsyncAppender">
+      <AppenderRef ref="AsyncAppenderFile" />
+    </Async>
     <RandomAccessFile name="RandomAccessFile" fileName="target/AsyncLoggerTest.log"
         immediateFlush="false" append="false">
       <PatternLayout>
@@ -21,9 +39,11 @@
   <Loggers>
     <AsyncLogger name="com.foo" level="info" includeLocation="false" additivity="true">
       <AppenderRef ref="RandomAccessFile"/>
+      <AppenderRef ref="AsyncLoggerAndAsyncAppender"/>
     </AsyncLogger>
     <Root level="info" includeLocation="false">
       <AppenderRef ref="SynchronousRandomAccessFile"/>
+      <AppenderRef ref="AsyncAppender"/>
     </Root>
   </Loggers>
 </Configuration>
\ No newline at end of file