You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2021/05/27 08:21:47 UTC

[logging-log4j2] branch master updated: LOG4J2-3089 Fix sporadic JsonTemplateLayoutNullEventDelimiterTest failures on Windows.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ed1b5e6  LOG4J2-3089 Fix sporadic JsonTemplateLayoutNullEventDelimiterTest failures on Windows.
ed1b5e6 is described below

commit ed1b5e691e2a7d52d87bdc02d53a7e153100938d
Author: Volkan Yazıcı <vo...@gmail.com>
AuthorDate: Thu May 27 10:20:53 2021 +0200

    LOG4J2-3089 Fix sporadic JsonTemplateLayoutNullEventDelimiterTest failures on Windows.
---
 .../JsonTemplateLayoutNullEventDelimiterTest.java  | 56 ++++++++--------------
 ...nullEventDelimitedJsonTemplateLayoutLogging.xml |  2 +-
 src/changes/changes.xml                            |  3 ++
 3 files changed, 24 insertions(+), 37 deletions(-)

diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutNullEventDelimiterTest.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutNullEventDelimiterTest.java
index d2e02a5..f4cfe84 100644
--- a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutNullEventDelimiterTest.java
+++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutNullEventDelimiterTest.java
@@ -23,44 +23,41 @@ import org.assertj.core.api.Assertions;
 import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 
-import java.io.BufferedWriter;
 import java.io.ByteArrayOutputStream;
 import java.io.EOFException;
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStreamWriter;
-import java.nio.file.Files;
-import java.nio.charset.StandardCharsets;
-import java.net.BindException;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.time.Duration;
 
-public class JsonTemplateLayoutNullEventDelimiterTest {
+class JsonTemplateLayoutNullEventDelimiterTest {
+
     @Test
-    public void test() throws Exception {
-        final File tempConfig = File.createTempFile("nullEventDelimitedJsonTemplateLayoutLogging", ".xml");
-        // Set the expected bytes.
-        final byte[] expectedBytes = {
-                '"', 'f', 'o', 'o', '"', '\0',
-                '"', 'b', 'a', 'r', '"', '\0'
-        };
+    void test() throws Exception {
 
         // Start the TCP server.
         try (final TcpServer server = new TcpServer(0)) {
-            makeTempConfig(server.getLocalPort(), tempConfig);
+
+            // Set the configuration.
+            System.setProperty(
+                    "serverPort",
+                    String.valueOf(server.getPort()));
             System.setProperty(
                     "log4j.configurationFile",
-                    tempConfig.getAbsolutePath());
-
+                    "nullEventDelimitedJsonTemplateLayoutLogging.xml");
 
             // Produce log events.
             final Logger logger = LogManager.getLogger(JsonTemplateLayoutNullEventDelimiterTest.class);
             logger.log(Level.INFO, "foo");
             logger.log(Level.INFO, "bar");
 
+            // Set the expected bytes.
+            final byte[] expectedBytes = {
+                    '"', 'f', 'o', 'o', '"', '\0',
+                    '"', 'b', 'a', 'r', '"', '\0'
+            };
+
             // Wait for the log events.
             Awaitility
                     .await()
@@ -71,22 +68,9 @@ public class JsonTemplateLayoutNullEventDelimiterTest {
             // Verify the received log events.
             final byte[] actualBytes = server.getReceivedBytes();
             Assertions.assertThat(actualBytes).startsWith(expectedBytes);
-        } finally {
-            tempConfig.delete();
-        }
-
-    }
 
-    public void makeTempConfig(final int serverPort, final File tempConfigFile) throws IOException {
-        try (final FileOutputStream fos = new FileOutputStream(tempConfigFile);
-                final OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
-                final BufferedWriter bw = new BufferedWriter(osw);) {
-            final File xmlTemplate = new File("src/test/resources/nullEventDelimitedJsonTemplateLayoutLogging.xml");
-            for (final String line : Files.readAllLines(xmlTemplate.toPath())) {
-                bw.write(line.replace("TARGET_PORT_NUMBER", Integer.toString(serverPort)));
-                bw.newLine();
-            }
         }
+
     }
 
     private static final class TcpServer extends Thread implements AutoCloseable {
@@ -108,10 +92,6 @@ public class JsonTemplateLayoutNullEventDelimiterTest {
             start();
         }
 
-        public int getLocalPort() {
-            return serverSocket.getLocalPort();
-        }
-
         @Override
         public void run() {
             try {
@@ -138,6 +118,10 @@ public class JsonTemplateLayoutNullEventDelimiterTest {
             }
         }
 
+        public int getPort() {
+            return serverSocket.getLocalPort();
+        }
+
         public synchronized byte[] getReceivedBytes() {
             return outputStream.toByteArray();
         }
diff --git a/log4j-layout-template-json/src/test/resources/nullEventDelimitedJsonTemplateLayoutLogging.xml b/log4j-layout-template-json/src/test/resources/nullEventDelimitedJsonTemplateLayoutLogging.xml
index f15bac2..9eff27a 100644
--- a/log4j-layout-template-json/src/test/resources/nullEventDelimitedJsonTemplateLayoutLogging.xml
+++ b/log4j-layout-template-json/src/test/resources/nullEventDelimitedJsonTemplateLayoutLogging.xml
@@ -19,7 +19,7 @@
   <Appenders>
     <Socket name="Socket"
             host="localhost"
-            port="TARGET_PORT_NUMBER"
+            port="${sys:serverPort}"
             protocol="TCP"
             ignoreExceptions="false"
             reconnectionDelay="100"
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ff0e8a6..98f5010 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -208,6 +208,9 @@
         Allow a PatternSelector to be specified on GelfLayout.
       </action>
       <!-- FIXES -->
+      <action issue="LOG4J2-3089" dev="vy" type="fix" due-to="Tim Perry">
+        Fix sporadic JsonTemplateLayoutNullEventDelimiterTest failures on Windows.
+      </action>
       <action issue="LOG4J2-3075" dev="vy" type="fix">
         Fix formatting of nanoseconds in JsonTemplateLayout.
       </action>