-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_test.go
183 lines (135 loc) · 4.45 KB
/
policy_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
package main
import (
"github.com/a-pavlov/ged2k/proto"
"testing"
"time"
)
func Test_Peers(t *testing.T) {
e1, _ := proto.FromString("192.168.1.1:3333")
e2, _ := proto.FromString("212.168.1.1:3330")
e3, _ := proto.FromString("10.0.0.11:3330")
p1 := Peer{endpoint: e1}
p2 := Peer{endpoint: e2}
p3 := Peer{endpoint: e3}
if !LeftBetterRightToConnect(&p1, &p2) {
t.Errorf("p1 not better than p2 - wrong")
}
if LeftBetterRightToConnect(&p3, &p1) {
t.Errorf("p3 better than p1 - wrong")
}
}
func Test_PolicyBase(t *testing.T) {
policy := NewPolicy(4)
p1 := policy.FindConnectCandidate(time.Now())
if p1 != nil {
t.Errorf("Connect candidate?")
}
e1, _ := proto.FromString("192.168.1.1:3333")
e2, _ := proto.FromString("212.168.1.1:3330")
e3, _ := proto.FromString("10.0.0.11:3330")
e4, _ := proto.FromString("217.221.1.1:3330")
if !policy.AddPeer(&Peer{endpoint: e1}) {
t.Errorf("Can not add peer")
}
if !policy.AddPeer(&Peer{endpoint: e2}) {
t.Errorf("Can not add peer")
}
if !policy.AddPeer(&Peer{endpoint: e3}) {
t.Errorf("Can not add peer")
}
if !policy.AddPeer(&Peer{endpoint: e4}) {
t.Errorf("Can not add peer")
}
if policy.AddPeer(&Peer{endpoint: e3}) {
t.Errorf("Peer was added as duplicate")
}
tm := time.Now()
peer1 := policy.FindConnectCandidate(tm)
if peer1 == nil {
t.Errorf("Can not find connect candidate")
}
peer1.peerConnection = &PeerConnection{}
peer2 := policy.FindConnectCandidate(tm)
if peer2 == nil {
t.Errorf("Can not find connect candidate 2")
}
if peer1 == peer2 {
t.Errorf("Connect candidates equal")
}
if !peer1.endpoint.IsLocalAddress() || !peer2.endpoint.IsLocalAddress() {
t.Errorf("Wrong connect candidates")
}
peer2.NextConnection = tm.Add(time.Second * time.Duration(2))
peer3 := policy.FindConnectCandidate(tm)
peer3.peerConnection = &PeerConnection{}
peer4 := policy.FindConnectCandidate(tm)
peer4.peerConnection = &PeerConnection{}
peer5 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(1)))
if peer5 != nil {
t.Errorf("Found connectot candidate when candidates exchosted")
}
peer6 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(3)))
if peer6 != peer2 {
t.Errorf("Reusing peer after next connection doesn't work")
}
peer6.NextConnection = tm.Add(time.Second * time.Duration(6))
peer1.peerConnection = nil
peer1.LastConnected = tm.Add(time.Second * time.Duration(4))
peer1.FailCount += 1
if policy.FindConnectCandidate(tm.Add(time.Second*time.Duration(5))) != nil {
t.Errorf("Found unexpected connect candidate")
}
peer8 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(7)))
if peer6 != peer8 {
t.Errorf("Can not take peer after next connect")
}
peer8.peerConnection = &PeerConnection{}
peer9 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(MIN_RECONNECT_TIMEOUT_SEC+5)))
if peer9 != peer1 {
t.Errorf("Can not take peer after fail")
}
peer9.peerConnection = &PeerConnection{}
peer3.peerConnection = nil
peer3.FailCount = 3
peer4.peerConnection = nil
peer4.FailCount = 2
peer10 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(MIN_RECONNECT_TIMEOUT_SEC*6)))
if peer10 != peer4 {
t.Errorf("Wrong peer by fail count")
}
peer10.peerConnection = &PeerConnection{}
peer11 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(MIN_RECONNECT_TIMEOUT_SEC*6)))
if peer11 != peer3 {
t.Errorf("Wrong peer by fail count 2")
}
e5, _ := proto.FromString("217.120.1.1:3330")
e6, _ := proto.FromString("217.120.1.2:3330")
if policy.AddPeer(&Peer{endpoint: e5}) {
t.Errorf("Peer was added - error")
}
peer3.peerConnection = nil
peer4.peerConnection = nil
if policy.AddPeer(&Peer{endpoint: e5}) {
t.Errorf("Peer was added - error")
}
peer3.FailCount = 6
peer4.FailCount = 7
if !policy.AddPeer(&Peer{endpoint: e5}) {
t.Errorf("Peer was added - error")
}
if !policy.AddPeer(&Peer{endpoint: e6}) {
t.Errorf("Peer was added - error")
}
peerA1 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(MIN_RECONNECT_TIMEOUT_SEC*7)))
if peerA1.endpoint != e5 && peerA1.endpoint != e6 {
t.Errorf("New added peer not found")
}
peerA1.peerConnection = &PeerConnection{}
peerA2 := policy.FindConnectCandidate(tm.Add(time.Second * time.Duration(MIN_RECONNECT_TIMEOUT_SEC*7)))
if peerA2.endpoint != e5 && peerA2.endpoint != e6 {
t.Errorf("New added peer not found 2")
}
if policy.erasePeers() {
t.Errorf("Erase peer returned true - error")
}
}