-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforest_test.go
More file actions
206 lines (169 loc) · 5.44 KB
/
Copy pathforest_test.go
File metadata and controls
206 lines (169 loc) · 5.44 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
package dmt
import (
"os"
"path/filepath"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestNewForest(t *testing.T) {
Convey("Given a forest configuration", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
config := ForestConfig{
PersistDir: tmpDir,
}
Convey("When creating a new forest", func() {
forest, err := NewForest(config)
So(err, ShouldBeNil)
defer forest.Close()
Convey("Then it should be properly initialized", func() {
So(forest, ShouldNotBeNil)
So(forest.trees, ShouldNotBeNil)
So(forest.updates, ShouldNotBeNil)
So(forest.ctx, ShouldNotBeNil)
So(forest.cancel, ShouldNotBeNil)
})
Convey("And it should have one initial tree", func() {
So(len(forest.trees), ShouldEqual, 1)
So(forest.trees[0], ShouldNotBeNil)
})
})
})
}
func TestForestOperations(t *testing.T) {
Convey("Given a new forest", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
forest, err := NewForest(ForestConfig{PersistDir: tmpDir})
So(err, ShouldBeNil)
defer forest.Close()
Convey("When performing insert operations", func() {
forest.Insert([]byte("key1"), []byte("value1"))
forest.Insert([]byte("key2"), []byte("value2"))
Convey("Then the data should be retrievable", func() {
value1, exists := forest.Get([]byte("key1"))
So(exists, ShouldBeTrue)
So(value1, ShouldResemble, []byte("value1"))
value2, exists := forest.Get([]byte("key2"))
So(exists, ShouldBeTrue)
So(value2, ShouldResemble, []byte("value2"))
})
Convey("And seek operations should work", func() {
value, exists := forest.Seek([]byte("key"))
So(exists, ShouldBeTrue)
So(value, ShouldResemble, []byte("value1")) // Should find first key alphabetically
})
})
})
}
func TestForestSynchronization(t *testing.T) {
Convey("Given a forest with multiple trees", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
forest, err := NewForest(ForestConfig{PersistDir: tmpDir})
So(err, ShouldBeNil)
defer forest.Close()
// Add a second tree
tree2, err := NewTree("")
So(err, ShouldBeNil)
forest.AddTree(tree2)
Convey("When inserting data", func() {
forest.Insert([]byte("sync-key"), []byte("sync-value"))
// Wait for sync to complete
time.Sleep(100 * time.Millisecond)
Convey("Then all trees should have the data", func() {
for _, tree := range forest.trees {
value, exists := tree.Get([]byte("sync-key"))
So(exists, ShouldBeTrue)
So(value, ShouldResemble, []byte("sync-value"))
}
})
})
})
}
func TestForestPerformance(t *testing.T) {
Convey("Given a forest with multiple trees", t, func() {
forest, err := NewForest(ForestConfig{})
So(err, ShouldBeNil)
defer forest.Close()
// Add trees with different simulated performance characteristics
tree1, err := NewTree("")
So(err, ShouldBeNil)
tree2, err := NewTree("")
So(err, ShouldBeNil)
forest.AddTree(tree1)
forest.AddTree(tree2)
Convey("When getting the fastest tree", func() {
fastestTree := forest.getFastestTree()
Convey("Then it should return a valid tree", func() {
So(fastestTree, ShouldNotBeNil)
So(fastestTree.AVG(), ShouldBeGreaterThanOrEqualTo, 0)
})
})
})
}
func TestForestNetworking(t *testing.T) {
Convey("Given a forest with network configuration", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
config := ForestConfig{
PersistDir: tmpDir,
Network: &NetworkConfig{
ListenAddr: ":0", // Use random port
NodeID: "test-node",
},
}
forest, err := NewForest(config)
So(err, ShouldBeNil)
defer forest.Close()
Convey("Then the network node should be initialized", func() {
So(forest.network, ShouldNotBeNil)
So(forest.network.config.NodeID, ShouldEqual, "test-node")
})
Convey("When inserting data with networking enabled", func() {
forest.Insert([]byte("network-key"), []byte("network-value"))
Convey("Then the network node should broadcast the insert", func() {
// Verify local insertion worked
value, exists := forest.Get([]byte("network-key"))
So(exists, ShouldBeTrue)
So(value, ShouldResemble, []byte("network-value"))
// Network metrics should be updated
metrics := forest.network.GetMetrics()
networkStats := metrics["network"].(map[string]interface{})
So(networkStats["bytes_tx"], ShouldBeGreaterThan, 0)
})
})
})
}
func TestForestClose(t *testing.T) {
Convey("Given a forest with multiple components", t, func() {
tmpDir := filepath.Join(os.TempDir(), "radix-test-"+time.Now().Format("20060102150405"))
defer os.RemoveAll(tmpDir)
config := ForestConfig{
PersistDir: tmpDir,
Network: &NetworkConfig{
ListenAddr: ":0",
NodeID: "test-node",
},
}
forest, err := NewForest(config)
So(err, ShouldBeNil)
// Add additional tree
tree2, err := NewTree("")
So(err, ShouldBeNil)
forest.AddTree(tree2)
Convey("When closing the forest", func() {
forest.Close()
Convey("Then the context should be cancelled", func() {
select {
case <-forest.ctx.Done():
// Context was cancelled as expected
So(true, ShouldBeTrue)
default:
So(false, ShouldBeTrue, "Context was not cancelled")
}
})
})
})
}