-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstadis_test.go
329 lines (286 loc) · 8.99 KB
/
stadis_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
package stadis
import (
"bytes"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"testing"
"time"
)
const (
appleHostName = "plant.fruit.apple"
lionHostName = "animal.land.lion"
tigerHostName = "animal.land.tiger"
)
var defaultServer = NewApiServer()
func init() {
log.SetFlags(log.Lshortfile)
go http.ListenAndServe(Cli.ApiAddr, defaultServer)
time.Sleep(time.Millisecond)
}
func resetDefaultServer() (err error) {
err = Cli.UpdateConfig(bytes.NewReader(DefaultConfig))
if err != nil {
log.Println(err)
return
}
return
}
func TestApi(t *testing.T) {
err := resetDefaultServer()
if err != nil {
t.Fatal(err)
}
lionPort := "30001"
Cli.UpdateNodeState("animal.land", NodeState{ExternalDown: true})
Cli.ServerStarted(lionHostName, lionPort)
appleDialLion, _ := Cli.DialState(appleHostName, lionPort)
expectedState := ConnState{
Latency: 3 * time.Minute,
}
if expectedState != appleDialLion {
t.Fatal("wrong state for apple dial lion, expected", expectedState, "actual", appleDialLion)
}
applePort := "30003"
Cli.ServerStarted(appleHostName, applePort)
appleDialApple, _ := Cli.DialState(appleHostName, applePort)
expectedState = ConnState{
OK: true,
Latency: 0,
}
if expectedState != appleDialApple {
t.Fatal("latency for connection between processes on the same host should be 0, expected",
expectedState, "actual ", appleDialApple)
}
tigerPort := "30011"
Cli.ServerStarted(tigerHostName, tigerPort)
Cli.UpdateNodeState("animal.land", NodeState{ExternalDown: false})
appleDialTiger, _ := Cli.DialState(appleHostName, tigerPort)
expectedState = ConnState{
OK: true,
Latency: 2 * (1 + 10 + 100 + 100 + 10 + 1) * time.Millisecond,
}
if expectedState != appleDialTiger {
defaultServer.DumpNode("")
t.Fatal("latency should be two times the sum of latency of every node along the path, expected",
expectedState, "actual", appleDialTiger)
}
Cli.UpdateNodeState(appleHostName, NodeState{Latency: 30 * time.Millisecond})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
expectedState = ConnState{
OK: true,
Latency: 2 * (30 + 10 + 100 + 100 + 10 + 1) * time.Millisecond,
}
if expectedState != appleDialTiger {
t.Fatal("wrong state for apple dial tiger, expected", expectedState, "actual", appleDialTiger)
}
Cli.UpdateNodeState("plant.fruit", NodeState{ExternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
expectedState = ConnState{
OK: false,
Latency: 3 * time.Minute,
}
if appleDialTiger != expectedState {
defaultServer.DumpNode("")
t.Fatal("wrong state for apple dial tiger, expected", expectedState, "actual", appleDialTiger)
}
//set NodeState to internal down, the expected state will stay the same.
Cli.UpdateNodeState("plant.fruit", NodeState{InternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger != expectedState {
t.Fatal("wrong state for apple dial tiger, expected", expectedState, "actual", appleDialTiger)
}
Cli.UpdateNodeState("plant.fruit", NodeState{})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != true {
t.Fatal("apple dial tiger should be ok.")
}
Cli.UpdateNodeState("plant", NodeState{InternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when plant internal is down.")
}
Cli.UpdateNodeState("plant", NodeState{ExternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when plant external is down.")
}
Cli.UpdateNodeState("plant", NodeState{})
Cli.UpdateNodeState("animal", NodeState{ExternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when animal external is down.")
}
Cli.UpdateNodeState("animal", NodeState{InternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when animal internal is down.")
}
Cli.UpdateNodeState("animal", NodeState{})
Cli.UpdateNodeState("animal.land", NodeState{ExternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when animal.land external is down.")
}
Cli.UpdateNodeState("animal.land", NodeState{InternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when animal.land internal is down.")
}
Cli.UpdateNodeState("animal.land", NodeState{})
Cli.UpdateNodeState(tigerHostName, NodeState{ExternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when tiger external is down.")
}
Cli.UpdateNodeState(tigerHostName, NodeState{InternalDown: true})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != false {
t.Fatal("apple dial tiger should not be ok when tiger internal is down.")
}
if appleDialTiger.Latency != 3*time.Minute {
t.Fatal("apple dial tiger's latency should be 15 minutes.")
}
Cli.UpdateNodeState(tigerHostName, NodeState{})
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
if appleDialTiger.OK != true {
defaultServer.DumpNode("")
t.Fatal("apple dial tiger should be ok.")
}
err = Cli.ServerStopped(tigerHostName, tigerPort)
if err != nil {
t.Fatal(err)
}
appleDialTiger, _ = Cli.DialState(appleHostName, tigerPort)
expectedState = ConnState{
OK: false,
Latency: 2 * (30 + 10 + 100 + 100 + 10 + 1) * time.Millisecond,
}
if appleDialTiger != expectedState {
defaultServer.DumpNode("")
t.Fatal("apple dial tiger latency should be ", expectedState, "actual", appleDialTiger)
}
}
func TestLatency(t *testing.T) {
err := resetDefaultServer()
if err != nil {
t.Fatal(err)
}
appleHostName := "plant.fruit.apple"
applePort := "30003"
appleListener, err := net.Listen("tcp", "localhost:"+applePort)
if err != nil {
t.Fatal(err)
}
mListener, err := NewListener(appleListener, appleHostName)
if err != nil {
t.Fatal(err)
}
go echoServe(mListener)
tigerHostName := "animal.land.tiger"
tigerDialFunc := NewDialFunc(tigerHostName, 0)
tigerConn, err := tigerDialFunc("tcp", "localhost:30003")
if err != nil {
t.Fatal(err)
}
defer tigerConn.Close()
tigerToAppleState, _ := Cli.ConnState(localPort(tigerConn), applePort, nil)
buf := make([]byte, 4096)
before := time.Now()
tigerConn.Write(buf)
tigerConn.Write(buf)
_, err = tigerConn.Read(buf)
if err != nil {
t.Fatal(err)
}
tigerConn.Read(buf)
latency := time.Now().Sub(before)
if latency < tigerToAppleState.Latency*2 || latency > tigerToAppleState.Latency*3 {
defaultServer.DumpNode("")
t.Fatal("expected latency", tigerToAppleState.Latency*2, "actual", latency)
}
}
func TestProxy(t *testing.T) {
originAddr := "localhost:6546"
originListener, err := net.Listen("tcp", originAddr)
if err != nil {
t.Fatal(err)
}
go echoServe(originListener)
resetDefaultServer()
proxyName := "matter.metal.gold"
proxyPort := "6577"
localName := "animal.air.eagle"
err = Cli.StartProxy(localName, proxyName, proxyPort, originAddr)
if err != nil {
t.Fatal(err)
}
defer Cli.StopProxy(proxyPort)
testConn, err := net.DialTimeout("tcp", "localhost:"+proxyPort, time.Second)
if err != nil {
t.Fatal(err)
}
before := time.Now()
writeData := bytes.Repeat([]byte("abcdefghi"), 1000)
testConn.Write(writeData)
readData, err := ioutil.ReadAll(io.LimitReader(testConn, int64(len(writeData))))
if err != nil {
t.Fatal(err)
}
duration := time.Now().Sub(before)
t.Log(duration)
if duration < time.Millisecond*444 || duration > time.Millisecond*555 {
t.Error("wrong latency, expected", 444*time.Millisecond, "actual", duration)
defaultServer.DumpNode("")
}
if !bytes.Equal(readData, writeData) {
t.Error("read data should be equal to written data")
}
err = testConn.Close()
if err != nil {
t.Fatal(err)
}
// change proxy's client name.
newLocalName := "matter.metal.gold"
err = Cli.UpdateProxy(newLocalName, proxyPort)
if err != nil {
t.Fatal(err)
}
newConn, err := net.Dial("tcp", "localhost:"+proxyPort)
if err != nil {
t.Fatal(err)
}
defer newConn.Close()
before = time.Now()
newConn.Write(writeData)
_, err = io.ReadFull(newConn, readData)
if err != nil {
t.Fatal(err)
}
duration = time.Now().Sub(before)
t.Log(duration)
if duration > time.Millisecond*10 {
t.Error("wrong latency, expected less than", 10*time.Millisecond, "actual", duration)
defaultServer.DumpNode("")
}
if !bytes.Equal(readData, writeData) {
t.Error("read data should be equal to wrttien data")
}
}
func echoServe(listener net.Listener) {
for {
conn, err := listener.Accept()
if err != nil {
return
}
go func() {
n, err := io.Copy(conn, conn)
if err != nil {
log.Println(n, err)
}
conn.Close()
}()
}
}