You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2023/01/02 16:05:04 UTC
[logging-log4j2] 02/02: Revert usage of artifical Clock in test as it causes occasional deadlocks
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 8baedd77949e4ff4c9d9999d747ff30e39903309
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Jan 2 09:04:43 2023 -0700
Revert usage of artifical Clock in test as it causes occasional deadlocks
---
.../appender/rolling/RollingAppenderTimeTest.java | 61 +++++++++++++---------
1 file changed, 35 insertions(+), 26 deletions(-)
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java
index 3633c38368..3233341d43 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java
@@ -16,48 +16,57 @@
*/
package org.apache.logging.log4j.core.appender.rolling;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
-import org.apache.logging.log4j.plugins.Named;
-import org.apache.logging.log4j.test.junit.CleanUpDirectories;
-import org.junit.jupiter.api.Test;
-
import java.io.File;
-import java.util.concurrent.CountDownLatch;
-import static org.assertj.core.api.Assertions.assertThat;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
+import org.hamcrest.Matcher;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+
+import static org.apache.logging.log4j.core.test.hamcrest.Descriptors.that;
+import static org.apache.logging.log4j.core.test.hamcrest.FileMatchers.hasName;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
/**
*
*/
-public class RollingAppenderTimeTest extends AbstractRollingListenerTest {
+public class RollingAppenderTimeTest {
private static final String CONFIG = "log4j-rolling2.xml";
private static final String DIR = "target/rolling2";
- private final CountDownLatch rollover = new CountDownLatch(1);
+
+ private final LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+
+ @Rule
+ public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);
@Test
- @CleanUpDirectories(DIR)
- @LoggerContextSource(value = CONFIG, timeout = 10)
- public void testAppender(final LoggerContext context, @Named("RollingFile") final RollingFileManager manager) throws Exception {
- manager.addRolloverListener(this);
- final Logger logger = context.getLogger(getClass());
+ public void testAppender() throws Exception {
+ final Logger logger = loggerContextRule.getLogger();
logger.debug("This is test message number 1");
- currentTimeMillis.addAndGet(1500);
+ Thread.sleep(1500);
// Trigger the rollover
for (int i = 0; i < 16; ++i) {
- logger.debug("This is test message number {}", i + 1);
- currentTimeMillis.addAndGet(100);
+ logger.debug("This is test message number " + i + 1);
}
- rollover.await();
final File dir = new File(DIR);
- assertThat(dir).isNotEmptyDirectory();
- assertThat(dir).isDirectoryContaining("glob:**.gz");
- }
+ assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
- @Override
- public void rolloverComplete(final String fileName) {
- rollover.countDown();
+ final int MAX_TRIES = 20;
+ final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
+ for (int i = 0; i < MAX_TRIES; i++) {
+ final File[] files = dir.listFiles();
+ if (hasGzippedFile.matches(files)) {
+ return; // test succeeded
+ }
+ logger.debug("Adding additional event " + i);
+ Thread.sleep(100); // Allow time for rollover to complete
+ }
+ fail("No compressed files found");
}
}