-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
158 lines (146 loc) · 4.6 KB
/
server_test.go
File metadata and controls
158 lines (146 loc) · 4.6 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
pb "KVStore/API"
"context"
"fmt"
"google.golang.org/grpc"
"testing"
)
const address = "127.0.0.1:50051"
func TestWal(t *testing.T) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
c := pb.NewStoreServiceClient(conn)
fmt.Println("-------------------------------------------------------")
fmt.Println("test for WAL")
fmt.Println("school age country,the keys,had been inserted")
fmt.Println("Get school")
GetResult, err := c.Get(context.Background(), &pb.GetRequest{Key: "school"})
if err != nil {
t.Log("should return the value of school")
} else {
fmt.Println(GetResult.Value)
}
fmt.Println("Get age")
GetResult, err = c.Get(context.Background(), &pb.GetRequest{Key: "age"})
if err != nil {
t.Log("should return the value of age")
} else {
fmt.Println(GetResult.Value)
}
fmt.Println("Get country")
GetResult, err = c.Get(context.Background(), &pb.GetRequest{Key: "country"})
if err != nil {
t.Log("should return the value of country")
} else {
fmt.Println(GetResult.Value)
}
}
func TestGet(t *testing.T) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
t.Log("can not access service")
}
defer conn.Close()
c := pb.NewStoreServiceClient(conn)
fmt.Println("-------------------------------------------------------")
fmt.Println("test for Get")
fmt.Println("Get notexit")
_, err = c.Get(context.Background(), &pb.GetRequest{Key: "notexit"})
if err == nil {
t.Log("should retrun error for the key not exist")
}
fmt.Println("Get akeyverylong")
_, err = c.Get(context.Background(), &pb.GetRequest{Key: "akeyverylong"})
if err == nil {
t.Log("should retrun error for the key is too long")
}
}
func TestPut(t *testing.T) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
c := pb.NewStoreServiceClient(conn)
fmt.Println("-------------------------------------------------------")
fmt.Println("test for PUT")
fmt.Println("PUT how fine")
_, err = c.Put(context.Background(), &pb.PutRequest{Key: "how", Value: "fine"})
if err == nil {
fmt.Println("insert how success")
} else {
fmt.Println("insert how fail")
}
fmt.Println("Get how")
GetResult, err := c.Get(context.Background(), &pb.GetRequest{Key: "how"})
if err != nil {
t.Log("should retrun the value of how")
} else {
fmt.Println(GetResult.Value)
}
fmt.Println(`PUT longval sdaasdfjsdddddddddddaaggdsadfhhhhhhhhhhhhhhhhjsdfakjjjjjkjsdfajkasvs
sfh3et22222gdddddddddddddddddddddddddddddddddddddddddascfeeeeeeeeeeeeeeeeedzdxvfeasfcweasdfcas
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddasddddddddddddddddddddasdasdaADASDSAD
sadfannngoooooooooooooooooooooooooddddddddddddddddddddddddddfi`)
_, err = c.Put(context.Background(), &pb.PutRequest{Key: "longval", Value: `sdaasdfjsdddddddddddaaggdsadfhhhhhhhhhhhhhhhhjsdfakjjjjjkjsdfajkasvs
sfh3et22222gdddddddddddddddddddddddddddddddddddddddddascfeeeeeeeeeeeeeeeeedzdxvfeasfcweasdfcas
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddasddddddddddddddddddddasdasdaADASDSAD
sadfannngoooooooooooooooooooooooooddddddddddddddddddddddddddfi`})
if err == nil {
t.Log("should retrun error for value is too long")
}
}
func TestDelete(t *testing.T) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
c := pb.NewStoreServiceClient(conn)
fmt.Println("-------------------------------------------------------")
fmt.Println("test for delete")
fmt.Println("delete how")
_, err = c.Delete(context.Background(), &pb.DeleteRequest{Key: "how"})
if err == nil {
fmt.Println("delete how success")
} else {
fmt.Println("delete how fail")
}
fmt.Println("Get how")
_, err = c.Get(context.Background(), &pb.GetRequest{Key: "how"})
if err != nil {
fmt.Println(err)
} else {
t.Log("should retrun error for the key had been delete")
}
}
func TestScan(t *testing.T) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
c := pb.NewStoreServiceClient(conn)
fmt.Println("-------------------------------------------------------")
fmt.Println("test for scan")
fmt.Println("scan 0 3")
scanResult, err := c.Scan(context.Background(), &pb.ScanRequest{Start: 0, Limit: 3})
if err != nil {
fmt.Println(err)
} else {
for _, str := range scanResult.Result {
fmt.Println(str)
}
}
fmt.Println("scan -1 3")
scanResult, err = c.Scan(context.Background(), &pb.ScanRequest{Start: -1, Limit: 3})
if err != nil {
fmt.Println(err)
} else {
t.Log("should return error for start is -1")
}
}