From fe72f3a80145f662bb68105ebfef163cd3103f4e Mon Sep 17 00:00:00 2001 From: rjuare8 Date: Thu, 2 Oct 2025 23:30:28 -0500 Subject: [PATCH 1/4] fixed undeterministic RollingAppenderDirectCronTest --- .../rolling/RollingAppenderDirectCronTest.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java index 6751d3cb1f6..cfadd237577 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java @@ -78,7 +78,7 @@ public RolloverDelay(final RollingFileManager manager) { } public void waitForRollover() { - waitAtMost(5, TimeUnit.SECONDS) + waitAtMost(7, TimeUnit.SECONDS) .alias("Rollover timeout") .until(() -> latch.getCount() == 0 || assertion != null); if (assertion != null) { @@ -102,16 +102,18 @@ public void rolloverComplete(final String fileName) { final Path path = Paths.get(fileName); final Matcher matcher = FILE_PATTERN.matcher(path.getFileName().toString()); assertThat(matcher).as("Rolled file").matches(); - try { - final List lines = Files.readAllLines(path); - assertThat(lines).isNotEmpty(); - assertThat(lines.get(0)).startsWith(matcher.group(1)); - } catch (final IOException ex) { - fail("Unable to read file " + fileName + ": " + ex.getMessage()); - } + + // Wait until the file is non-empty (or timeout) + waitAtMost(2, TimeUnit.SECONDS).until(() -> Files.exists(path) && Files.size(path) > 0); + + final List lines = Files.readAllLines(path); + assertThat(lines).isNotEmpty(); + assertThat(lines.get(0)).startsWith(matcher.group(1)); latch.countDown(); } catch (final AssertionError ex) { assertion = ex; + } catch (final IOException ex) { + fail("Unable to read file " + fileName + ": " + ex.getMessage()); } } } From 2076a8c6a33c12bb53fd2850678f02c9f14e8622 Mon Sep 17 00:00:00 2001 From: rjuare8 Date: Thu, 16 Oct 2025 20:50:12 -0500 Subject: [PATCH 2/4] Fixed non-deterministic behavior in testFlushAtEndOfBatch() test --- .../log4j/core/appender/XmlCompleteFileAppenderTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java index d5ce1a47e67..aa54635a1c3 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java @@ -82,8 +82,13 @@ public void testFlushAtEndOfBatch() throws Exception { try (final BufferedReader reader = new BufferedReader(new FileReader(logFile))) { line1 = reader.readLine(); line2 = reader.readLine(); - reader.readLine(); // ignore the empty line after the root - line3 = reader.readLine(); + + String tmp; + do { + tmp = reader.readLine(); // ignore the empty lines after the root + } while (tmp != null && tmp.trim().isEmpty()); + + line3 = tmp; line4 = reader.readLine(); line5 = reader.readLine(); } finally { From 6fbd257b84020359dfd2feb9c7b9ee6db948c090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Wed, 22 Oct 2025 11:34:38 +0200 Subject: [PATCH 3/4] Improve verification --- .../RollingAppenderDirectCronTest.java | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java index cfadd237577..444016dfd1f 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java @@ -18,9 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.waitAtMost; -import static org.junit.jupiter.api.Assertions.fail; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -70,7 +68,7 @@ void testAppender(final LoggerContext ctx, @Named("RollingFile") final RollingFi private static class RolloverDelay implements RolloverListener { private final Logger logger = StatusLogger.getLogger(); private volatile CountDownLatch latch; - private volatile AssertionError assertion; + private volatile Exception verificationFailure; public RolloverDelay(final RollingFileManager manager) { latch = new CountDownLatch(1); @@ -78,11 +76,11 @@ public RolloverDelay(final RollingFileManager manager) { } public void waitForRollover() { - waitAtMost(7, TimeUnit.SECONDS) + waitAtMost(5, TimeUnit.SECONDS) .alias("Rollover timeout") - .until(() -> latch.getCount() == 0 || assertion != null); - if (assertion != null) { - throw assertion; + .until(() -> latch.getCount() == 0 || verificationFailure != null); + if (verificationFailure != null) { + throw new RuntimeException(verificationFailure); } } @@ -102,18 +100,15 @@ public void rolloverComplete(final String fileName) { final Path path = Paths.get(fileName); final Matcher matcher = FILE_PATTERN.matcher(path.getFileName().toString()); assertThat(matcher).as("Rolled file").matches(); - - // Wait until the file is non-empty (or timeout) - waitAtMost(2, TimeUnit.SECONDS).until(() -> Files.exists(path) && Files.size(path) > 0); - - final List lines = Files.readAllLines(path); - assertThat(lines).isNotEmpty(); - assertThat(lines.get(0)).startsWith(matcher.group(1)); + waitAtMost(2, TimeUnit.SECONDS).untilAsserted(() -> { + final List lines = Files.readAllLines(path); + assertThat(lines).isNotEmpty(); + assertThat(lines.get(0)).startsWith(matcher.group(1)); + }); latch.countDown(); - } catch (final AssertionError ex) { - assertion = ex; - } catch (final IOException ex) { - fail("Unable to read file " + fileName + ": " + ex.getMessage()); + } catch (final Exception ex) { + verificationFailure = + new RuntimeException("Rollover completion verification failure for file: " + fileName, ex); } } } From fc0906ae99826f67d76ffc1b6836346fefe9ef04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Wed, 22 Oct 2025 12:28:03 +0200 Subject: [PATCH 4/4] Fix Spotless failures --- .../log4j/core/appender/XmlCompleteFileAppenderTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java index aa54635a1c3..c3310ac9303 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java @@ -82,13 +82,10 @@ public void testFlushAtEndOfBatch() throws Exception { try (final BufferedReader reader = new BufferedReader(new FileReader(logFile))) { line1 = reader.readLine(); line2 = reader.readLine(); - - String tmp; - do { - tmp = reader.readLine(); // ignore the empty lines after the root - } while (tmp != null && tmp.trim().isEmpty()); - line3 = tmp; + // Locate the first non-empty line after the `` root + while ((line3 = reader.readLine()) != null && line3.trim().isEmpty()) {} + line4 = reader.readLine(); line5 = reader.readLine(); } finally {