forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
167 lines (147 loc) · 3.21 KB
/
util_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
package buddystore
import (
"errors"
"testing"
"time"
)
func TestRandStabilize(t *testing.T) {
min := time.Duration(10 * time.Second)
max := time.Duration(30 * time.Second)
conf := &Config{
StabilizeMin: min,
StabilizeMax: max}
var times []time.Duration
for i := 0; i < 1000; i++ {
after := randStabilize(conf)
times = append(times, after)
if after < min {
t.Fatalf("after below min")
}
if after > max {
t.Fatalf("after above max")
}
}
collisions := 0
for idx, val := range times {
for i := 0; i < len(times); i++ {
if idx != i && times[i] == val {
collisions += 1
}
}
}
if collisions > 3 {
t.Fatalf("too many collisions! %d", collisions)
}
}
func TestBetween(t *testing.T) {
t1 := []byte{0, 0, 0, 0}
t2 := []byte{1, 0, 0, 0}
k := []byte{0, 0, 5, 0}
if !between(t1, t2, k) {
t.Fatalf("expected k between!")
}
if between(t1, t2, t1) {
t.Fatalf("dont expect t1 between!")
}
if between(t1, t2, t2) {
t.Fatalf("dont expect t1 between!")
}
k = []byte{2, 0, 0, 0}
if between(t1, t2, k) {
t.Fatalf("dont expect k between!")
}
}
func TestBetweenWrap(t *testing.T) {
t1 := []byte{0xff, 0, 0, 0}
t2 := []byte{1, 0, 0, 0}
k := []byte{0, 0, 5, 0}
if !between(t1, t2, k) {
t.Fatalf("expected k between!")
}
k = []byte{0xff, 0xff, 0, 0}
if !between(t1, t2, k) {
t.Fatalf("expect k between!")
}
}
func TestBetweenRightIncl(t *testing.T) {
t1 := []byte{0, 0, 0, 0}
t2 := []byte{1, 0, 0, 0}
k := []byte{1, 0, 0, 0}
if !betweenRightIncl(t1, t2, k) {
t.Fatalf("expected k between!")
}
}
func TestBetweenRightInclWrap(t *testing.T) {
t1 := []byte{0xff, 0, 0, 0}
t2 := []byte{1, 0, 0, 0}
k := []byte{1, 0, 0, 0}
if !betweenRightIncl(t1, t2, k) {
t.Fatalf("expected k between!")
}
}
func TestPowerOffset(t *testing.T) {
id := []byte{0, 0, 0, 0}
exp := 30
mod := 32
val := powerOffset(id, exp, mod)
if val[0] != 64 {
t.Fatalf("unexpected val! %v", val)
}
// 0-7, 8-15, 16-23, 24-31
id = []byte{0, 0xff, 0xff, 0xff}
exp = 23
val = powerOffset(id, exp, mod)
if val[0] != 1 || val[1] != 0x7f || val[2] != 0xff || val[3] != 0xff {
t.Fatalf("unexpected val! %v", val)
}
}
func TestMax(t *testing.T) {
if max(-10, 10) != 10 {
t.Fatalf("bad max")
}
if max(10, -10) != 10 {
t.Fatalf("bad max")
}
}
func TestMin(t *testing.T) {
if min(-10, 10) != -10 {
t.Fatalf("bad min")
}
if min(10, -10) != -10 {
t.Fatalf("bad min")
}
}
func TestNearestVnodesKey(t *testing.T) {
vnodes := make([]*Vnode, 5)
vnodes[0] = &Vnode{Id: []byte{2}}
vnodes[1] = &Vnode{Id: []byte{4}}
vnodes[2] = &Vnode{Id: []byte{7}}
vnodes[3] = &Vnode{Id: []byte{10}}
vnodes[4] = &Vnode{Id: []byte{14}}
key := []byte{6}
near := nearestVnodeToKey(vnodes, key)
if near != vnodes[1] {
t.Fatalf("got wrong node back!")
}
key = []byte{0}
near = nearestVnodeToKey(vnodes, key)
if near != vnodes[4] {
t.Fatalf("got wrong node back!")
}
}
func TestMergeErrors(t *testing.T) {
e1 := errors.New("test1")
e2 := errors.New("test2")
if mergeErrors(e1, nil) != e1 {
t.Fatalf("bad merge")
}
if mergeErrors(nil, e1) != e1 {
t.Fatalf("bad merge")
}
if mergeErrors(nil, nil) != nil {
t.Fatalf("bad merge")
}
if mergeErrors(e1, e2).Error() != "test1\ntest2" {
t.Fatalf("bad merge")
}
}