-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_test.go
More file actions
230 lines (191 loc) · 5.33 KB
/
Copy pathtree_test.go
File metadata and controls
230 lines (191 loc) · 5.33 KB
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
package dmt
import (
"os"
"path/filepath"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestNewTree(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When a new tree is created", func() {
So(tree, ShouldNotBeNil)
})
})
}
func TestSeek(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When a seek is performed", func() {
value, ok := tree.Seek([]byte("test"))
So(ok, ShouldBeTrue)
So(value, ShouldEqual, []byte("test"))
})
})
}
func TestInsert(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When an insert is performed", func() {
newTree, ok := tree.Insert([]byte("test"), []byte("test"))
So(ok, ShouldBeTrue)
So(newTree, ShouldNotBeNil)
// Verify the insert worked
value, exists := newTree.Get([]byte("test"))
So(exists, ShouldBeTrue)
So(value, ShouldResemble, []byte("test"))
})
})
}
func TestGet(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When a get is performed", func() {
value, ok := tree.Get([]byte("test"))
So(ok, ShouldBeTrue)
So(value, ShouldEqual, []byte("test"))
})
})
}
func TestAVG(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When a avg is performed", func() {
avg := tree.AVG()
So(avg, ShouldBeGreaterThan, 0)
})
})
}
func TestClose(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When a close is performed", func() {
err := tree.Close()
So(err, ShouldBeNil)
})
})
}
func TestUpdateTerm(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When a update term is performed", func() {
tree.UpdateTerm(1)
So(tree.term, ShouldEqual, 1)
})
})
}
func TestGetLogState(t *testing.T) {
Convey("Given a new tree", t, func() {
tree, err := NewTree("")
So(err, ShouldBeNil)
Convey("When a get log state is performed", func() {
term, index := tree.GetLogState()
So(term, ShouldEqual, 0)
So(index, ShouldEqual, 0)
})
})
}
func TestTreeWithPersistence(t *testing.T) {
Convey("Given a temporary directory", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
Convey("When creating a tree with persistence", func() {
tree, err := NewTree(tmpDir)
So(err, ShouldBeNil)
defer tree.Close()
Convey("Then the persistence store should be initialized", func() {
So(tree.persist, ShouldNotBeNil)
})
Convey("And when inserting data", func() {
newTree, ok := tree.Insert([]byte("test-key"), []byte("test-value"))
So(ok, ShouldBeTrue)
So(newTree, ShouldNotBeNil)
Convey("The data should be persisted", func() {
// Create new tree instance with same persistence
tree2, err := NewTree(tmpDir)
So(err, ShouldBeNil)
defer tree2.Close()
// Verify term and index were loaded
term, index := tree2.GetLogState()
So(term, ShouldEqual, tree.term)
So(index, ShouldEqual, tree.logIndex)
// Verify data was recovered
value, exists := tree2.Get([]byte("test-key"))
So(exists, ShouldBeTrue)
So(value, ShouldResemble, []byte("test-value"))
})
})
})
})
}
func TestTreeStateRecovery(t *testing.T) {
Convey("Given a tree with existing WAL", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
// Create and populate first tree
tree1, err := NewTree(tmpDir)
So(err, ShouldBeNil)
entries := []struct {
key string
value string
term uint64
}{
{"key1", "value1", 1},
{"key2", "value2", 1},
{"key3", "value3", 2},
}
for _, e := range entries {
tree1.UpdateTerm(e.term)
tree1, _ = tree1.Insert([]byte(e.key), []byte(e.value))
}
tree1.Close()
Convey("When creating a new tree instance", func() {
tree2, err := NewTree(tmpDir)
So(err, ShouldBeNil)
defer tree2.Close()
Convey("Then it should recover the correct state", func() {
term, index := tree2.GetLogState()
So(term, ShouldEqual, entries[len(entries)-1].term)
So(index, ShouldEqual, uint64(len(entries)))
Convey("And all data should be accessible", func() {
for _, e := range entries {
value, exists := tree2.Get([]byte(e.key))
So(exists, ShouldBeTrue)
So(value, ShouldResemble, []byte(e.value))
}
})
})
})
})
}
func TestTreeTermUpdate(t *testing.T) {
Convey("Given a persistent tree", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
tree, err := NewTree(tmpDir)
So(err, ShouldBeNil)
defer tree.Close()
Convey("When updating the term", func() {
tree.UpdateTerm(5)
Convey("Then the term should be persisted", func() {
term, _ := tree.GetLogState()
So(term, ShouldEqual, uint64(5))
// Verify term survives restart
tree.Close()
newTree, err := NewTree(tmpDir)
So(err, ShouldBeNil)
defer newTree.Close()
term, _ = newTree.GetLogState()
So(term, ShouldEqual, uint64(5))
})
})
})
}