Skip to content

Commit 8baedd7

Browse files
committed
Revert usage of artifical Clock in test as it causes occasional deadlocks
1 parent 7b10ba6 commit 8baedd7

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,57 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.rolling;
1818

19-
import org.apache.logging.log4j.Logger;
20-
import org.apache.logging.log4j.core.LoggerContext;
21-
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
22-
import org.apache.logging.log4j.plugins.Named;
23-
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
24-
import org.junit.jupiter.api.Test;
25-
2619
import java.io.File;
27-
import java.util.concurrent.CountDownLatch;
2820

29-
import static org.assertj.core.api.Assertions.assertThat;
21+
import org.apache.logging.log4j.Logger;
22+
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
23+
import org.hamcrest.Matcher;
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
import org.junit.rules.RuleChain;
27+
28+
import static org.apache.logging.log4j.core.test.hamcrest.Descriptors.that;
29+
import static org.apache.logging.log4j.core.test.hamcrest.FileMatchers.hasName;
30+
import static org.hamcrest.Matchers.endsWith;
31+
import static org.hamcrest.Matchers.hasItemInArray;
32+
import static org.junit.Assert.assertTrue;
33+
import static org.junit.Assert.fail;
3034

3135
/**
3236
*
3337
*/
34-
public class RollingAppenderTimeTest extends AbstractRollingListenerTest {
38+
public class RollingAppenderTimeTest {
3539

3640
private static final String CONFIG = "log4j-rolling2.xml";
3741
private static final String DIR = "target/rolling2";
38-
private final CountDownLatch rollover = new CountDownLatch(1);
42+
43+
private final LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
44+
45+
@Rule
46+
public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);
3947

4048
@Test
41-
@CleanUpDirectories(DIR)
42-
@LoggerContextSource(value = CONFIG, timeout = 10)
43-
public void testAppender(final LoggerContext context, @Named("RollingFile") final RollingFileManager manager) throws Exception {
44-
manager.addRolloverListener(this);
45-
final Logger logger = context.getLogger(getClass());
49+
public void testAppender() throws Exception {
50+
final Logger logger = loggerContextRule.getLogger();
4651
logger.debug("This is test message number 1");
47-
currentTimeMillis.addAndGet(1500);
52+
Thread.sleep(1500);
4853
// Trigger the rollover
4954
for (int i = 0; i < 16; ++i) {
50-
logger.debug("This is test message number {}", i + 1);
51-
currentTimeMillis.addAndGet(100);
55+
logger.debug("This is test message number " + i + 1);
5256
}
53-
rollover.await();
5457
final File dir = new File(DIR);
55-
assertThat(dir).isNotEmptyDirectory();
56-
assertThat(dir).isDirectoryContaining("glob:**.gz");
57-
}
58+
assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
5859

59-
@Override
60-
public void rolloverComplete(final String fileName) {
61-
rollover.countDown();
60+
final int MAX_TRIES = 20;
61+
final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
62+
for (int i = 0; i < MAX_TRIES; i++) {
63+
final File[] files = dir.listFiles();
64+
if (hasGzippedFile.matches(files)) {
65+
return; // test succeeded
66+
}
67+
logger.debug("Adding additional event " + i);
68+
Thread.sleep(100); // Allow time for rollover to complete
69+
}
70+
fail("No compressed files found");
6271
}
6372
}

0 commit comments

Comments
 (0)