summaryrefslogtreecommitdiffstats
path: root/container-search/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/test')
-rw-r--r--container-search/src/test/java/com/yahoo/search/logging/LocalDiskLoggerTest.java50
-rw-r--r--container-search/src/test/java/com/yahoo/search/logging/SpoolerTest.java107
2 files changed, 157 insertions, 0 deletions
diff --git a/container-search/src/test/java/com/yahoo/search/logging/LocalDiskLoggerTest.java b/container-search/src/test/java/com/yahoo/search/logging/LocalDiskLoggerTest.java
new file mode 100644
index 00000000000..bf6938424d7
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/logging/LocalDiskLoggerTest.java
@@ -0,0 +1,50 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+package com.yahoo.search.logging;
+
+import com.yahoo.io.IOUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Base64;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
+public class LocalDiskLoggerTest {
+
+ @TempDir
+ Path tempDir;
+
+ @Test
+ public void testLocalDiskLogger() throws InterruptedException, IOException {
+ File logFile = tempDir.resolve("localdisklogger.log").toFile();
+
+ LocalDiskLoggerConfig.Builder builder = new LocalDiskLoggerConfig.Builder();
+ builder.path(logFile.getAbsolutePath());
+ LocalDiskLogger logger = new LocalDiskLogger(builder.build());
+
+ logger.newEntry()
+ .blob("Yo entry".getBytes())
+ .send();
+ waitForFile(logFile);
+
+ String test = IOUtils.readAll(new FileReader(logFile));
+ assertTrue(test.contains(Base64.getEncoder().encodeToString("Yo entry".getBytes())));
+ }
+
+ private void waitForFile(File file) throws InterruptedException {
+ int waitFor = 10;
+ while ( ! file.exists() && --waitFor > 0) {
+ Thread.sleep(10);
+ }
+ if ( ! file.exists()) {
+ fail("Local disk logger file was not created");
+ }
+ }
+
+}
diff --git a/container-search/src/test/java/com/yahoo/search/logging/SpoolerTest.java b/container-search/src/test/java/com/yahoo/search/logging/SpoolerTest.java
new file mode 100644
index 00000000000..abd3b27b70b
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/logging/SpoolerTest.java
@@ -0,0 +1,107 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.logging;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class SpoolerTest {
+
+ @TempDir
+ Path tempDir;
+
+ @Test
+ public void testSpoolingLogger() throws IOException {
+ Path spoolDir = tempDir.resolve("spool");
+
+ Spooler spooler = new Spooler(spoolDir);
+
+ TestLogger logger = new TestLogger(spooler);
+ assertTrue(logger.newEntry()
+ .blob("Yo entry".getBytes())
+ .send());
+ assertTrue(logger.newEntry()
+ .blob("Yo entry 2".getBytes())
+ .send());
+
+ Path readyPath = spooler.readyPath();
+ Path readyFile1 = readyPath.resolve("1");
+ waitUntilFileExists(readyFile1);
+ Path readyFile2 = readyPath.resolve("2");
+ waitUntilFileExists(readyFile2);
+
+ // Check content after being moved to ready path
+ String content = Files.readString(readyFile1);
+ assertTrue(content.contains(Base64.getEncoder().encodeToString("Yo entry".getBytes())));
+ assertTrue(Files.readString(readyFile2).contains(Base64.getEncoder().encodeToString("Yo entry 2".getBytes())));
+
+ // Process files (read, transport files)
+ logger.manualRun();
+ assertEquals(2, logger.entriesSent());
+
+ // No files in processing or ready, 2 files in successes
+ assertEquals(0, spooler.listFilesInPath(spooler.processingPath()).size());
+ assertEquals(0, spooler.listFilesInPath(readyPath).size());
+ assertEquals(2, spooler.listFilesInPath(spooler.successesPath()).size());
+ assertEquals(0, spooler.listFilesInPath(spooler.failuresPath()).size());
+ }
+
+ private void waitUntilFileExists(Path path) {
+ Instant end = Instant.now().plus(Duration.ofSeconds(1));
+ while (!path.toFile().exists() && Instant.now().isBefore(end)) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ assertTrue(path.toFile().exists());
+ }
+
+
+ private static class TestLogger extends AbstractSpoolingLogger {
+
+ private final List<LoggerEntry> entriesSent = new ArrayList<>();
+
+ public TestLogger(Spooler spooler) {
+ super(spooler);
+ }
+
+ @Override
+ void transport(LoggerEntry entry) {
+ System.out.println("Called transport()");
+ entriesSent.add(entry);
+ }
+
+ @Override
+ public void run() {
+ // Do nothing, use manualRun
+ }
+
+ @Override
+ public boolean send(LoggerEntry entry) {
+ return spooler.write(entry);
+ }
+
+ public void manualRun() {
+ super.run();
+ }
+
+ int entriesSent() {
+ return entriesSent.size();
+ }
+
+ }
+
+}