|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.core.appender.rolling; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.FileWriter; |
| 25 | +import java.io.IOException; |
| 26 | +import org.apache.logging.log4j.core.appender.rolling.action.Action; |
| 27 | +import org.apache.logging.log4j.core.appender.rolling.action.GzCompressAction; |
| 28 | +import org.apache.logging.log4j.core.appender.rolling.action.ZipCompressAction; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.io.TempDir; |
| 31 | + |
| 32 | +/** |
| 33 | + * Issue #4012 — verifies that FileExtension.GZ and FileExtension.ZIP correctly pass |
| 34 | + * maxCompressionDelaySeconds through to the compression action. |
| 35 | + * |
| 36 | + * This was the root cause of the bug: the 5-argument createCompressAction() in GZ and ZIP |
| 37 | + * fell back to the 4-argument version, silently dropping the delay value. |
| 38 | + */ |
| 39 | +class FileExtensionCompressDelayTest { |
| 40 | + |
| 41 | + // ── GZ ──────────────────────────────────────────────────────────────── |
| 42 | + |
| 43 | + /** |
| 44 | + * FileExtension.GZ.createCompressAction(5-args) must produce a GzCompressAction |
| 45 | + * that applies the random delay — not fall back to 0. |
| 46 | + */ |
| 47 | + @Test |
| 48 | + void testGzCreateCompressActionWithDelay(@TempDir File tempDir) throws Exception { |
| 49 | + File source = new File(tempDir, "app.log"); |
| 50 | + File dest = new File(tempDir, "app.log.gz"); |
| 51 | + writeContent(source, "gz test content"); |
| 52 | + |
| 53 | + int maxDelay = 2; |
| 54 | + Action action = FileExtension.GZ.createCompressAction(source.getPath(), dest.getPath(), true, -1, maxDelay); |
| 55 | + |
| 56 | + // Must return a GzCompressAction (not some other type) |
| 57 | + assertInstanceOf(GzCompressAction.class, action, "Expected GzCompressAction"); |
| 58 | + |
| 59 | + long start = System.currentTimeMillis(); |
| 60 | + action.execute(); |
| 61 | + long elapsed = System.currentTimeMillis() - start; |
| 62 | + |
| 63 | + // Must finish within maxDelay + margin (delay IS applied via FileExtension) |
| 64 | + assertTrue( |
| 65 | + elapsed <= (maxDelay * 1000L) + 500, |
| 66 | + "GZ compress via FileExtension exceeded maxDelay=" + maxDelay + "s: " + elapsed + "ms"); |
| 67 | + assertTrue(dest.exists(), "Compressed .gz file must exist"); |
| 68 | + assertFalse(source.exists(), "Source must be deleted after compression"); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * FileExtension.GZ.createCompressAction(5-args, delay=0) must behave identically |
| 73 | + * to the 4-arg version — instant compression, no delay. |
| 74 | + */ |
| 75 | + @Test |
| 76 | + void testGzCreateCompressActionNoDelay(@TempDir File tempDir) throws Exception { |
| 77 | + File source = new File(tempDir, "app-nodelay.log"); |
| 78 | + File dest = new File(tempDir, "app-nodelay.log.gz"); |
| 79 | + writeContent(source, "gz no-delay content"); |
| 80 | + |
| 81 | + Action action = FileExtension.GZ.createCompressAction(source.getPath(), dest.getPath(), true, -1, 0); |
| 82 | + |
| 83 | + assertInstanceOf(GzCompressAction.class, action); |
| 84 | + |
| 85 | + long start = System.currentTimeMillis(); |
| 86 | + action.execute(); |
| 87 | + long elapsed = System.currentTimeMillis() - start; |
| 88 | + |
| 89 | + assertTrue(elapsed < 500, "GZ with delay=0 should be instant, took " + elapsed + "ms"); |
| 90 | + assertTrue(dest.exists(), "Compressed .gz file must exist"); |
| 91 | + assertFalse(source.exists(), "Source must be deleted"); |
| 92 | + } |
| 93 | + |
| 94 | + // ── ZIP ─────────────────────────────────────────────────────────────── |
| 95 | + |
| 96 | + /** |
| 97 | + * FileExtension.ZIP.createCompressAction(5-args) must produce a ZipCompressAction |
| 98 | + * that applies the random delay — not fall back to 0. |
| 99 | + */ |
| 100 | + @Test |
| 101 | + void testZipCreateCompressActionWithDelay(@TempDir File tempDir) throws Exception { |
| 102 | + File source = new File(tempDir, "app.log"); |
| 103 | + File dest = new File(tempDir, "app.log.zip"); |
| 104 | + writeContent(source, "zip test content"); |
| 105 | + |
| 106 | + int maxDelay = 2; |
| 107 | + Action action = FileExtension.ZIP.createCompressAction(source.getPath(), dest.getPath(), true, 0, maxDelay); |
| 108 | + |
| 109 | + assertInstanceOf(ZipCompressAction.class, action, "Expected ZipCompressAction"); |
| 110 | + |
| 111 | + long start = System.currentTimeMillis(); |
| 112 | + action.execute(); |
| 113 | + long elapsed = System.currentTimeMillis() - start; |
| 114 | + |
| 115 | + assertTrue( |
| 116 | + elapsed <= (maxDelay * 1000L) + 500, |
| 117 | + "ZIP compress via FileExtension exceeded maxDelay=" + maxDelay + "s: " + elapsed + "ms"); |
| 118 | + assertTrue(dest.exists(), "Compressed .zip file must exist"); |
| 119 | + assertFalse(source.exists(), "Source must be deleted after compression"); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * FileExtension.ZIP.createCompressAction(5-args, delay=0) must behave identically |
| 124 | + * to the 4-arg version — instant compression, no delay. |
| 125 | + */ |
| 126 | + @Test |
| 127 | + void testZipCreateCompressActionNoDelay(@TempDir File tempDir) throws Exception { |
| 128 | + File source = new File(tempDir, "app-nodelay.log"); |
| 129 | + File dest = new File(tempDir, "app-nodelay.log.zip"); |
| 130 | + writeContent(source, "zip no-delay content"); |
| 131 | + |
| 132 | + Action action = FileExtension.ZIP.createCompressAction(source.getPath(), dest.getPath(), true, 0, 0); |
| 133 | + |
| 134 | + assertInstanceOf(ZipCompressAction.class, action); |
| 135 | + |
| 136 | + long start = System.currentTimeMillis(); |
| 137 | + action.execute(); |
| 138 | + long elapsed = System.currentTimeMillis() - start; |
| 139 | + |
| 140 | + assertTrue(elapsed < 500, "ZIP with delay=0 should be instant, took " + elapsed + "ms"); |
| 141 | + assertTrue(dest.exists(), "Compressed .zip file must exist"); |
| 142 | + assertFalse(source.exists(), "Source must be deleted"); |
| 143 | + } |
| 144 | + |
| 145 | + // ── helpers ─────────────────────────────────────────────────────────── |
| 146 | + |
| 147 | + private static void writeContent(final File file, final String content) throws IOException { |
| 148 | + try (FileWriter writer = new FileWriter(file)) { |
| 149 | + writer.write(content); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments