forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 2
/
net_utp_test.go
114 lines (92 loc) · 2.3 KB
/
net_utp_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
package chord
import (
"fmt"
"testing"
"time"
"github.com/anacrolix/utp"
"github.com/ipkg/go-mux"
)
func prepRingUTP(port int) (*Config, *UTPTransport, error) {
listen := fmt.Sprintf("127.0.0.1:%d", port)
conf := DefaultConfig(listen)
conf.StabilizeMin = time.Duration(15 * time.Millisecond)
conf.StabilizeMax = time.Duration(45 * time.Millisecond)
dialTimeout := time.Duration(1 * time.Second)
rpcTimeout := time.Duration(20 * time.Millisecond)
maxConnIdle := time.Duration(300 * time.Second)
ln, err := utp.NewSocket("udp", listen)
if err != nil {
return nil, nil, err
}
lgen := &mux.UTPLayerGen{}
mx := mux.NewMux(ln, ln.Addr(), lgen)
go mx.Serve()
sock1 := mx.Listen(72)
trans, err := InitUTPTransport(sock1, dialTimeout, rpcTimeout, maxConnIdle)
if err != nil {
return nil, nil, err
}
return conf, trans, nil
}
func TestUTPJoin(t *testing.T) {
// Prepare to create 2 nodes
c1, t1, err := prepRingUTP(30025)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
<-time.After(300 * time.Millisecond)
c2, t2, err := prepRingUTP(30026)
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 TestUTPLeave(t *testing.T) {
c1, t1, err := prepRingUTP(30027)
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)
}
<-time.After(200 * time.Millisecond)
c2, t2, err := prepRingUTP(30028)
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(300 * time.Millisecond)
// Node 1 should leave
r1.Leave()
t1.Shutdown()
// Wait for stabilization
<-time.After(1000 * 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])
}
}
}