-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv_test.go
538 lines (409 loc) · 13.3 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// NOTE: These tests require NATS to be available on "localhost"
package natty
import (
"context"
"math/rand"
"time"
"github.com/nats-io/nats.go"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("KV", func() {
var (
cfg *Config
n *Natty
)
BeforeEach(func() {
var err error
cfg = NewConfig()
n, err = New(cfg)
Expect(err).To(BeNil())
Expect(n).NotTo(BeNil())
})
Describe("Get", func() {
It("should return the value for a key", func() {
bucket, key, value := NewKVSet()
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "created during kv test",
})
Expect(err).To(BeNil())
Expect(kv).NotTo(BeNil())
_, putErr := kv.Put(key, []byte(value))
Expect(putErr).To(BeNil())
data, err := n.Get(nil, bucket, key)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal([]byte(value)))
})
It("should not auto-create a bucket", func() {
data, err := n.Get(nil, "non-existent-bucket", "non-existent-key")
Expect(err).To(Equal(nats.ErrKeyNotFound))
Expect(data).To(BeNil())
kv, err := n.js.KeyValue("non-existent-bucket")
Expect(err).To(Equal(nats.ErrBucketNotFound))
Expect(kv).To(BeNil())
})
})
Describe("CreateBucket", func() {
It("should validate args before creating bucket", func() {
type Case struct {
Condition string
Bucket string
TTL time.Duration
ExpectedErr string
Replicas int
ShouldError bool
}
cases := []Case{
{Condition: "bucket name is required", Bucket: "", ExpectedErr: "bucket name cannot be empty", ShouldError: true, Replicas: 1},
{Condition: "replica must be >0", Bucket: "test", ExpectedErr: "replicaCount must be greater than 0", ShouldError: true, Replicas: 0},
{Condition: "bad bucket name", Bucket: "bad bucket name", ExpectedErr: "can only contain alphanumeric", ShouldError: true, Replicas: 1},
{Condition: "happy path", Bucket: "test", ShouldError: false, Replicas: 1},
}
for _, c := range cases {
err := n.CreateBucket(nil, c.Bucket, c.TTL, c.Replicas)
if c.ShouldError {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(c.ExpectedErr))
} else {
Expect(err).ToNot(HaveOccurred())
}
}
})
})
Describe("Refresh", func() {
It("should refresh the TTL for a key", func() {
bucket, key, value := NewKVSet()
ttl := time.Second
// Create bucket
_, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
TTL: ttl,
})
Expect(err).ToNot(HaveOccurred())
// Create entry
err = n.Create(nil, bucket, key, value)
Expect(err).ToNot(HaveOccurred())
// We are waiting for half of the TTL to occur
time.Sleep(500 * time.Millisecond)
// Refresh
err = n.Refresh(nil, bucket, key)
Expect(err).ToNot(HaveOccurred())
// Let's wait to get past the 1s TTL
time.Sleep(600 * time.Millisecond)
// We have *definitely* slept for longer than TTL - value should still be there
// Get entry
data, err := n.Get(nil, bucket, key)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(value))
})
It("should error if bucket or key does not exist", func() {
Expect(n.Refresh(nil, "non-existent-bucket", "non-existent-key")).To(HaveOccurred())
// Create bucket, but no key
bucket, _, _ := NewKVSet()
Expect(n.CreateBucket(nil, bucket, 10*time.Second, 1)).ToNot(HaveOccurred())
Expect(n.Refresh(nil, bucket, "non-existent-key")).To(HaveOccurred())
})
It("should error if revision ID does not match the revision ID we expect", func() {
})
})
Describe("Create", func() {
It("should auto-create bucket + create kv entry", func() {
bucket, key, value := NewKVSet()
putErr := n.Create(nil, bucket, key, value)
Expect(putErr).ToNot(HaveOccurred())
// Bucket should've been created
kv, err := n.js.KeyValue(bucket)
Expect(err).ToNot(HaveOccurred())
Expect(kv).NotTo(BeNil())
// K/V should've been created
kve, err := kv.Get(key)
Expect(err).ToNot(HaveOccurred())
Expect(kve).NotTo(BeNil())
// Values should match
Expect(kve.Value()).To(Equal(value))
})
It("should work if bucket already exists", func() {
bucket, key, value := NewKVSet()
ttl := 10 * time.Second
// Pre-create bucket
_, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
TTL: ttl,
})
Expect(err).ToNot(HaveOccurred())
// Verify that bucket exists
kv, err := n.js.KeyValue(bucket)
Expect(err).ToNot(HaveOccurred())
Expect(kv).NotTo(BeNil())
// Create entry
err = n.Create(nil, bucket, key, value)
Expect(err).ToNot(HaveOccurred())
// Did the entry get created?
kve, err := kv.Get(key)
Expect(err).ToNot(HaveOccurred())
Expect(kve).NotTo(BeNil())
// Values should match
Expect(kve.Value()).To(Equal(value))
})
It("should error if key already exists in bucket", func() {
bucket, key, value := NewKVSet()
ttl := 10 * time.Second
// Pre-create bucket
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
TTL: ttl,
})
Expect(err).ToNot(HaveOccurred())
// Pre-add key
_, err = kv.Create(key, value)
Expect(err).ToNot(HaveOccurred())
// Attempt to create for same key should error
err = n.Create(nil, bucket, key, value)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("wrong last sequence"))
})
It("should use TTL", func() {
bucket, key, value := NewKVSet()
ttl := 10 * time.Second
_, err := n.js.KeyValue(bucket)
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(nats.ErrBucketNotFound))
err = n.Create(nil, bucket, key, value, ttl)
Expect(err).ToNot(HaveOccurred())
kv, err := n.js.KeyValue(bucket)
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
status, err := kv.Status()
Expect(err).ToNot(HaveOccurred())
Expect(status.TTL()).To(Equal(ttl))
})
})
Describe("Put", func() {
It("should set the value for a key (and auto-create the bucket)", func() {
bucket, key, value := NewKVSet()
putErr := n.Put(nil, bucket, key, value)
Expect(putErr).ToNot(HaveOccurred())
// Bucket should've been created
kv, err := n.js.KeyValue(bucket)
Expect(err).ToNot(HaveOccurred())
Expect(kv).NotTo(BeNil())
// K/V should've been created
kve, err := kv.Get(key)
Expect(err).ToNot(HaveOccurred())
Expect(kve).NotTo(BeNil())
// Values should match
Expect(kve.Value()).To(Equal(value))
})
It("a key with a TTL will get auto expired", func() {
bucket, key, value := NewKVSet()
putErr := n.Put(nil, bucket, key, value, 1*time.Second)
Expect(putErr).ToNot(HaveOccurred())
// Bucket should've been created
kv, err := n.js.KeyValue(bucket)
Expect(err).ToNot(HaveOccurred())
Expect(kv).NotTo(BeNil())
// Wait a couple sec
time.Sleep(2 * time.Second)
// K/V should no longer be there
kve, err := kv.Get(key)
Expect(err).To(HaveOccurred())
Expect(kve).To(BeNil())
Expect(err).To(Equal(nats.ErrKeyNotFound))
})
})
Describe("Status", func() {
It("should return correct status for the bucket", func() {
bucket := MustNewUUID()
numKeys := uint64(100)
// Add some kvs
for i := uint64(0); i < numKeys; i++ {
key := MustNewUUID()
value := []byte(MustNewUUID())
err := n.Create(nil, bucket, key, value)
Expect(err).ToNot(HaveOccurred())
}
// Now fetch the status
status, err := n.Status(nil, bucket)
Expect(err).ToNot(HaveOccurred())
Expect(status).ToNot(BeNil())
Expect(status.Values()).To(Equal(numKeys))
Expect(status.Bytes()).To(BeNumerically(">", 0))
})
})
Describe("Delete", func() {
It("should delete the value for a key", func() {
bucket, key, value := NewKVSet()
// Create a bucket + key
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "created during kv test",
})
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
_, putErr := kv.Put(key, value)
Expect(putErr).ToNot(HaveOccurred())
// Try to delete it
delErr := n.Delete(nil, bucket, key)
Expect(delErr).ToNot(HaveOccurred())
// Check via js context that it's gone
_, getErr := kv.Get(key)
Expect(getErr).To(Equal(nats.ErrKeyNotFound))
})
})
Describe("Keys", func() {
It("should return all keys in bucket", func() {
// Create bucket, add a bunch of keys into it
bucket, _, _ := NewKVSet()
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "tmp bucket for testing Keys()",
})
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
numKeys := rand.Intn(20) + 1 // + 1 to avoid 0
for i := 0; i < numKeys; i++ {
_, putErr := kv.Put(MustNewUUID(), []byte("test"))
Expect(putErr).ToNot(HaveOccurred())
}
keys, err := n.Keys(context.Background(), bucket)
Expect(err).ToNot(HaveOccurred())
Expect(len(keys)).To(Equal(numKeys))
})
It("should return emtpy slice if no keys in bucket", func() {
// Create bucket, add a bunch of keys into it
bucket, _, _ := NewKVSet()
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "tmp bucket for testing Keys()",
})
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
keys, err := n.Keys(context.Background(), bucket)
Expect(err).ToNot(HaveOccurred())
Expect(len(keys)).To(Equal(0))
})
It("should error if bucket does not exist", func() {
keys, err := n.Keys(context.Background(), MustNewUUID())
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(nats.ErrBucketNotFound))
Expect(keys).To(BeNil())
})
})
Describe("WatchBucket", func() {
It("should receive updates when a key is added", func() {
bucket, _, _ := NewKVSet()
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "tmp bucket for testing WatchBucket()",
})
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
watcher, err := n.WatchBucket(context.Background(), bucket)
Expect(err).ToNot(HaveOccurred())
ch := watcher.Updates()
Eventually(ch, "100ms").Should(Receive())
_, putErr := kv.Put(MustNewUUID(), []byte("test"))
Expect(putErr).ToNot(HaveOccurred())
})
It("should receive updates when a key is deleted", func() {
bucket, _, _ := NewKVSet()
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "tmp bucket for testing WatchBucket()",
})
key := MustNewUUID()
_, putErr := kv.Put(key, []byte("test"))
Expect(putErr).ToNot(HaveOccurred())
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
watcher, err := n.WatchBucket(context.Background(), bucket)
Expect(err).ToNot(HaveOccurred())
ch := watcher.Updates()
// TODO: This is not enough; first received value could be nil which
// will produce a false positive test
Eventually(ch, "100ms").Should(Receive())
delErr := kv.Delete(key)
Expect(delErr).ToNot(HaveOccurred())
})
})
Describe("WatchKey", func() {
It("should receive updates when key is updated", func() {
bucket, key, _ := NewKVSet()
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "tmp bucket for testing WatchBucket()",
})
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
watcher, err := n.WatchKey(context.Background(), bucket, key)
Expect(err).ToNot(HaveOccurred())
ch := watcher.Updates()
// Read for 100ms; should receive a Put operation
go func() {
defer GinkgoRecover()
MAIN:
for {
select {
case v := <-ch:
if v == nil {
continue
}
// Not nil, check operation
Expect(v.Operation()).To(Equal(nats.KeyValuePut))
Expect(v.Value()).To(Equal([]byte("test")))
case <-time.After(time.Second):
Fail("timed out waiting for put")
break MAIN
}
}
}()
// Wait for watcher to start (hopefully 100ms is enough; this is flakey)
time.Sleep(time.Millisecond * 100)
_, putErr := kv.Put(key, []byte("test"))
Expect(putErr).ToNot(HaveOccurred())
})
It("should receive updates when a key is deleted", func() {
bucket, key, _ := NewKVSet()
kv, err := n.js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: bucket,
Description: "tmp bucket for testing WatchBucket()",
})
Expect(err).ToNot(HaveOccurred())
Expect(kv).ToNot(BeNil())
watcher, err := n.WatchKey(context.Background(), bucket, key)
Expect(err).ToNot(HaveOccurred())
ch := watcher.Updates()
// Read for 100ms; should receive a Put operation
go func() {
defer GinkgoRecover()
MAIN:
for {
select {
case v := <-ch:
if v == nil {
continue
}
Eventually(v.Operation()).Should(Equal(nats.KeyValueDelete))
case <-time.After(time.Second):
Fail("timed out waiting for delete")
break MAIN
}
}
}()
// Wait for watcher to start (hopefully 100ms is enough; this is flakey)
time.Sleep(time.Millisecond * 100)
_, putErr := kv.Put(key, []byte("test"))
Expect(putErr).ToNot(HaveOccurred())
delErr := kv.Delete(key)
Expect(delErr).ToNot(HaveOccurred())
})
})
})
func NewKVSet() (bucket string, key string, value []byte) {
bucket = MustNewUUID()
key = MustNewUUID()
value = []byte(MustNewUUID())
testBuckets = append(testBuckets, bucket)
return
}