forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_test.go
92 lines (79 loc) · 1.86 KB
/
net_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
package buddystore
import (
"fmt"
"testing"
"time"
)
func prepRing(port int) (*Config, *TCPTransport, error) {
listen := fmt.Sprintf("localhost:%d", port)
conf := DefaultConfig(listen)
conf.StabilizeMin = time.Duration(15 * time.Millisecond)
conf.StabilizeMax = time.Duration(45 * time.Millisecond)
timeout := time.Duration(20 * time.Millisecond)
trans, err := InitTCPTransport(listen, timeout)
if err != nil {
return nil, nil, err
}
return conf, trans, nil
}
func TCPJoin(t *testing.T) {
// Prepare to create 2 nodes
c1, t1, err := prepRing(10025)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
c2, t2, err := prepRing(10026)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
// Create initial ring
r1, err := Create(c1, t1)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
// Join ring
r2, err := Join(c2, t2, c1.Hostname)
if err != nil {
t.Fatalf("failed to join local node! Got %s", err)
}
// Shutdown
r1.Shutdown()
r2.Shutdown()
t1.Shutdown()
t2.Shutdown()
}
func TCPLeave(t *testing.T) {
// Prepare to create 2 nodes
c1, t1, err := prepRing(10027)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
c2, t2, err := prepRing(10028)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
// Create initial ring
r1, err := Create(c1, t1)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
// Join ring
r2, err := Join(c2, t2, c1.Hostname)
if err != nil {
t.Fatalf("failed to join local node! Got %s", err)
}
// Wait for some stabilization
<-time.After(100 * time.Millisecond)
// Node 1 should leave
r1.Leave()
t1.Shutdown()
// Wait for stabilization
<-time.After(100 * time.Millisecond)
// Verify r2 ring is still in tact
for _, vn := range r2.vnodes {
if vn.successors[0].Host != r2.config.Hostname {
t.Fatalf("bad successor! Got:%s:%s", vn.successors[0].Host,
vn.successors[0])
}
}
}