-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
128 lines (107 loc) · 2.76 KB
/
main_test.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"bufio"
"net"
"os"
"strings"
"testing"
"time"
)
func TestMain(m *testing.M) {
go main() // start the server
time.Sleep(2 * time.Second) // give the server some time to start
// run the tests
code := m.Run()
// stop the server
os.Exit(code)
}
func TestConnection(t *testing.T) {
// try to connect to the server
conn, err := net.Dial("tcp", "0.0.0.0:6379")
if err != nil {
t.Fatalf("Could not connect to server: %v", err)
}
// close the connection
err = conn.Close()
if err != nil {
t.Fatalf("Could not close the connection: %v", err)
}
}
func TestPing(t *testing.T) {
// try to connect to the server
conn, err := net.Dial("tcp", "0.0.0.0:6379")
if err != nil {
t.Fatalf("Could not connect to server: %v", err)
}
defer conn.Close()
// send PING command
_, err = conn.Write([]byte("*1\r\n$4\r\nPING\r\n"))
if err != nil {
t.Fatalf("Could not write to connection: %v", err)
}
// read response
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
t.Fatalf("Could not read from connection: %v", err)
}
// check response
if response != "+PONG\r\n" {
t.Fatalf("Unexpected response: %v", response)
}
}
func TestSet(t *testing.T) {
// try to connect to the server
conn, err := net.Dial("tcp", "0.0.0.0:6379")
if err != nil {
t.Fatalf("Could not connect to server: %v", err)
}
defer conn.Close()
// send SET command
_, err = conn.Write([]byte("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n"))
if err != nil {
t.Fatalf("Could not write to connection: %v", err)
}
// read response
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
t.Fatalf("Could not read from connection: %v", err)
}
// check response
if response != "+OK\r\n" {
t.Fatalf("Unexpected response: %v", response)
}
}
func TestGet(t *testing.T) {
// try to connect to the server
conn, err := net.Dial("tcp", "0.0.0.0:6379")
if err != nil {
t.Fatalf("Could not connect to server: %v", err)
}
defer conn.Close()
// send GET command
_, err = conn.Write([]byte("*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n"))
if err != nil {
t.Fatalf("Could not write to connection: %v", err)
}
// read response
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
t.Fatalf("Could not read from connection: %v", err)
}
// if the response contains multiple lines, read the next line
if strings.HasPrefix(response, "$") {
nextLine, err := reader.ReadString('\n')
if err != nil {
t.Fatalf("Could not read from connection: %v", err)
}
response += nextLine
}
// check response
expectedResponse := "$5\r\nvalue\r\n"
if response != expectedResponse {
t.Fatalf("Unexpected response: got %v want %v", response, expectedResponse)
}
}