summaryrefslogtreecommitdiffstats
path: root/container-accesslogging
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2020-03-30 15:21:52 +0200
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2020-03-30 15:33:37 +0200
commitfd93060e78edfb32d67151f5e809efad4b3ac1cb (patch)
treed39815821b6c90b5969b4f64c5dd607723891a1f /container-accesslogging
parentc098a56abdf64cb98554dc91592ca1b409e92540 (diff)
Stabilize LogFileHandlerTestCase.testSymlink()
- Loop until second file has expected file size - Improve naming of local variables - Remove unnecessary intermediate variables - Replace File.length() with Files.size() - Add test timeout - Simplify loops
Diffstat (limited to 'container-accesslogging')
-rw-r--r--container-accesslogging/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java68
1 files changed, 32 insertions, 36 deletions
diff --git a/container-accesslogging/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java b/container-accesslogging/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java
index d7361eec488..bc7257b1ca9 100644
--- a/container-accesslogging/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java
+++ b/container-accesslogging/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java
@@ -10,6 +10,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -103,52 +105,46 @@ public class LogFileHandlerTestCase {
h.shutdown();
}
- @Test
+ @Test(timeout = /*5 minutes*/300_000)
public void testSymlink() throws IOException, InterruptedException {
File root = temporaryFolder.newFolder("testlogforsymlinkchecking");
- LogFileHandler h = new LogFileHandler();
- h.setFilePattern(root.getAbsolutePath() + "/logfilehandlertest.%Y%m%d%H%M%S%s");
- h.setFormatter(new Formatter() {
+ LogFileHandler handler = new LogFileHandler();
+ handler.setFilePattern(root.getAbsolutePath() + "/logfilehandlertest.%Y%m%d%H%M%S%s");
+ handler.setFormatter(new Formatter() {
public String format(LogRecord r) {
DateFormat df = new SimpleDateFormat("yyyy.MM.dd:HH:mm:ss.SSS");
String timeStamp = df.format(new Date(r.getMillis()));
return ("["+timeStamp+"]" + " " + formatMessage(r) + "\n");
}
} );
- h.setSymlinkName("symlink");
- LogRecord lr = new LogRecord(Level.INFO, "test");
- h.publish(lr);
- String f1 = h.getFileName();
- String f2 = null;
- while (f1 == null) {
- Thread.sleep(1);
- f1 = h.getFileName();
- }
- h.rotateNow();
- Thread.sleep(1);
- f2 = h.getFileName();
- while (f1.equals(f2)) {
+ handler.setSymlinkName("symlink");
+
+ handler.publish(new LogRecord(Level.INFO, "test"));
+ String firstFile;
+ do {
+ Thread.sleep(1);
+ firstFile = handler.getFileName();
+ } while (firstFile == null);
+ handler.rotateNow();
+ String secondFileName;
+ do {
Thread.sleep(1);
- f2 = h.getFileName();
- }
- lr = new LogRecord(Level.INFO, "string which is way longer than the word test");
- h.publish(lr);
- h.waitDrained();
- File f = new File(f1);
- long first = f.length();
- f = new File(f2);
- long second = f.length();
- final long secondLength = 72;
- for (int n = 0; n < 20 && second != secondLength; ++n) {
+ secondFileName = handler.getFileName();
+ } while (firstFile.equals(secondFileName));
+
+ handler.publish(new LogRecord(Level.INFO, "string which is way longer than the word test"));
+ handler.waitDrained();
+ assertThat(Files.size(Paths.get(firstFile))).isEqualTo(31);
+ final long expectedSecondFileLength = 72;
+ long secondFileLength;
+ do {
Thread.sleep(1);
- second = f.length();
- }
- f = new File(root, "symlink");
- long link = f.length();
- assertThat(secondLength).isEqualTo(link);
- assertThat(31).isEqualTo(first);
- assertThat(secondLength).isEqualTo(second);
- h.shutdown();
+ secondFileLength = Files.size(Paths.get(secondFileName));
+ } while (secondFileLength != expectedSecondFileLength);
+
+ long symlinkFileLength = Files.size(root.toPath().resolve("symlink"));
+ assertThat(symlinkFileLength).isEqualTo(expectedSecondFileLength);
+ handler.shutdown();
}
@Test