You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2020/11/11 16:59:02 UTC

[logging-log4j2] branch release-2.x updated: Fix async test since The GZ compression takes place asynchronously.

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

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 097c60a  Fix async test since The GZ compression takes place asynchronously.
097c60a is described below

commit 097c60a7e15293022aef2dfc783c177c7b2e62cf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Nov 11 11:58:56 2020 -0500

    Fix async test since The GZ compression takes place asynchronously.
    
    - Make sure it's done before validating.
    - Use JUnit 5 APIs.
---
 .../core/appender/rolling/RollingFileManager.java  |  9 ++++
 .../rolling/RollingAppenderRestartTest.java        | 53 +++++++++++++++-------
 2 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
index 638ff43..7d7811d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
@@ -409,6 +409,15 @@ public class RollingFileManager extends FileManager {
     }
 
     /**
+     * Package-private access for tests only.
+     *
+     * @return The semaphore that controls access to the rollover operation.
+     */
+    Semaphore getSemaphore() {
+        return semaphore;
+    }
+
+    /**
      * Returns the rollover strategy.
      * @return The RolloverStrategy
      */
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
index 1c02ac9..384e110 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
@@ -16,15 +16,13 @@
  */
 package org.apache.logging.log4j.core.appender.rolling;
 
-import org.apache.commons.io.file.PathUtils;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.hamcrest.Matcher;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.RuleChain;
+import static org.apache.logging.log4j.hamcrest.Descriptors.that;
+import static org.apache.logging.log4j.hamcrest.FileMatchers.hasName;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.File;
 import java.io.IOException;
@@ -37,12 +35,18 @@ import java.nio.file.attribute.FileTime;
 import java.time.Instant;
 import java.time.temporal.ChronoUnit;
 import java.util.Arrays;
+import java.util.concurrent.TimeUnit;
 
-import static org.apache.logging.log4j.hamcrest.Descriptors.that;
-import static org.apache.logging.log4j.hamcrest.FileMatchers.hasName;
-import static org.hamcrest.Matchers.endsWith;
-import static org.hamcrest.Matchers.hasItemInArray;
-import static org.junit.Assert.assertTrue;
+import org.apache.commons.io.file.PathUtils;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.RollingFileAppender;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.hamcrest.Matcher;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
 
 public class RollingAppenderRestartTest {
 
@@ -82,11 +86,26 @@ public class RollingAppenderRestartTest {
     public void testAppender() throws Exception {
         final Logger logger = loggerContextRule.getLogger();
         logger.info("This is test message number 1");
+        // The GZ compression takes place asynchronously.
+        // Make sure it's done before validating.
+        Thread.yield();
+        final String name = "RollingFile";
+        RollingFileAppender appender = loggerContextRule.getAppender(name);
+        assertNotNull(appender, name);
+        if (appender.getManager().getSemaphore().tryAcquire(5, TimeUnit.SECONDS)) {
+            // If we are in here, either the rollover is done or has not taken place yet.
+            validate();            
+        } else {
+            fail("Rolling over is taking too long.");
+        }
+    }
+
+    private void validate() {
         final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
         final File[] files = DIR.toFile().listFiles();
-        assertTrue(
-                "was expecting files with '.gz' suffix, found: " + Arrays.toString(files),
-                hasGzippedFile.matches(files));
+        Arrays.sort(files);
+        assertTrue(hasGzippedFile.matches(files),
+                () -> "was expecting files with '.gz' suffix, found: " + Arrays.toString(files));
     }
 
 }