-
Notifications
You must be signed in to change notification settings - Fork 2
/
gokv_test.go
124 lines (101 loc) · 2.65 KB
/
gokv_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
package gokv
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
dv "github.com/bradberger/gokv/drivers/diskv"
"github.com/bradberger/gokv/kv"
"github.com/peterbourgon/diskv"
"github.com/stretchr/testify/assert"
)
func getTestOptions() diskv.Options {
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("diskv-%d", rand.Intn(100)))
if err != nil {
panic(err)
}
return diskv.Options{
BasePath: tmpDir,
Transform: func(s string) []string { return []string{} },
CacheSizeMax: 1024 * 1024,
}
}
func TestReplicationN(t *testing.T) {
opts := getTestOptions()
opts2 := getTestOptions()
db := dv.New(opts)
db2 := dv.New(opts2)
defer func() {
os.RemoveAll(opts.BasePath)
os.RemoveAll(opts2.BasePath)
}()
var s string
c := New()
assert.Error(t, c.Set("foo", "bar"))
c.AddNode("node-01", db)
c.ReplicateToN(2)
assert.NoError(t, c.Set("foo", "bar"))
c.AddNode("node-02", db2)
assert.NoError(t, c.Set("foo", "bar"))
assert.NoError(t, c.nodes["node-01"].Get("foo", &s))
assert.NoError(t, c.nodes["node-02"].Get("foo", &s))
}
func TestSetNodes(t *testing.T) {
c := New()
opts := getTestOptions()
db := dv.New(opts)
defer func() {
os.RemoveAll(opts.BasePath)
}()
assert.Error(t, c.ReplaceNode("node-01", db))
assert.Error(t, c.SetNode("node-01", nil))
assert.NoError(t, c.AddNode("node-01", db))
assert.NoError(t, c.ReplaceNode("node-01", db))
assert.Error(t, c.AddNode("node-01", db))
assert.NoError(t, c.RemoveNode("node-01"))
assert.Len(t, c.nodes, 0)
}
func TestGetErr(t *testing.T) {
opts := getTestOptions()
db := dv.New(opts)
defer func() {
os.RemoveAll(opts.BasePath)
}()
var v string
c := New()
c.AddNode("node-01", db)
assert.Equal(t, kv.ErrNotFound, c.Get("foo", &v))
}
func TestReplicationSync(t *testing.T) {
opts := getTestOptions()
opts2 := getTestOptions()
db := dv.New(opts)
db2 := dv.New(opts2)
defer func() {
os.RemoveAll(opts.BasePath)
os.RemoveAll(opts2.BasePath)
}()
assert.NotEqual(t, opts.BasePath, opts2.BasePath)
var s string
c := New()
c.AddNode("node-01", db)
c.AddNode("node-02", db2)
c.ReplicateToN(2)
c.SetReplicateMethod(ReplicateSync)
assert.NoError(t, c.Set("foo", "bar"))
assert.NoError(t, c.nodes["node-01"].Get("foo", &s))
assert.NoError(t, c.nodes["node-02"].Get("foo", &s))
assert.NoError(t, c.nodes["node-01"].Del("foo"))
assert.NoError(t, c.Get("foo", &s))
assert.Equal(t, "bar", s)
assert.True(t, db2.Diskv().Has("foo"))
assert.NoError(t, c.nodes["node-02"].Del("foo"))
assert.Equal(t, kv.ErrNotFound, c.Del("foo"))
}
func TestGetNErr(t *testing.T) {
c := New()
assert.Error(t, c.Del("foo"))
assert.Error(t, c.Set("foo", "bar"))
assert.Error(t, c.Get("foo", nil))
}