File: example_kv_test.go

package info (click to toggle)
etcd 3.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,688 kB
  • sloc: sh: 3,222; makefile: 527
file content (334 lines) | stat: -rw-r--r-- 7,759 bytes parent folder | download | duplicates (5)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package clientv3_test

import (
	"context"
	"fmt"
	"log"

	"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
	"go.etcd.io/etcd/client/v3"
)

func mockKV_put() {}

func ExampleKV_put() {
	forUnitTestsRunInMockedContext(mockKV_put, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		_, err = cli.Put(ctx, "sample_key", "sample_value")
		cancel()
		if err != nil {
			log.Fatal(err)
		}
	})
	// Output:
}

func mockKV_putErrorHandling() {
	fmt.Println("client-side error: etcdserver: key is not provided")
}

func ExampleKV_putErrorHandling() {
	forUnitTestsRunInMockedContext(mockKV_putErrorHandling, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		_, err = cli.Put(ctx, "", "sample_value")
		cancel()
		if err != nil {
			switch err {
			case context.Canceled:
				fmt.Printf("ctx is canceled by another routine: %v\n", err)
			case context.DeadlineExceeded:
				fmt.Printf("ctx is attached with a deadline is exceeded: %v\n", err)
			case rpctypes.ErrEmptyKey:
				fmt.Printf("client-side error: %v\n", err)
			default:
				fmt.Printf("bad cluster endpoints, which are not etcd servers: %v\n", err)
			}
		}
	})
	// Output: client-side error: etcdserver: key is not provided
}

func mockKV_get() {
	fmt.Println("foo : bar")
}

func ExampleKV_get() {
	forUnitTestsRunInMockedContext(mockKV_get, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		_, err = cli.Put(context.TODO(), "foo", "bar")
		if err != nil {
			log.Fatal(err)
		}

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		resp, err := cli.Get(ctx, "foo")
		cancel()
		if err != nil {
			log.Fatal(err)
		}
		for _, ev := range resp.Kvs {
			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
		}
	})
	// Output: foo : bar
}

func mockKV_getWithRev() {
	fmt.Println("foo : bar1")
}

func ExampleKV_getWithRev() {
	forUnitTestsRunInMockedContext(mockKV_getWithRev, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		presp, err := cli.Put(context.TODO(), "foo", "bar1")
		if err != nil {
			log.Fatal(err)
		}
		_, err = cli.Put(context.TODO(), "foo", "bar2")
		if err != nil {
			log.Fatal(err)
		}

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		resp, err := cli.Get(ctx, "foo", clientv3.WithRev(presp.Header.Revision))
		cancel()
		if err != nil {
			log.Fatal(err)
		}
		for _, ev := range resp.Kvs {
			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
		}
	})
	// Output: foo : bar1
}

func mockKV_getSortedPrefix() {
	fmt.Println(`key_2 : value`)
	fmt.Println(`key_1 : value`)
	fmt.Println(`key_0 : value`)
}

func ExampleKV_getSortedPrefix() {
	forUnitTestsRunInMockedContext(mockKV_getSortedPrefix, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		for i := range make([]int, 3) {
			ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
			_, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value")
			cancel()
			if err != nil {
				log.Fatal(err)
			}
		}

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
		cancel()
		if err != nil {
			log.Fatal(err)
		}
		for _, ev := range resp.Kvs {
			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
		}
	})
	// Output:
	// key_2 : value
	// key_1 : value
	// key_0 : value
}

func mockKV_delete() {
	fmt.Println("Deleted all keys: true")
}

func ExampleKV_delete() {
	forUnitTestsRunInMockedContext(mockKV_delete, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		defer cancel()

		// count keys about to be deleted
		gresp, err := cli.Get(ctx, "key", clientv3.WithPrefix())
		if err != nil {
			log.Fatal(err)
		}

		// delete the keys
		dresp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
		if err != nil {
			log.Fatal(err)
		}

		fmt.Println("Deleted all keys:", int64(len(gresp.Kvs)) == dresp.Deleted)
	})
	// Output:
	// Deleted all keys: true
}

func mockKV_compact() {}

func ExampleKV_compact() {
	forUnitTestsRunInMockedContext(mockKV_compact, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		resp, err := cli.Get(ctx, "foo")
		cancel()
		if err != nil {
			log.Fatal(err)
		}
		compRev := resp.Header.Revision // specify compact revision of your choice

		ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
		_, err = cli.Compact(ctx, compRev)
		cancel()
		if err != nil {
			log.Fatal(err)
		}
	})
	// Output:
}

func mockKV_txn() {
	fmt.Println("key : XYZ")
}

func ExampleKV_txn() {
	forUnitTestsRunInMockedContext(mockKV_txn, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		kvc := clientv3.NewKV(cli)

		_, err = kvc.Put(context.TODO(), "key", "xyz")
		if err != nil {
			log.Fatal(err)
		}

		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
		_, err = kvc.Txn(ctx).
			// txn value comparisons are lexical
			If(clientv3.Compare(clientv3.Value("key"), ">", "abc")).
			// the "Then" runs, since "xyz" > "abc"
			Then(clientv3.OpPut("key", "XYZ")).
			// the "Else" does not run
			Else(clientv3.OpPut("key", "ABC")).
			Commit()
		cancel()
		if err != nil {
			log.Fatal(err)
		}

		gresp, err := kvc.Get(context.TODO(), "key")
		if err != nil {
			log.Fatal(err)
		}
		for _, ev := range gresp.Kvs {
			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
		}
	})
	// Output: key : XYZ
}

func mockKV_do() {}

func ExampleKV_do() {
	forUnitTestsRunInMockedContext(mockKV_do, func() {
		cli, err := clientv3.New(clientv3.Config{
			Endpoints:   exampleEndpoints(),
			DialTimeout: dialTimeout,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer cli.Close()

		ops := []clientv3.Op{
			clientv3.OpPut("put-key", "123"),
			clientv3.OpGet("put-key"),
			clientv3.OpPut("put-key", "456")}

		for _, op := range ops {
			if _, err := cli.Do(context.TODO(), op); err != nil {
				log.Fatal(err)
			}
		}
	})
	// Output:
}