|
16 | 16 | */ |
17 | 17 | package org.apache.logging.log4j.core.appender.rolling; |
18 | 18 |
|
19 | | -import static org.junit.Assert.assertEquals; |
| 19 | +import static org.awaitility.Awaitility.waitAtMost; |
20 | 20 | import static org.junit.Assert.assertTrue; |
21 | 21 | import static org.junit.Assert.fail; |
22 | 22 |
|
|
30 | 30 | import java.nio.file.attribute.FileTime; |
31 | 31 | import java.util.Arrays; |
32 | 32 | import java.util.List; |
| 33 | +import java.util.Objects; |
| 34 | +import java.util.concurrent.TimeUnit; |
33 | 35 | import org.apache.logging.log4j.Logger; |
34 | 36 | 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; |
37 | 37 | import org.junit.Rule; |
38 | 38 | import org.junit.Test; |
39 | 39 | import org.junit.rules.RuleChain; |
@@ -67,24 +67,38 @@ public void testAppender() throws Exception { |
67 | 67 | // 30 chars per message: each message triggers a rollover |
68 | 68 | logger.debug("This is a test message number " + i); // 30 chars: |
69 | 69 | } |
70 | | - Thread.sleep(100); // Allow time for rollover to complete |
71 | 70 |
|
72 | 71 | final File dir = new File(DIR); |
73 | 72 | assertTrue("Dir " + DIR + " should exist", dir.exists()); |
74 | | - assertTrue("Dir " + DIR + " should contain files", dir.listFiles().length > 0); |
75 | 73 |
|
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 | | - } |
81 | 74 | 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()); |
86 | 83 | } |
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 | + }); |
88 | 102 | } |
89 | 103 |
|
90 | 104 | private void updateLastModified(final Path... paths) throws IOException { |
|
0 commit comments