-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpIntegrationTest.java
More file actions
96 lines (84 loc) · 3 KB
/
Copy pathTcpIntegrationTest.java
File metadata and controls
96 lines (84 loc) · 3 KB
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
package com.vaultdb;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
class TcpIntegrationTest {
private VaultDBServer server;
private ExecutorService serverThread;
private Path aofPath;
private int port;
@BeforeEach
void setUp() throws Exception {
aofPath = Files.createTempFile("vaultdb-tcp-", ".aof");
try (ServerSocket probe = new ServerSocket(0)) {
port = probe.getLocalPort();
}
server = new VaultDBServer(port, aofPath, false);
serverThread = Executors.newSingleThreadExecutor();
serverThread.submit(() -> {
try {
server.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Thread.sleep(300);
}
@AfterEach
void tearDown() throws Exception {
server.close();
serverThread.shutdownNow();
serverThread.awaitTermination(2, TimeUnit.SECONDS);
Files.deleteIfExists(aofPath);
}
@Test
void redisCliStylePingAndSetGet() throws Exception {
try (Socket socket = new Socket("127.0.0.1", port)) {
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
sendCommand(out, "PING");
assertEquals("+PONG\r\n", readLine(in));
sendCommand(out, "SET", "cli", "works");
assertEquals("+OK\r\n", readLine(in));
sendCommand(out, "GET", "cli");
assertEquals("$5\r\n", readLine(in));
assertEquals("works\r\n", readLine(in));
}
}
private static void sendCommand(OutputStream out, String... args) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append('*').append(args.length).append("\r\n");
for (String arg : args) {
sb.append('$').append(arg.getBytes(StandardCharsets.UTF_8).length).append("\r\n");
sb.append(arg).append("\r\n");
}
out.write(sb.toString().getBytes(StandardCharsets.UTF_8));
out.flush();
}
private static String readLine(InputStream in) throws IOException {
StringBuilder sb = new StringBuilder();
int previous = -1;
int current;
while ((current = in.read()) != -1) {
if (previous == '\r' && current == '\n') {
sb.setLength(sb.length() - 1);
return sb + "\r\n";
}
sb.append((char) current);
previous = current;
}
throw new IOException("Stream closed");
}
}