forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_test.go
190 lines (170 loc) · 3.66 KB
/
ring_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
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
package buddystore
import (
"bytes"
"crypto/sha1"
"sort"
"testing"
"time"
)
type MockDelegate struct {
shutdown bool
}
func (m *MockDelegate) NewPredecessor(local, remoteNew, remotePrev *Vnode) {
}
func (m *MockDelegate) Leaving(local, pred, succ *Vnode) {
}
func (m *MockDelegate) PredecessorLeaving(local, remote *Vnode) {
}
func (m *MockDelegate) SuccessorLeaving(local, remote *Vnode) {
}
func (m *MockDelegate) Shutdown() {
m.shutdown = true
}
func makeRing() *Ring {
conf := &Config{
NumVnodes: 5,
NumSuccessors: 8,
HashFunc: sha1.New,
hashBits: 160,
StabilizeMin: time.Second,
StabilizeMax: 5 * time.Second,
}
ring := &Ring{}
ring.init(conf, nil)
return ring
}
func TestRingInit(t *testing.T) {
// Create a ring
ring := &Ring{}
conf := DefaultConfig("test")
ring.init(conf, nil)
// Test features
if ring.config != conf {
t.Fatalf("wrong config")
}
if ring.transport == nil {
t.Fatalf("missing transport")
}
// Check the vnodes
for i := 0; i < conf.NumVnodes; i++ {
if ring.vnodes[i] == nil {
t.Fatalf("missing vnode!")
}
if ring.vnodes[i].ring != ring {
t.Fatalf("ring missing!")
}
if ring.vnodes[i].Id == nil {
t.Fatalf("ID not initialized!")
}
}
}
func TestRingLen(t *testing.T) {
ring := makeRing()
if ring.Len() != 5 {
t.Fatalf("wrong len")
}
}
func TestRingSort(t *testing.T) {
ring := makeRing()
sort.Sort(ring)
if bytes.Compare(ring.vnodes[0].Id, ring.vnodes[1].Id) != -1 {
t.Fatalf("bad sort")
}
if bytes.Compare(ring.vnodes[1].Id, ring.vnodes[2].Id) != -1 {
t.Fatalf("bad sort")
}
if bytes.Compare(ring.vnodes[2].Id, ring.vnodes[3].Id) != -1 {
t.Fatalf("bad sort")
}
if bytes.Compare(ring.vnodes[3].Id, ring.vnodes[4].Id) != -1 {
t.Fatalf("bad sort")
}
}
func TestRingNearest(t *testing.T) {
ring := makeRing()
ring.vnodes[0].Id = []byte{2}
ring.vnodes[1].Id = []byte{4}
ring.vnodes[2].Id = []byte{7}
ring.vnodes[3].Id = []byte{10}
ring.vnodes[4].Id = []byte{14}
key := []byte{6}
near := ring.nearestVnode(key)
if near != ring.vnodes[1] {
t.Fatalf("got wrong node back!")
}
key = []byte{0}
near = ring.nearestVnode(key)
if near != ring.vnodes[4] {
t.Fatalf("got wrong node back!")
}
}
func TestRingSchedule(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ring := makeRing()
ring.setLocalSuccessors()
ring.schedule()
for i := 0; i < len(ring.vnodes); i++ {
if ring.vnodes[i].timer == nil {
t.Fatalf("expected timer!")
}
}
ring.stopVnodes()
}
func TestRingSetLocalSucc(t *testing.T) {
ring := makeRing()
ring.setLocalSuccessors()
for i := 0; i < len(ring.vnodes); i++ {
for j := 0; j < 4; j++ {
if ring.vnodes[i].successors[j] == nil {
t.Fatalf("expected successor!")
}
}
if ring.vnodes[i].successors[4] != nil {
t.Fatalf("should not have 5th successor!")
}
}
// Verify the successor manually for node 3
vn := ring.vnodes[2]
if vn.successors[0] != &ring.vnodes[3].Vnode {
t.Fatalf("bad succ!")
}
if vn.successors[1] != &ring.vnodes[4].Vnode {
t.Fatalf("bad succ!")
}
if vn.successors[2] != &ring.vnodes[0].Vnode {
t.Fatalf("bad succ!")
}
if vn.successors[3] != &ring.vnodes[1].Vnode {
t.Fatalf("bad succ!")
}
}
func TestRingDelegate(t *testing.T) {
d := &MockDelegate{}
ring := makeRing()
ring.setLocalSuccessors()
ring.config.Delegate = d
ring.schedule()
var b bool
f := func() {
println("run!")
b = true
}
ch := ring.invokeDelegate(f)
if ch == nil {
t.Fatalf("expected chan")
}
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
if !b {
t.Fatalf("b should be true")
}
ring.stopDelegate()
if !d.shutdown {
t.Fatalf("delegate did not get shutdown")
}
}