-
Notifications
You must be signed in to change notification settings - Fork 0
/
boltdb_test.go
154 lines (114 loc) · 3.91 KB
/
boltdb_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
package boltdb
import (
"context"
"os"
"testing"
"time"
"github.com/kvtools/valkeyrie"
"github.com/kvtools/valkeyrie/store"
"github.com/kvtools/valkeyrie/testsuite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testTimeout = 60 * time.Second
func makeBoltDBClient(t *testing.T) store.Store {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
config := &Config{Bucket: "boltDBTest"}
kv, err := New(ctx, []string{"/tmp/not_exist_dir/__boltdbtest"}, config)
require.NoErrorf(t, err, "cannot create store")
return kv
}
func TestRegister(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
config := &Config{Bucket: "boltDBTest"}
kv, err := valkeyrie.NewStore(ctx, StoreName, []string{"/tmp/not_exist_dir/__boltdbtest"}, config)
require.NoError(t, err)
require.NotNil(t, kv)
assert.IsTypef(t, kv, new(Store), "Error registering and initializing boltDB")
_ = os.Remove("/tmp/not_exist_dir/__boltdbtest")
}
// TestMultiplePersistConnection tests the second connection to a
// BoltDB fails when one is already open with PersistConnection flag.
func TestMultiplePersistConnection(t *testing.T) {
config := &Config{
Bucket: "boltDBTest",
ConnectionTimeout: 1 * time.Second,
PersistConnection: true,
}
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
kv, err := valkeyrie.NewStore(ctx, StoreName, []string{"/tmp/not_exist_dir/__boltdbtest"}, config)
require.NoError(t, err)
assert.NotNil(t, kv)
assert.IsTypef(t, kv, new(Store), "Error registering and initializing boltDB")
// Must fail if multiple boltdb requests are made with a valid timeout.
_, err = valkeyrie.NewStore(ctx, StoreName, []string{"/tmp/not_exist_dir/__boltdbtest"}, config)
assert.Error(t, err)
_ = os.Remove("/tmp/not_exist_dir/__boltdbtest")
}
// TestConcurrentConnection tests simultaneous get/put using
// two handles.
func TestConcurrentConnection(t *testing.T) {
config := &Config{
Bucket: "boltDBTest",
ConnectionTimeout: 1 * time.Second,
}
t.Cleanup(func() {
_ = os.Remove("/tmp/__boltdbtest")
})
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
kv1, err := valkeyrie.NewStore(ctx, StoreName, []string{"/tmp/__boltdbtest"}, config)
require.NoError(t, err)
assert.NotNil(t, kv1)
kv2, err := valkeyrie.NewStore(ctx, StoreName, []string{"/tmp/__boltdbtest"}, config)
require.NoError(t, err)
assert.NotNil(t, kv2)
key1 := "TestKV1"
value1 := []byte("TestVal1")
err = kv1.Put(ctx, key1, value1, nil)
require.NoError(t, err)
key2 := "TestKV2"
value2 := []byte("TestVal2")
err = kv2.Put(ctx, key2, value2, nil)
require.NoError(t, err)
pair1, err := kv1.Get(ctx, key1, nil)
require.NoError(t, err)
require.NotNil(t, pair1)
assert.Equal(t, pair1.Value, value1)
pair2, err := kv2.Get(ctx, key2, nil)
require.NoError(t, err)
require.NotNil(t, pair2)
assert.Equal(t, pair2.Value, value2)
// AtomicPut using kv1 and kv2 should succeed.
_, _, err = kv1.AtomicPut(ctx, key1, []byte("TestnewVal1"), pair1, nil)
require.NoError(t, err)
_, _, err = kv2.AtomicPut(ctx, key2, []byte("TestnewVal2"), pair2, nil)
require.NoError(t, err)
testsuite.RunTestCommon(t, kv1)
testsuite.RunTestCommon(t, kv2)
_ = kv1.Close()
_ = kv2.Close()
}
func TestBoltDBStore(t *testing.T) {
kv := makeBoltDBClient(t)
testsuite.RunTestCommon(t, kv)
testsuite.RunTestAtomic(t, kv)
_ = os.Remove("/tmp/not_exist_dir/__boltdbtest")
}
func TestGetAllKeys(t *testing.T) {
kv := makeBoltDBClient(t)
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
t.Cleanup(func() {
_ = kv.Delete(ctx, "key1")
})
err := kv.Put(ctx, "key1", []byte("value1"), &store.WriteOptions{})
require.NoError(t, err)
pairs, err := kv.List(ctx, "", nil)
require.NoError(t, err)
assert.Len(t, pairs, 1)
}