Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
import java.nio.file.attribute.FileTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.core.util.datetime.FixedDateFormat;
import org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat;
import static org.awaitility.Awaitility.waitAtMost;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
Expand Down Expand Up @@ -67,24 +71,37 @@ public void testAppender() throws Exception {
// 30 chars per message: each message triggers a rollover
logger.debug("This is a test message number " + i); // 30 chars:
}
Thread.sleep(100); // Allow time for rollover to complete

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

final File[] files = dir.listFiles();
for (final File file : files) {
System.out.println(file + " (" + file.length() + "B) "
+ FixedDateFormat.create(FixedFormat.ABSOLUTE).format(file.lastModified()));
}
// Wait until the directory contents stabilize (no size change across two polls)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. You don't need Thread.sleep(), you can remove it. (Awaitility already polls.)
  2. Don't wait until contents stabilize, instead, wait until what you want to verify succeeds. That is, place the entire check logic there.
  3. Sign all your commits

waitAtMost(5, TimeUnit.SECONDS).until(() -> {
final File[] a = dir.listFiles();
if (a == null) return false;
final int n1 = a.length;
try { Thread.sleep(150); } catch (InterruptedException ignored) { }
final File[] b = dir.listFiles();
return b != null && b.length == n1;
});

final File[] files = Objects.requireNonNull(dir.listFiles());
assertTrue("Dir " + DIR + " should contain files", files.length > 0);

final List<String> expected = Arrays.asList("my-1.log", "my-2.log", "my-3.log", "my-4.log", "my-5.log");
assertEquals(Arrays.toString(files), expected.size() + 6, files.length);

// No unexpected names
for (final File file : files) {
if (!expected.contains(file.getName()) && !file.getName().startsWith("test-")) {
fail("unexpected file" + file);
fail("unexpected file " + file);
}
}

final long rolled =
Stream.of(files).filter(f -> f.getName().startsWith("test-")).count();

assertTrue("expected at least 6 rolled files but got " + rolled, rolled >= 6);
assertTrue("expected not more than 9 rolled files but got " + rolled, rolled <= 9);
}

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