forked from yaojingguo/geekbang-zk-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv_test.go
286 lines (248 loc) · 6.7 KB
/
kv_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package etcd_code
import (
"context"
"fmt"
v3 "go.etcd.io/etcd/clientv3"
"testing"
)
func printGetResponse(r *v3.GetResponse) {
fmt.Printf("response header: %q\n", r.Header)
fmt.Printf("kvs: %v\n", r.Kvs)
fmt.Printf("more: %t\n", r.More)
fmt.Printf("count: %d\n", r.Count)
}
// Run beforehand:
// etcdctl put TestGet 1
func TestGet(t *testing.T) {
cli := newClient(t)
defer cli.Close()
key := "TestGet"
response, err := cli.Get(context.Background(), key)
if err != nil {
t.Fatal(err)
}
printGetResponse(response)
}
// Run beforehand:
// etcdctl put TestGetWithPrefix 1
// etcdctl put TestGetWithPrefix2 2
func TestGetWithPrefix(t *testing.T) {
cli := newClient(t)
defer cli.Close()
keyPrefix := "TestGetWithPrefix"
response, err := cli.Get(context.Background(), keyPrefix, v3.WithPrefix())
if err != nil {
t.Fatal(err)
}
printGetResponse(response)
}
// Run beforehand:
// etcdctl put TestGetWithRange 1
// etcdctl put TestGetWithRange2 2
func TestGetWithRange(t *testing.T) {
cli := newClient(t)
defer cli.Close()
response, err := cli.Get(context.Background(), "TestGetWithRange", v3.WithRange("TestGetWithRange2\x00"))
if err != nil {
t.Fatal(err)
}
printGetResponse(response)
}
// Run beforehand:
// etcdctl put TestGetWithSerializable 1
func TestGetWithSerializable(t *testing.T) {
cli := newClient(t)
defer cli.Close()
key := "TestGetWithSerializable"
response, err := cli.Get(context.Background(), key, v3.WithSerializable())
if err != nil {
t.Fatal(err)
}
printGetResponse(response)
}
func TestPut(t *testing.T) {
cli := newClient(t)
defer cli.Close()
key := "TestPut"
response, err := cli.Put(context.Background(), key, "1", v3.WithPrevKV())
if err != nil {
t.Fatal(err)
}
fmt.Printf("header: %q\n", response.Header)
fmt.Printf("prev_kv: %q\n", response.PrevKv)
}
// Run beforehand:
// etcdctl put TestDelete 1
func TestDelete(t *testing.T) {
cli := newClient(t)
defer cli.Close()
key := "TestDelete"
response, err := cli.Delete(context.Background(), key)
if err != nil {
t.Fatal(err)
}
fmt.Printf("header: %v\n", response.Header)
fmt.Printf("deleted: %d\n", response.Deleted)
fmt.Printf("pre_kvs: %v\n", response.PrevKvs)
}
// Run beforehand:
// etcdctl put TestTxn 1
func TestTxn(t *testing.T) {
cli := newClient(t)
defer cli.Close()
key := "TestTxn"
thenValue := "then"
ctx := context.Background()
_, err := cli.Txn(ctx).
If(v3.Compare(v3.Value(key), "<", "2")).
Then(v3.OpPut(key, thenValue)).
Else(v3.OpPut(key, "else")).
Commit()
if err != nil {
t.Fatal(err)
}
// Return "then"
gresp, err := cli.Get(ctx, key)
if err != nil {
t.Fatal(err)
}
if l := len(gresp.Kvs); l != 1 {
t.Errorf("expeected 1 key-value, but got %d key-values\n", l)
}
if value := string(gresp.Kvs[0].Value); value != "then" {
t.Errorf("expected value %s, but got %s\n", thenValue, value)
}
for _, ev := range gresp.Kvs {
fmt.Printf("%s: %s\n", ev.Key, ev.Value)
}
//delete(cli, key, t)
}
func TestTxnOpOrder(t *testing.T) {
cli := newClient(t)
defer cli.Close()
key := "TestTxnOpOrder"
ctx := context.Background()
// If CreateRevision is 0, it means that the key does not exist.
cmp := v3.Compare(v3.CreateRevision(key), "=", 0)
put := v3.OpPut(key, "1")
get := v3.OpGet(key)
// Get Put
resp, err := cli.Txn(ctx).If(cmp).Then(get, put).Commit()
if err != nil {
t.Fatal(err)
}
printTxnResponse(resp)
if l := len(resp.Responses[0].GetResponseRange().Kvs); l != 0 {
t.Errorf("expeected 0 key-value, but got %d key-values\n", l)
}
delete(ctx, cli, key, t)
// Put Get
resp, err = cli.Txn(ctx).If(cmp).Then(put, get).Commit()
if err != nil {
t.Fatal(err)
}
printTxnResponse(resp)
if l := len(resp.Responses[1].GetResponseRange().Kvs); l != 1 {
t.Errorf("expeected 1 key-value, but got %d key-values\n", l)
}
delete(ctx, cli, key, t)
}
func TestTxnMultis(t *testing.T) {
cli := newClient(t)
defer cli.Close()
ctx := context.Background()
keyPrefix := "TestTxnMultis"
keys := []string{keyPrefix + "1", keyPrefix + "2"}
var cmps [2]v3.Cmp
var puts [2]v3.Op
var gets [2]v3.Op
for i := 0; i < 2; i++ {
val := string(i + 1)
cmps[i] = v3.Compare(v3.CreateRevision(keys[i]), "=", 0)
puts[i] = v3.OpPut(keys[i], val)
gets[i] = v3.OpGet(keys[i])
}
// Executes two Puts
tresp, err := cli.Txn(ctx).If(cmps[:]...).Then(puts[:]...).Else(gets[:]...).Commit()
if err != nil {
t.Fatal(t)
}
if !tresp.Succeeded {
t.Fatal("Txn should succeed, but failed")
}
// Executes two Gets
tresp, err = cli.Txn(ctx).If(cmps[:]...).Then(puts[:]...).Else(gets[:]...).Commit()
if err != nil {
t.Fatal(t)
}
if tresp.Succeeded {
t.Fatal("Txn should fail, but succeeded")
}
for i := 0; i < 2; i++ {
if count := tresp.Responses[i].GetResponseRange().Count; count != 1 {
t.Errorf("expected 1 value for key %s, but got %d value", keys[0], count)
}
}
// Execute two Deletes
tresp, err = cli.Txn(ctx).Then(v3.OpDelete(keys[0]), v3.OpDelete(keys[1])).Commit();
if err != nil {
t.Fatal(err)
}
for _, r := range tresp.Responses {
if deleted := r.GetResponseDeleteRange().Deleted; deleted != 1 {
t.Fatalf("expected to delete 1 key-value, but deleted %d key-value", deleted)
}
}
gresp, err := cli.Get(ctx, keyPrefix, v3.WithPrefix())
if gresp.Count != 0 {
t.Errorf("expected 0 key-values, bot got %d", gresp.Count)
}
}
func printTxnResponse(resp *v3.TxnResponse) {
fmt.Printf("response header: %q\n", resp.Header)
fmt.Printf("succeeded: %t\n", resp.Succeeded)
}
// version revision test
func TestVersionAndRevision(t *testing.T) {
cli := newClient(t)
defer cli.Close()
key := "TestVersionAndRevision"
ctx := context.Background()
put(ctx, cli, key, t)
get(ctx, cli, key, t)
put(ctx, cli, key, t)
get(ctx, cli, key, t)
delete(ctx, cli, key, t)
put(ctx, cli, key, t)
get(ctx, cli, key, t)
put(ctx, cli, key, t)
get(ctx, cli, key, t)
}
func put(ctx context.Context, cli *v3.Client, key string, t *testing.T) {
fmt.Println("Put")
response, err := cli.Put(ctx, key, "one", v3.WithPrevKV())
if err != nil {
t.Fatal(err)
}
fmt.Printf("put revision: %d\n", response.Header.Revision)
fmt.Printf("prev_kv: %v\n\n", response.PrevKv)
}
func get(ctx context.Context, cli *v3.Client, key string, t *testing.T) {
fmt.Println("Get")
response, err := cli.Get(ctx, key, v3.WithPrevKV())
if err != nil {
t.Fatal(err)
}
fmt.Printf("get revision: %d\n", response.Header.Revision)
fmt.Printf("kvs: %v\n\n", response.Kvs)
}
func delete(ctx context.Context, cli *v3.Client, key string, t *testing.T) {
fmt.Println("Delete")
response, err := cli.Delete(ctx, key, v3.WithPrevKV())
if err != nil {
t.Fatal(err)
}
fmt.Printf("delete revision: %d\n", response.Header.Revision)
fmt.Printf("deleted: %d\n", response.Deleted)
fmt.Printf("prev_kvs: %v\n\n", response.PrevKvs)
}