summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'jdisc_http_service/src/test')
-rw-r--r--jdisc_http_service/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java62
-rw-r--r--jdisc_http_service/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java42
-rw-r--r--jdisc_http_service/src/test/java/com/yahoo/container/logging/CompressWhileDrop.java11
-rw-r--r--jdisc_http_service/src/test/java/com/yahoo/container/logging/JSONLogTestCase.java276
-rw-r--r--jdisc_http_service/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java187
-rw-r--r--jdisc_http_service/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java27
6 files changed, 605 insertions, 0 deletions
diff --git a/jdisc_http_service/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java b/jdisc_http_service/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java
new file mode 100644
index 00000000000..32a55b4bb72
--- /dev/null
+++ b/jdisc_http_service/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java
@@ -0,0 +1,62 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.hasItems;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.contains;
+import static org.junit.Assert.assertThat;
+
+public class AccessLogSamplerTest {
+
+ private final List<String> uris = new ArrayList<>();
+ private CircularArrayAccessLogKeeper circularArrayAccessLogKeeperMock = new CircularArrayAccessLogKeeper() {
+ @Override
+ public void addUri(String uri) {
+ uris.add(uri);
+ }
+ };
+ private AccessLogSampler accessLogSampler = new AccessLogSampler(circularArrayAccessLogKeeperMock);
+
+ @Test
+ public void testAFewLines() {
+ accessLogSampler.log(createLogEntry(200, "/search/foo"));
+ accessLogSampler.log(createLogEntry(500, "/search/bar"));
+ accessLogSampler.log(createLogEntry(500, "bar"));
+ accessLogSampler.log(createLogEntry(200, "/search/what"));
+ assertThat(uris, contains("/search/foo", "/search/what"));
+ }
+
+ @Test
+ public void testSubSampling() {
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
+ accessLogSampler.log(createLogEntry(200, "/search/" + String.valueOf(i)));
+ }
+ assertThat(uris.size(), is(CircularArrayAccessLogKeeper.SIZE));
+ assertThat(uris, hasItems("/search/0", "/search/1", "/search/2",
+ "/search/" + String.valueOf(CircularArrayAccessLogKeeper.SIZE - 1)));
+ uris.clear();
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
+ accessLogSampler.log(createLogEntry(200, "/search/fuzz"));
+ }
+ assertThat(uris, hasItem("/search/fuzz"));
+ assertThat(uris.size(), is(1));
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
+ accessLogSampler.log(createLogEntry(200, "/search/ball"));
+ }
+ assertThat(uris, hasItem("/search/ball"));
+ assertThat(uris.size(), is(2));
+ }
+
+ private AccessLogEntry createLogEntry(int statusCode, String uri) {
+ AccessLogEntry accessLogEntry = new AccessLogEntry();
+ accessLogEntry.setStatusCode(statusCode);
+ accessLogEntry.setRawPath(uri);
+ return accessLogEntry;
+ }
+}
diff --git a/jdisc_http_service/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java b/jdisc_http_service/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java
new file mode 100644
index 00000000000..5d9509eb045
--- /dev/null
+++ b/jdisc_http_service/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java
@@ -0,0 +1,42 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.hasItems;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsCollectionContaining.hasItem;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+
+public class CircularArrayAccessLogKeeperTest {
+ private CircularArrayAccessLogKeeper circularArrayAccessLogKeeper = new CircularArrayAccessLogKeeper();
+
+ @Test
+ public void testSizeIsCroppedCorrectly() {
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE - 1; i++) {
+ circularArrayAccessLogKeeper.addUri(String.valueOf(i));
+ }
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(CircularArrayAccessLogKeeper.SIZE -1));
+ circularArrayAccessLogKeeper.addUri("foo");
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(CircularArrayAccessLogKeeper.SIZE));
+ circularArrayAccessLogKeeper.addUri("bar");
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(CircularArrayAccessLogKeeper.SIZE));
+ assertThat(circularArrayAccessLogKeeper.getUris(), hasItems("1", "2", "3", "foo", "bar"));
+ assertThat(circularArrayAccessLogKeeper.getUris(), not(hasItem("0")));
+ }
+
+ @Test
+ public void testEmpty() {
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(0));
+ }
+
+ @Test
+ public void testSomeItems() {
+ circularArrayAccessLogKeeper.addUri("a");
+ circularArrayAccessLogKeeper.addUri("b");
+ circularArrayAccessLogKeeper.addUri("b");
+ assertThat(circularArrayAccessLogKeeper.getUris(), contains("a", "b", "b"));
+ }
+}
diff --git a/jdisc_http_service/src/test/java/com/yahoo/container/logging/CompressWhileDrop.java b/jdisc_http_service/src/test/java/com/yahoo/container/logging/CompressWhileDrop.java
new file mode 100644
index 00000000000..2099037203f
--- /dev/null
+++ b/jdisc_http_service/src/test/java/com/yahoo/container/logging/CompressWhileDrop.java
@@ -0,0 +1,11 @@
+// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import java.io.File;
+
+public class CompressWhileDrop {
+ public static void main(String [] args) {
+ System.out.println("Start compressing file " + args[0]);
+ LogFileHandler.runCompression(new File(args[0]));
+ }
+}
diff --git a/jdisc_http_service/src/test/java/com/yahoo/container/logging/JSONLogTestCase.java b/jdisc_http_service/src/test/java/com/yahoo/container/logging/JSONLogTestCase.java
new file mode 100644
index 00000000000..6c7878f99ed
--- /dev/null
+++ b/jdisc_http_service/src/test/java/com/yahoo/container/logging/JSONLogTestCase.java
@@ -0,0 +1,276 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import com.yahoo.yolean.trace.TraceNode;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.URI;
+
+import static org.junit.Assert.assertEquals;
+
+
+/**
+ * @author frodelu
+ */
+public class JSONLogTestCase {
+
+ private static String ipAddress = "152.200.54.243";
+ private static final String EMPTY_REFERRER = "";
+ private static final String EMPTY_USERAGENT = "";
+
+ private AccessLogEntry newAccessLogEntry(final String query) {
+ return newAccessLogEntry(query, new Coverage(100,100,100,0));
+ }
+ private AccessLogEntry newAccessLogEntry(final String query, Coverage coverage) {
+ final AccessLogEntry entry = new AccessLogEntry();
+ entry.setRawQuery("query="+query);
+ entry.setRawPath("");
+ entry.setIpV4Address(ipAddress);
+ entry.setHttpMethod("GET");
+ entry.setHttpVersion("HTTP/1.1");
+ entry.setUserAgent("Mozilla/4.05 [en] (Win95; I)");
+ entry.setHitCounts(new HitCounts(0, 10, 1234, 0, 10, coverage));
+ entry.setHostString("localhost");
+ entry.setStatusCode(200);
+ entry.setTimeStamp(920880005023L);
+ entry.setDurationBetweenRequestResponse(122);
+ entry.setReturnedContentSize(9875);
+
+ return entry;
+ }
+
+ private static URI newQueryUri(final String query) {
+ return URI.create("http://localhost?query=" + query);
+ }
+
+ @Test
+ public void test_json_log_entry() throws Exception {
+ AccessLogEntry entry = newAccessLogEntry("test");
+
+ String expectedOutput =
+ "{\"ip\":\"152.200.54.243\"," +
+ "\"time\":920880005.023," +
+ "\"duration\":0.122," +
+ "\"responsesize\":9875," +
+ "\"code\":200," +
+ "\"method\":\"GET\"," +
+ "\"uri\":\"?query=test\"," +
+ "\"version\":\"HTTP/1.1\"," +
+ "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
+ "\"host\":\"localhost\"," +
+ "\"scheme\":null," +
+ "\"localport\":0," +
+ "\"search\":{" +
+ "\"totalhits\":1234," +
+ "\"hits\":0," +
+ "\"coverage\":{\"coverage\":100,\"documents\":100}" +
+ "}" +
+ "}";
+
+ assertEquals(expectedOutput, new JSONFormatter(entry).format());
+ }
+
+ @Test
+ public void test_json_of_trace() throws IOException {
+ TraceNode root = new TraceNode("root", 7);
+ AccessLogEntry entry = newAccessLogEntry("test");
+ entry.setTrace(root);
+
+ String expectedOutput =
+ "{\"ip\":\"152.200.54.243\"," +
+ "\"time\":920880005.023," +
+ "\"duration\":0.122," +
+ "\"responsesize\":9875," +
+ "\"code\":200," +
+ "\"method\":\"GET\"," +
+ "\"uri\":\"?query=test\"," +
+ "\"version\":\"HTTP/1.1\"," +
+ "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
+ "\"host\":\"localhost\"," +
+ "\"scheme\":null," +
+ "\"localport\":0," +
+ "\"trace\":{\"timestamp\":0,\"message\":\"root\"}," +
+ "\"search\":{" +
+ "\"totalhits\":1234," +
+ "\"hits\":0," +
+ "\"coverage\":{\"coverage\":100,\"documents\":100}" +
+ "}" +
+ "}";
+
+ assertEquals(expectedOutput, new JSONFormatter(entry).format());
+ }
+
+ @Test
+ public void test_with_keyvalues() {
+ AccessLogEntry entry = newAccessLogEntry("test");
+ entry.addKeyValue("singlevalue", "value1");
+ entry.addKeyValue("multivalue", "value2");
+ entry.addKeyValue("multivalue", "value3");
+
+ String expectedOutput =
+ "{\"ip\":\"152.200.54.243\"," +
+ "\"time\":920880005.023," +
+ "\"duration\":0.122," +
+ "\"responsesize\":9875," +
+ "\"code\":200," +
+ "\"method\":\"GET\"," +
+ "\"uri\":\"?query=test\"," +
+ "\"version\":\"HTTP/1.1\"," +
+ "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
+ "\"host\":\"localhost\"," +
+ "\"scheme\":null," +
+ "\"localport\":0," +
+ "\"search\":{" +
+ "\"totalhits\":1234," +
+ "\"hits\":0," +
+ "\"coverage\":{\"coverage\":100,\"documents\":100}" +
+ "}," +
+ "\"attributes\":{" +
+ "\"singlevalue\":\"value1\"," +
+ "\"multivalue\":[\"value2\",\"value3\"]}" +
+ "}";
+
+ assertEquals(expectedOutput, new JSONFormatter(entry).format());
+
+ }
+
+ @Test
+ public void test_with_remoteaddrport() throws Exception {
+ AccessLogEntry entry = newAccessLogEntry("test");
+
+ // First test with only remote address and not port set
+ entry.setRemoteAddress("FE80:0000:0000:0000:0202:B3FF:FE1E:8329");
+
+ String expectedOutput =
+ "{\"ip\":\"152.200.54.243\"," +
+ "\"time\":920880005.023," +
+ "\"duration\":0.122," +
+ "\"responsesize\":9875," +
+ "\"code\":200," +
+ "\"method\":\"GET\"," +
+ "\"uri\":\"?query=test\"," +
+ "\"version\":\"HTTP/1.1\"," +
+ "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
+ "\"host\":\"localhost\"," +
+ "\"scheme\":null," +
+ "\"localport\":0," +
+ "\"remoteaddr\":\"FE80:0000:0000:0000:0202:B3FF:FE1E:8329\"," +
+ "\"search\":{" +
+ "\"totalhits\":1234," +
+ "\"hits\":0," +
+ "\"coverage\":{\"coverage\":100,\"documents\":100}" +
+ "}" +
+ "}";
+
+ assertEquals(expectedOutput, new JSONFormatter(entry).format());
+
+ // Add remote port and verify
+ entry.setRemotePort(1234);
+
+ expectedOutput =
+ "{\"ip\":\"152.200.54.243\"," +
+ "\"time\":920880005.023," +
+ "\"duration\":0.122," +
+ "\"responsesize\":9875," +
+ "\"code\":200," +
+ "\"method\":\"GET\"," +
+ "\"uri\":\"?query=test\"," +
+ "\"version\":\"HTTP/1.1\"," +
+ "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
+ "\"host\":\"localhost\"," +
+ "\"scheme\":null," +
+ "\"localport\":0," +
+ "\"remoteaddr\":\"FE80:0000:0000:0000:0202:B3FF:FE1E:8329\"," +
+ "\"remoteport\":1234," +
+ "\"search\":{" +
+ "\"totalhits\":1234," +
+ "\"hits\":0," +
+ "\"coverage\":{\"coverage\":100,\"documents\":100}" +
+ "}" +
+ "}";
+
+ assertEquals(expectedOutput, new JSONFormatter(entry).format());
+ }
+
+ @Test
+ public void test_remote_address_same_as_ip_address() throws Exception {
+ AccessLogEntry entry = newAccessLogEntry("test");
+ AccessLogEntry entrywithremote = newAccessLogEntry("test");
+ entrywithremote.setRemoteAddress(entry.getIpV4Address());
+
+ assertEquals(new JSONFormatter(entry).format(), new JSONFormatter(entrywithremote).format());
+ }
+
+ @Test
+ public void test_useragent_with_quotes() {
+ final AccessLogEntry entry = new AccessLogEntry();
+ entry.setRawQuery("query=test");
+ entry.setRawPath("");
+ entry.setIpV4Address(ipAddress);
+ entry.setHttpMethod("GET");
+ entry.setHttpVersion("HTTP/1.1");
+ entry.setUserAgent("Mozilla/4.05 [en] (Win95; I; \"Best Browser Ever\")");
+ entry.setHitCounts(new HitCounts(0, 10, 1234, 0, 10, new Coverage(100,200,200,0)));
+ entry.setHostString("localhost");
+ entry.setStatusCode(200);
+ entry.setTimeStamp(920880005023L);
+ entry.setDurationBetweenRequestResponse(122);
+ entry.setReturnedContentSize(9875);
+
+ String expectedOutput =
+ "{\"ip\":\"152.200.54.243\"," +
+ "\"time\":920880005.023," +
+ "\"duration\":0.122," +
+ "\"responsesize\":9875," +
+ "\"code\":200," +
+ "\"method\":\"GET\"," +
+ "\"uri\":\"?query=test\"," +
+ "\"version\":\"HTTP/1.1\"," +
+ "\"agent\":\"Mozilla/4.05 [en] (Win95; I; \\\"Best Browser Ever\\\")\"," +
+ "\"host\":\"localhost\"," +
+ "\"scheme\":null," +
+ "\"localport\":0," +
+ "\"search\":{" +
+ "\"totalhits\":1234," +
+ "\"hits\":0," +
+ "\"coverage\":{\"coverage\":50,\"documents\":100,\"degraded\":{\"non-ideal-state\":true}}" +
+ "}" +
+ "}";
+
+ assertEquals(expectedOutput, new JSONFormatter(entry).format());
+ }
+
+ private void verifyCoverage(String coverage, AccessLogEntry entry) {
+ assertEquals("{\"ip\":\"152.200.54.243\"," +
+ "\"time\":920880005.023," +
+ "\"duration\":0.122," +
+ "\"responsesize\":9875," +
+ "\"code\":200," +
+ "\"method\":\"GET\"," +
+ "\"uri\":\"?query=test\"," +
+ "\"version\":\"HTTP/1.1\"," +
+ "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
+ "\"host\":\"localhost\"," +
+ "\"scheme\":null," +
+ "\"localport\":0," +
+ "\"search\":{" +
+ "\"totalhits\":1234," +
+ "\"hits\":0," +
+ coverage +
+ "}" +
+ "}", new JSONFormatter(entry).format());
+ }
+
+ @Test
+ public void test_with_coverage_degradation() {
+ verifyCoverage("\"coverage\":{\"coverage\":50,\"documents\":100,\"degraded\":{\"non-ideal-state\":true}}",
+ newAccessLogEntry("test", new Coverage(100,200,200,0)));
+ verifyCoverage("\"coverage\":{\"coverage\":50,\"documents\":100,\"degraded\":{\"match-phase\":true}}",
+ newAccessLogEntry("test", new Coverage(100,200,200,1)));
+ verifyCoverage("\"coverage\":{\"coverage\":50,\"documents\":100,\"degraded\":{\"timeout\":true}}",
+ newAccessLogEntry("test", new Coverage(100,200,200,2)));
+ verifyCoverage("\"coverage\":{\"coverage\":50,\"documents\":100,\"degraded\":{\"adaptive-timeout\":true}}",
+ newAccessLogEntry("test", new Coverage(100,200,200,4)));
+ }
+}
diff --git a/jdisc_http_service/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java b/jdisc_http_service/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java
new file mode 100644
index 00000000000..bc7257b1ca9
--- /dev/null
+++ b/jdisc_http_service/src/test/java/com/yahoo/container/logging/LogFileHandlerTestCase.java
@@ -0,0 +1,187 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import com.yahoo.io.IOUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+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;
+import java.util.logging.Formatter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.SimpleFormatter;
+import java.util.zip.GZIPInputStream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Bob Travis
+ * @author bjorncs
+ */
+public class LogFileHandlerTestCase {
+
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+ @Test
+ public void testIt() throws IOException {
+ File root = temporaryFolder.newFolder("logfilehandlertest");
+
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern(root.getAbsolutePath() + "/logfilehandlertest.%Y%m%d%H%M%S");
+ h.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");
+ }
+ } );
+ long now = System.currentTimeMillis();
+ long millisPerDay = 60*60*24*1000;
+ long tomorrowDays = (now / millisPerDay) +1;
+ long tomorrowMillis = tomorrowDays * millisPerDay;
+ assertThat(tomorrowMillis).isEqualTo(h.getNextRotationTime(now));
+ long[] rTimes = {1000, 2000, 10000};
+ h.setRotationTimes(rTimes);
+ assertThat(tomorrowMillis+1000).isEqualTo(h.getNextRotationTime(tomorrowMillis));
+ assertThat(tomorrowMillis+10000).isEqualTo(h.getNextRotationTime(tomorrowMillis+3000));
+ LogRecord lr = new LogRecord(Level.INFO, "test");
+ h.publish(lr);
+ h.publish(new LogRecord(Level.INFO, "another test"));
+ h.rotateNow();
+ h.publish(lr);
+ h.flush();
+ h.shutdown();
+ }
+
+ @Test
+ public void testSimpleLogging() throws IOException {
+ File logFile = temporaryFolder.newFile("testLogFileG1.txt");
+
+ //create logfilehandler
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern(logFile.getAbsolutePath());
+ h.setFormatter(new SimpleFormatter());
+ h.setRotationTimes("0 5 ...");
+
+ //write log
+ LogRecord lr = new LogRecord(Level.INFO, "testDeleteFileFirst1");
+ h.publish(lr);
+ h.flush();
+ h.shutdown();
+ }
+
+ @Test
+ public void testDeleteFileDuringLogging() throws IOException {
+ File logFile = temporaryFolder.newFile("testLogFileG2.txt");
+
+ //create logfilehandler
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern(logFile.getAbsolutePath());
+ h.setFormatter(new SimpleFormatter());
+ h.setRotationTimes("0 5 ...");
+
+ //write log
+ LogRecord lr = new LogRecord(Level.INFO, "testDeleteFileDuringLogging1");
+ h.publish(lr);
+ h.flush();
+
+ //delete log file
+ logFile.delete();
+
+ //write log again
+ lr = new LogRecord(Level.INFO, "testDeleteFileDuringLogging2");
+ h.publish(lr);
+ h.flush();
+ h.shutdown();
+ }
+
+ @Test(timeout = /*5 minutes*/300_000)
+ public void testSymlink() throws IOException, InterruptedException {
+ File root = temporaryFolder.newFolder("testlogforsymlinkchecking");
+ 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");
+ }
+ } );
+ 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);
+ 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);
+ secondFileLength = Files.size(Paths.get(secondFileName));
+ } while (secondFileLength != expectedSecondFileLength);
+
+ long symlinkFileLength = Files.size(root.toPath().resolve("symlink"));
+ assertThat(symlinkFileLength).isEqualTo(expectedSecondFileLength);
+ handler.shutdown();
+ }
+
+ @Test
+ public void testcompression() throws InterruptedException, IOException {
+ File root = temporaryFolder.newFolder("testcompression");
+
+ LogFileHandler h = new LogFileHandler(true);
+ h.setFilePattern(root.getAbsolutePath() + "/logfilehandlertest.%Y%m%d%H%M%S%s");
+ h.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");
+ }
+ } );
+ int logEntries = 10000;
+ for (int i = 0; i < logEntries; i++) {
+ LogRecord lr = new LogRecord(Level.INFO, "test");
+ h.publish(lr);
+ }
+ h.waitDrained();
+ String f1 = h.getFileName();
+ assertThat(f1).startsWith(root.getAbsolutePath() + "/logfilehandlertest.");
+ File uncompressed = new File(f1);
+ File compressed = new File(f1 + ".gz");
+ assertThat(uncompressed).exists();
+ assertThat(compressed).doesNotExist();
+ String content = IOUtils.readFile(uncompressed);
+ assertThat(content).hasLineCount(logEntries);
+ h.rotateNow();
+ while (uncompressed.exists()) {
+ Thread.sleep(1);
+ }
+ assertThat(compressed).exists();
+ String unzipped = IOUtils.readAll(new InputStreamReader(new GZIPInputStream(new FileInputStream(compressed))));
+ assertThat(content).isEqualTo(unzipped);
+ h.shutdown();
+ }
+
+}
diff --git a/jdisc_http_service/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java b/jdisc_http_service/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java
new file mode 100644
index 00000000000..ecacf95d100
--- /dev/null
+++ b/jdisc_http_service/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java
@@ -0,0 +1,27 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging.test;
+
+import com.yahoo.container.logging.LogFormatter;
+import org.junit.Test;
+
+import java.util.Date;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Bob Travis
+ */
+public class LogFormatterTestCase {
+
+ @Test
+ public void testIt() {
+ java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("UTC"));
+ @SuppressWarnings("deprecation")
+ long time = new Date(103,7,25,13,30,35).getTime();
+ String result = LogFormatter.insertDate("test%Y%m%d%H%M%S%x",time);
+ assertEquals("test20030825133035Aug",result);
+ result = LogFormatter.insertDate("test%s%T",time);
+ assertEquals("test000"+time, result);
+ }
+
+}