blob: abd3b27b70b10377f26caa711b2f90284082df29 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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();
}
}
}
|