-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_value_entry_test.go
47 lines (35 loc) · 1.11 KB
/
key_value_entry_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
package natsutil_test
import (
"testing"
"github.com/41north/natsutil.go"
"github.com/nats-io/nats.go/encoders/builtin"
"github.com/nats-io/nats.go"
"github.com/stretchr/testify/assert"
)
func TestKve(t *testing.T) {
s := runBasicJetStreamServer(t)
defer shutdownJSServerAndRemoveStorage(t, s)
_, js := jsClient(t, s)
bucket := createTestBucket(t, js)
encoder := builtin.JsonEncoder{}
kv := natsutil.NewKeyValue[testPayload](bucket, &encoder)
revision, err := kv.Put("foo", testPayload{123})
assert.Nil(t, err)
assert.Equal(t, uint64(1), revision)
kve, err := kv.Get("foo")
assert.Nil(t, err)
assert.Equal(t, bucket.Bucket(), kve.Bucket())
assert.Equal(t, "foo", kve.Key())
assert.Equal(t, nats.KeyValuePut, kve.Operation())
assert.Equal(t, revision, kve.Revision())
assert.Equal(t, uint64(0), kve.Delta())
assert.NotNil(t, kve.Created())
bytes := kve.Value()
assert.True(t, len(bytes) > 0)
for i := 0; i < 3; i++ {
// we do this a few times to trigger the caching related code for the value
value, err := kve.UnmarshalValue()
assert.Nil(t, err)
assert.Equal(t, testPayload{123}, value)
}
}