Skip to content

Commit 141a54e

Browse files
authored
Stabilize RollingAppenderDeleteAccumulatedCount1Test (#3957)
1 parent 60c62ee commit 141a54e

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

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

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

19-
import static org.junit.Assert.assertEquals;
19+
import static org.awaitility.Awaitility.waitAtMost;
2020
import static org.junit.Assert.assertTrue;
2121
import static org.junit.Assert.fail;
2222

@@ -30,10 +30,10 @@
3030
import java.nio.file.attribute.FileTime;
3131
import java.util.Arrays;
3232
import java.util.List;
33+
import java.util.Objects;
34+
import java.util.concurrent.TimeUnit;
3335
import org.apache.logging.log4j.Logger;
3436
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
35-
import org.apache.logging.log4j.core.util.datetime.FixedDateFormat;
36-
import org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat;
3737
import org.junit.Rule;
3838
import org.junit.Test;
3939
import org.junit.rules.RuleChain;
@@ -67,24 +67,38 @@ public void testAppender() throws Exception {
6767
// 30 chars per message: each message triggers a rollover
6868
logger.debug("This is a test message number " + i); // 30 chars:
6969
}
70-
Thread.sleep(100); // Allow time for rollover to complete
7170

7271
final File dir = new File(DIR);
7372
assertTrue("Dir " + DIR + " should exist", dir.exists());
74-
assertTrue("Dir " + DIR + " should contain files", dir.listFiles().length > 0);
7573

76-
final File[] files = dir.listFiles();
77-
for (final File file : files) {
78-
System.out.println(file + " (" + file.length() + "B) "
79-
+ FixedDateFormat.create(FixedFormat.ABSOLUTE).format(file.lastModified()));
80-
}
8174
final List<String> expected = Arrays.asList("my-1.log", "my-2.log", "my-3.log", "my-4.log", "my-5.log");
82-
assertEquals(Arrays.toString(files), expected.size() + 6, files.length);
83-
for (final File file : files) {
84-
if (!expected.contains(file.getName()) && !file.getName().startsWith("test-")) {
85-
fail("unexpected file" + file);
75+
76+
waitAtMost(7, TimeUnit.SECONDS).untilAsserted(() -> {
77+
final File[] files = Objects.requireNonNull(dir.listFiles(), "listFiles()");
78+
assertTrue("Dir " + DIR + " should contain files", files.length > 0);
79+
80+
// The 5 my-*.log files must exist
81+
for (final String name : expected) {
82+
assertTrue("missing " + name, new File(dir, name).exists());
8683
}
87-
}
84+
85+
// Only allow my-*.log and test-*.log
86+
for (final File file : files) {
87+
final String n = file.getName();
88+
if (!expected.contains(n) && !n.startsWith("test-")) {
89+
fail("unexpected file " + file);
90+
}
91+
}
92+
93+
// Rolled files count should be within a reasonable band
94+
final long rolled = Arrays.stream(files)
95+
.map(File::getName)
96+
.filter(n -> n.startsWith("test-"))
97+
.count();
98+
99+
// Tolerate CRLF/LF differences + timing jitter
100+
assertTrue("expected rolled count in [6, 9], got " + rolled, rolled >= 6 && rolled <= 9);
101+
});
88102
}
89103

90104
private void updateLastModified(final Path... paths) throws IOException {

0 commit comments

Comments
 (0)