This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch fix/2.x/rolling_appender_test in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit f1c98a736222ef052e84cd2da4654292f9e6aa0f Author: Piotr P. Karwasz <[email protected]> AuthorDate: Sun Feb 16 09:26:15 2025 +0100 Fix `RollingRandomAppenderDirectWriteAndSwitchDirectoryTest` Attempt to fix the flaky `RollingRandomAppenderDirectWriteAndSwitchDirectoryTest`. --- ...AppenderDirectWriteAndSwitchDirectoryTest.java} | 21 ++++++---- ...omAppenderDirectWriteAndSwitchDirectoryTest.xml | 36 ++++++++++++++++ ...log4j-rolling-random-direct-switch-director.xml | 48 ---------------------- 3 files changed, 48 insertions(+), 57 deletions(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectorTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.java similarity index 72% rename from log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectorTest.java rename to log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.java index ceb6faacfe..14e56c9d0f 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectorTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.java @@ -16,24 +16,28 @@ */ package org.apache.logging.log4j.core.appender.rolling; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; -import java.io.File; +import java.nio.file.Path; import java.time.LocalTime; import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.test.junit.LoggerContextSource; import org.apache.logging.log4j.test.junit.CleanUpDirectories; +import org.apache.logging.log4j.test.junit.TempLoggingDir; import org.junit.jupiter.api.Test; -@CleanUpDirectories(RollingRandomAppenderDirectWriteAndSwitchDirectorTest.DIR) -class RollingRandomAppenderDirectWriteAndSwitchDirectorTest { +@CleanUpDirectories(RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.DIR) +class RollingRandomAppenderDirectWriteAndSwitchDirectoryTest { public static final String DIR = "target/rolling-random-direct-switch-director"; + @TempLoggingDir + private Path loggingPath; + @Test - @LoggerContextSource(value = "log4j-rolling-random-direct-switch-director.xml", timeout = 10) + @LoggerContextSource(value = "appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.xml", timeout = 10) void testAppender(final LoggerContext context) throws Exception { - final Logger logger = context.getLogger(RollingRandomAppenderDirectWriteAndSwitchDirectorTest.class.getName()); + final Logger logger = context.getLogger(RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.class.getName()); final LocalTime start = LocalTime.now(); LocalTime end; do { @@ -41,8 +45,7 @@ class RollingRandomAppenderDirectWriteAndSwitchDirectorTest { logger.info("test log"); Thread.sleep(100); } while (start.getSecond() == end.getSecond()); - - final File nextLogFile = new File(String.format("%s/%d/%d.log", DIR, end.getSecond(), end.getSecond())); - assertTrue(nextLogFile.exists(), "nextLogFile not created"); + Path nextLogPath = loggingPath.resolve(String.format("%d/%d.log", end.getSecond(), end.getSecond())); + assertThat(nextLogPath).as("Archived log for second %s", end.getSecond()).exists(); } } diff --git a/log4j-core-test/src/test/resources/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.xml b/log4j-core-test/src/test/resources/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.xml new file mode 100644 index 0000000000..73ea080223 --- /dev/null +++ b/log4j-core-test/src/test/resources/appender/rolling/RollingRandomAppenderDirectWriteAndSwitchDirectoryTest.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to you under the Apache License, Version 2.0 + ~ (the "License"); you may not use this file except in compliance with + ~ the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<Configuration xmlns="https://logging.apache.org/xml/ns" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + https://logging.apache.org/xml/ns + https://logging.apache.org/xml/ns/log4j-config-2.xsd" + name="RollingRandomAppenderDirectWriteAndSwitchDirectoryTest"> + <Appenders> + <RollingRandomAccessFile name="ROLLING" + filePattern="${test:logging.path}/%d{s}/%d{s}.log"> + <PatternLayout pattern="%d{s.SSS} %m%n"/> + <TimeBasedTriggeringPolicy/> + </RollingRandomAccessFile> + </Appenders> + <Loggers> + <Root level="DEBUG"> + <AppenderRef ref="ROLLING"/> + </Root> + </Loggers> +</Configuration> diff --git a/log4j-core-test/src/test/resources/log4j-rolling-random-direct-switch-director.xml b/log4j-core-test/src/test/resources/log4j-rolling-random-direct-switch-director.xml deleted file mode 100644 index 638b438167..0000000000 --- a/log4j-core-test/src/test/resources/log4j-rolling-random-direct-switch-director.xml +++ /dev/null @@ -1,48 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ Licensed to the Apache Software Foundation (ASF) under one or more - ~ contributor license agreements. See the NOTICE file distributed with - ~ this work for additional information regarding copyright ownership. - ~ The ASF licenses this file to you under the Apache License, Version 2.0 - ~ (the "License"); you may not use this file except in compliance with - ~ the License. You may obtain a copy of the License at - ~ - ~ http://www.apache.org/licenses/LICENSE-2.0 - ~ - ~ Unless required by applicable law or agreed to in writing, software - ~ distributed under the License is distributed on an "AS IS" BASIS, - ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - ~ See the License for the specific language governing permissions and - ~ limitations under the License. - --> -<Configuration status="WARN" name="XMLConfigTest"> - <Properties> - <Property name="LOG_HEADER">start log4j-rolling-random-direct-switch-director test</Property> - <Property name="LOG_PATTERN">%d %p %C{1.} [%t] %m%n</Property> - <Property name="baseDir">target/rolling-random-direct-switch-director</Property> - </Properties> - <Appenders> - <Console name="STDOUT" target="SYSTEM_OUT" follow="true"> - <PatternLayout header= "${LOG_HEADER}"> - <Pattern>${LOG_PATTERN}</Pattern> - </PatternLayout> - </Console> - <RollingRandomAccessFile name="RollingFile" filePattern="${baseDir}/%d{s}/%d{s}.log"> - <PatternLayout header= "${LOG_HEADER}"> - <Pattern>${LOG_PATTERN}</Pattern> - </PatternLayout> - <Policies> - <TimeBasedTriggeringPolicy/> - </Policies> - </RollingRandomAccessFile> - </Appenders> - - <Loggers> - <Logger name="org.apache.logging.log4j.core.appender.rolling" level="debug" additivity="false"> - <AppenderRef ref="RollingFile"/> - </Logger>> - <Root level="error"> - <AppenderRef ref="STDOUT" /> - </Root> - </Loggers> -</Configuration>
