forked from hashicorp/memberlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
427 lines (390 loc) · 8.06 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package memberlist
import (
"errors"
"fmt"
"net"
"reflect"
"testing"
"time"
)
func TestGetPrivateIP(t *testing.T) {
ip, _, err := net.ParseCIDR("10.1.2.3/32")
if err != nil {
t.Fatalf("failed to parse private cidr: %v", err)
}
pubIP, _, err := net.ParseCIDR("8.8.8.8/32")
if err != nil {
t.Fatalf("failed to parse public cidr: %v", err)
}
tests := []struct {
addrs []net.Addr
expected net.IP
err error
}{
{
addrs: []net.Addr{
&net.IPAddr{
IP: ip,
},
&net.IPAddr{
IP: pubIP,
},
},
expected: ip,
},
{
addrs: []net.Addr{
&net.IPAddr{
IP: pubIP,
},
},
err: errors.New("No private IP address found"),
},
{
addrs: []net.Addr{
&net.IPAddr{
IP: ip,
},
&net.IPAddr{
IP: ip,
},
&net.IPAddr{
IP: pubIP,
},
},
err: errors.New("Multiple private IPs found. Please configure one."),
},
}
for _, test := range tests {
ip, err := GetPrivateIP(test.addrs)
switch {
case test.err != nil && err != nil:
if err.Error() != test.err.Error() {
t.Fatalf("unexpected error: %v != %v", test.err, err)
}
case (test.err == nil && err != nil) || (test.err != nil && err == nil):
t.Fatalf("unexpected error: %v != %v", test.err, err)
default:
if !test.expected.Equal(ip) {
t.Fatalf("unexpected ip: %v != %v", ip, test.expected)
}
}
}
}
func TestIsPrivateIP(t *testing.T) {
privateIPs := []string{
"10.1.2.3",
"100.115.110.19",
"127.0.0.1",
"169.254.1.254",
"172.16.45.100",
"192.168.1.1",
}
publicIPs := []string{
"8.8.8.8",
"208.67.222.222",
}
for _, privateIP := range privateIPs {
if !IsPrivateIP(privateIP) {
t.Fatalf("bad")
}
}
for _, publicIP := range publicIPs {
if IsPrivateIP(publicIP) {
t.Fatalf("bad")
}
}
}
func Test_hasPort(t *testing.T) {
cases := []struct {
s string
expected bool
}{
{"", false},
{":80", true},
{"127.0.0.1", false},
{"127.0.0.1:80", true},
{"::1", false},
{"2001:db8:a0b:12f0::1", false},
{"[2001:db8:a0b:12f0::1]", false},
{"[2001:db8:a0b:12f0::1]:80", true},
}
for _, c := range cases {
if hasPort(c.s) != c.expected {
t.Fatalf("bad: '%s' hasPort was not %v", c.s, c.expected)
}
}
}
func TestEncodeDecode(t *testing.T) {
msg := &ping{SeqNo: 100}
buf, err := encode(pingMsg, msg)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
var out ping
if err := decode(buf.Bytes()[1:], &out); err != nil {
t.Fatalf("unexpected err: %s", err)
}
if msg.SeqNo != out.SeqNo {
t.Fatalf("bad sequence no")
}
}
func TestRandomOffset(t *testing.T) {
vals := make(map[int]struct{})
for i := 0; i < 100; i++ {
offset := randomOffset(2 << 30)
if _, ok := vals[offset]; ok {
t.Fatalf("got collision")
}
vals[offset] = struct{}{}
}
}
func TestRandomOffset_Zero(t *testing.T) {
offset := randomOffset(0)
if offset != 0 {
t.Fatalf("bad offset")
}
}
func TestSuspicionTimeout(t *testing.T) {
timeouts := map[int]time.Duration{
5: 1000 * time.Millisecond,
10: 1000 * time.Millisecond,
50: 1698 * time.Millisecond,
100: 2000 * time.Millisecond,
500: 2698 * time.Millisecond,
1000: 3000 * time.Millisecond,
}
for n, expected := range timeouts {
timeout := suspicionTimeout(3, n, time.Second) / 3
if timeout != expected {
t.Fatalf("bad: %v, %v", expected, timeout)
}
}
}
func TestRetransmitLimit(t *testing.T) {
lim := retransmitLimit(3, 0)
if lim != 0 {
t.Fatalf("bad val %v", lim)
}
lim = retransmitLimit(3, 1)
if lim != 3 {
t.Fatalf("bad val %v", lim)
}
lim = retransmitLimit(3, 99)
if lim != 6 {
t.Fatalf("bad val %v", lim)
}
}
func TestShuffleNodes(t *testing.T) {
orig := []*nodeState{
&nodeState{
State: stateDead,
},
&nodeState{
State: stateAlive,
},
&nodeState{
State: stateAlive,
},
&nodeState{
State: stateDead,
},
&nodeState{
State: stateAlive,
},
&nodeState{
State: stateAlive,
},
&nodeState{
State: stateDead,
},
&nodeState{
State: stateAlive,
},
}
nodes := make([]*nodeState, len(orig))
copy(nodes[:], orig[:])
if !reflect.DeepEqual(nodes, orig) {
t.Fatalf("should match")
}
shuffleNodes(nodes)
if reflect.DeepEqual(nodes, orig) {
t.Fatalf("should not match")
}
}
func TestPushPullScale(t *testing.T) {
sec := time.Second
for i := 0; i <= 32; i++ {
if s := pushPullScale(sec, i); s != sec {
t.Fatalf("Bad time scale: %v", s)
}
}
for i := 33; i <= 64; i++ {
if s := pushPullScale(sec, i); s != 2*sec {
t.Fatalf("Bad time scale: %v", s)
}
}
for i := 65; i <= 128; i++ {
if s := pushPullScale(sec, i); s != 3*sec {
t.Fatalf("Bad time scale: %v", s)
}
}
}
func TestMoveDeadNodes(t *testing.T) {
nodes := []*nodeState{
&nodeState{
State: stateDead,
},
&nodeState{
State: stateAlive,
},
&nodeState{
State: stateAlive,
},
&nodeState{
State: stateDead,
},
&nodeState{
State: stateAlive,
},
}
idx := moveDeadNodes(nodes)
if idx != 3 {
t.Fatalf("bad index")
}
for i := 0; i < idx; i++ {
if nodes[i].State != stateAlive {
t.Fatalf("Bad state %d", i)
}
}
for i := idx; i < len(nodes); i++ {
if nodes[i].State != stateDead {
t.Fatalf("Bad state %d", i)
}
}
}
func TestKRandomNodes(t *testing.T) {
nodes := []*nodeState{}
for i := 0; i < 90; i++ {
// Half the nodes are in a bad state
state := stateAlive
switch i % 3 {
case 0:
state = stateAlive
case 1:
state = stateSuspect
case 2:
state = stateDead
}
nodes = append(nodes, &nodeState{
Node: Node{
Name: fmt.Sprintf("test%d", i),
},
State: state,
})
}
filterFunc := func(n *nodeState) bool {
if n.Name == "test0" || n.State != stateAlive {
return true
}
return false
}
s1 := kRandomNodes(3, nodes, filterFunc)
s2 := kRandomNodes(3, nodes, filterFunc)
s3 := kRandomNodes(3, nodes, filterFunc)
if reflect.DeepEqual(s1, s2) {
t.Fatalf("unexpected equal")
}
if reflect.DeepEqual(s1, s3) {
t.Fatalf("unexpected equal")
}
if reflect.DeepEqual(s2, s3) {
t.Fatalf("unexpected equal")
}
for _, s := range [][]*nodeState{s1, s2, s3} {
if len(s) != 3 {
t.Fatalf("bad len")
}
for _, n := range s {
if n.Name == "test0" {
t.Fatalf("Bad name")
}
if n.State != stateAlive {
t.Fatalf("Bad state")
}
}
}
}
func TestMakeCompoundMessage(t *testing.T) {
msg := &ping{SeqNo: 100}
buf, err := encode(pingMsg, msg)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
msgs := [][]byte{buf.Bytes(), buf.Bytes(), buf.Bytes()}
compound := makeCompoundMessage(msgs)
if compound.Len() != 3*buf.Len()+3*compoundOverhead+compoundHeaderOverhead {
t.Fatalf("bad len")
}
}
func TestDecodeCompoundMessage(t *testing.T) {
msg := &ping{SeqNo: 100}
buf, err := encode(pingMsg, msg)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
msgs := [][]byte{buf.Bytes(), buf.Bytes(), buf.Bytes()}
compound := makeCompoundMessage(msgs)
trunc, parts, err := decodeCompoundMessage(compound.Bytes()[1:])
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
if trunc != 0 {
t.Fatalf("should not truncate")
}
if len(parts) != 3 {
t.Fatalf("bad parts")
}
for _, p := range parts {
if len(p) != buf.Len() {
t.Fatalf("bad part len")
}
}
}
func TestDecodeCompoundMessage_Trunc(t *testing.T) {
msg := &ping{SeqNo: 100}
buf, err := encode(pingMsg, msg)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
msgs := [][]byte{buf.Bytes(), buf.Bytes(), buf.Bytes()}
compound := makeCompoundMessage(msgs)
trunc, parts, err := decodeCompoundMessage(compound.Bytes()[1:38])
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
if trunc != 1 {
t.Fatalf("truncate: %d", trunc)
}
if len(parts) != 2 {
t.Fatalf("bad parts")
}
for _, p := range parts {
if len(p) != buf.Len() {
t.Fatalf("bad part len")
}
}
}
func TestCompressDecompressPayload(t *testing.T) {
buf, err := compressPayload([]byte("testing"))
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
decomp, err := decompressPayload(buf.Bytes()[1:])
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
if !reflect.DeepEqual(decomp, []byte("testing")) {
t.Fatalf("bad payload: %v", decomp)
}
}