|
| 1 | +package redis.integration; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.OutputStream; |
| 7 | +import java.net.ServerSocket; |
| 8 | +import java.net.Socket; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import org.junit.jupiter.api.AfterAll; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import redis.Main; |
| 14 | + |
| 15 | +class ServerIntegrationTest { |
| 16 | + |
| 17 | + private static int port; |
| 18 | + private static Thread serverThread; |
| 19 | + |
| 20 | + @BeforeAll |
| 21 | + static void startServer() throws Exception { |
| 22 | + try (var ss = new ServerSocket(0)) { |
| 23 | + port = ss.getLocalPort(); |
| 24 | + } |
| 25 | + |
| 26 | + serverThread = Thread.ofVirtual().start(() -> { |
| 27 | + try { |
| 28 | + Main.main(new String[] { "--port", String.valueOf(port) }); |
| 29 | + } catch (Exception e) { |
| 30 | + // server shut down |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + Thread.sleep(500); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterAll |
| 38 | + static void stopServer() { |
| 39 | + serverThread.interrupt(); |
| 40 | + } |
| 41 | + |
| 42 | + private String sendCommand(String... parts) throws Exception { |
| 43 | + try (var socket = new Socket("localhost", port)) { |
| 44 | + OutputStream out = socket.getOutputStream(); |
| 45 | + InputStream in = socket.getInputStream(); |
| 46 | + |
| 47 | + StringBuilder resp = new StringBuilder(); |
| 48 | + resp.append("*").append(parts.length).append("\r\n"); |
| 49 | + for (String part : parts) { |
| 50 | + resp.append("$").append(part.length()).append("\r\n"); |
| 51 | + resp.append(part).append("\r\n"); |
| 52 | + } |
| 53 | + |
| 54 | + out.write(resp.toString().getBytes(StandardCharsets.UTF_8)); |
| 55 | + out.flush(); |
| 56 | + |
| 57 | + Thread.sleep(100); |
| 58 | + byte[] buf = new byte[4096]; |
| 59 | + int len = in.read(buf); |
| 60 | + return new String(buf, 0, len, StandardCharsets.UTF_8); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void pingReturnsPong() throws Exception { |
| 66 | + String response = sendCommand("PING"); |
| 67 | + assertThat(response).contains("PONG"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void echoReturnsArgument() throws Exception { |
| 72 | + String response = sendCommand("ECHO", "hello"); |
| 73 | + assertThat(response).contains("hello"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void setAndGet() throws Exception { |
| 78 | + sendCommand("SET", "testkey", "testvalue"); |
| 79 | + String response = sendCommand("GET", "testkey"); |
| 80 | + assertThat(response).contains("testvalue"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void getMissingKeyReturnsNil() throws Exception { |
| 85 | + String response = sendCommand("GET", "nonexistent_key_xyz"); |
| 86 | + assertThat(response).contains("$-1"); |
| 87 | + } |
| 88 | +} |
0 commit comments