Skip to content

Commit f1c98a7

Browse files
committedFeb 16, 2025··
Fix RollingRandomAppenderDirectWriteAndSwitchDirectoryTest
Attempt to fix the flaky `RollingRandomAppenderDirectWriteAndSwitchDirectoryTest`.
1 parent c59fdd4 commit f1c98a7

File tree

3 files changed

+48
-57
lines changed

3 files changed

+48
-57
lines changed
 
+12-9
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,36 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.rolling;
1818

19-
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import static org.assertj.core.api.Assertions.assertThat;
2020

21-
import java.io.File;
21+
import java.nio.file.Path;
2222
import java.time.LocalTime;
2323
import org.apache.logging.log4j.core.Logger;
2424
import org.apache.logging.log4j.core.LoggerContext;
2525
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2626
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
27+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
2728
import org.junit.jupiter.api.Test;
2829

29-
@CleanUpDirectories(RollingRandomAppenderDirectWriteAndSwitchDirectorTest.DIR)
30-
class RollingRandomAppenderDirectWriteAndSwitchDirectorTest {
30+
@CleanUpDirectories(RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.DIR)
31+
class RollingRandomAppenderDirectWriteAndSwitchDirectoryTest {
3132
public static final String DIR = "target/rolling-random-direct-switch-director";
3233

34+
@TempLoggingDir
35+
private Path loggingPath;
36+
3337
@Test
34-
@LoggerContextSource(value = "log4j-rolling-random-direct-switch-director.xml", timeout = 10)
38+
@LoggerContextSource(value = "appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.xml", timeout = 10)
3539
void testAppender(final LoggerContext context) throws Exception {
36-
final Logger logger = context.getLogger(RollingRandomAppenderDirectWriteAndSwitchDirectorTest.class.getName());
40+
final Logger logger = context.getLogger(RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.class.getName());
3741
final LocalTime start = LocalTime.now();
3842
LocalTime end;
3943
do {
4044
end = LocalTime.now();
4145
logger.info("test log");
4246
Thread.sleep(100);
4347
} while (start.getSecond() == end.getSecond());
44-
45-
final File nextLogFile = new File(String.format("%s/%d/%d.log", DIR, end.getSecond(), end.getSecond()));
46-
assertTrue(nextLogFile.exists(), "nextLogFile not created");
48+
Path nextLogPath = loggingPath.resolve(String.format("%d/%d.log", end.getSecond(), end.getSecond()));
49+
assertThat(nextLogPath).as("Archived log for second %s", end.getSecond()).exists();
4750
}
4851
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="
21+
https://logging.apache.org/xml/ns
22+
https://logging.apache.org/xml/ns/log4j-config-2.xsd"
23+
name="RollingRandomAppenderDirectWriteAndSwitchDirectoryTest">
24+
<Appenders>
25+
<RollingRandomAccessFile name="ROLLING"
26+
filePattern="${test:logging.path}/%d{s}/%d{s}.log">
27+
<PatternLayout pattern="%d{s.SSS} %m%n"/>
28+
<TimeBasedTriggeringPolicy/>
29+
</RollingRandomAccessFile>
30+
</Appenders>
31+
<Loggers>
32+
<Root level="DEBUG">
33+
<AppenderRef ref="ROLLING"/>
34+
</Root>
35+
</Loggers>
36+
</Configuration>

‎log4j-core-test/src/test/resources/log4j-rolling-random-direct-switch-director.xml

-48
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.