-
Notifications
You must be signed in to change notification settings - Fork 2
/
mockconn_test.go
133 lines (111 loc) · 3.9 KB
/
mockconn_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
package mockconn
import (
"fmt"
"log"
"math"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// go test -v -run=TestBidirection
func TestBidirection(t *testing.T) {
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: uint(256), Latency: 100 * time.Millisecond}
log.Printf("Going to test bi-direction communicating, throughput is %v, latency is %v \n",
conf.Throughput, conf.Latency)
aliceConn, bobConn, err := NewMockConn(conf)
require.NotNil(t, aliceConn)
require.NotNil(t, bobConn)
require.Nil(t, err)
// Alice send to Bob
nPackets := 100
i := 0
sendChan := make(chan []int64)
recvChan := make(chan []int64)
go WritePacktes(aliceConn, nPackets, sendChan)
go ReadPackets(bobConn, nPackets, conf.Latency, recvChan)
sendSeq := <-sendChan
recvSeq := <-recvChan
minLen := int(math.Min(float64(len(sendSeq)), float64(len(recvSeq))))
for i = 0; i < minLen; i++ {
if sendSeq[i] != recvSeq[i] {
log.Printf("%v sendSeq[%v] %v != %v recvSeq[%v] %v \n",
aliceConn.LocalAddr(), i, sendSeq[i], bobConn.LocalAddr(), i, recvSeq[i])
}
}
if i == nPackets {
log.Printf("%v write to %v %v packets, %v receive %v packets in the same sequence\n",
aliceConn.LocalAddr(), bobConn.LocalAddr(), nPackets, bobConn.LocalAddr(), nPackets)
}
// Bob send to Alice
nPackets = 50
go WritePacktes(bobConn, nPackets, sendChan)
go ReadPackets(aliceConn, nPackets, conf.Latency, recvChan)
sendSeq = <-sendChan
recvSeq = <-recvChan
minLen = int(math.Min(float64(len(sendSeq)), float64(len(recvSeq))))
for i = 0; i < minLen; i++ {
if sendSeq[i] != recvSeq[i] {
log.Printf("%v sendSeq[%v] %v != %v recvSeq[%v] %v \n",
bobConn.LocalAddr(), i, sendSeq[i], aliceConn.LocalAddr(), i, recvSeq[i])
}
}
if i == nPackets {
log.Printf("%v write to %v %v packets, %v receive %v packets in the same sequence\n",
bobConn.LocalAddr(), aliceConn.LocalAddr(), nPackets, aliceConn.LocalAddr(), nPackets)
}
}
// go test -v -run=TestNetConnThroughput
func TestNetConnThroughput(t *testing.T) {
tpBase := uint(256)
for i := 1; i <= 4; i++ {
tp := tpBase * uint(i)
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: tp, Latency: 20 * time.Millisecond}
log.Printf("Going to test throughput at %v packets/s, latency %v\n", conf.Throughput, conf.Latency)
aliceConn, bobConn, err := NewMockConn(conf)
require.NotNil(t, aliceConn)
require.NotNil(t, bobConn)
require.Nil(t, err)
nPackets := 256
sendChan := make(chan []int64)
recvChan := make(chan []int64)
go WritePacktes(aliceConn, nPackets, sendChan)
go ReadPackets(bobConn, nPackets, conf.Latency, recvChan)
<-sendChan
<-recvChan
fmt.Println()
}
}
// go test -v -run=TestHighLatency
func TestHighLatency(t *testing.T) {
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: uint(128), Latency: 500 * time.Millisecond}
log.Printf("Going to test throughput at %v packets/s, high latency %v\n", conf.Throughput, conf.Latency)
aliceConn, bobConn, err := NewMockConn(conf)
require.NotNil(t, aliceConn)
require.NotNil(t, bobConn)
require.Nil(t, err)
nPackets := 256
sendChan := make(chan []int64)
recvChan := make(chan []int64)
go WritePacktes(aliceConn, nPackets, sendChan)
go ReadPackets(bobConn, nPackets, conf.Latency, recvChan)
<-sendChan
<-recvChan
}
// go test -v -run=TestLoss
func TestLoss(t *testing.T) {
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: uint(128), Latency: 20 * time.Millisecond, Loss: 0.01}
log.Printf("Going to test throughput at %v packets/s, latency %v, loss :%v \n", conf.Throughput, conf.Latency, conf.Loss)
aliceConn, bobConn, err := NewMockConn(conf)
require.NotNil(t, aliceConn)
require.NotNil(t, bobConn)
require.Nil(t, err)
nPackets := 256
sendChan := make(chan []int64)
recvChan := make(chan []int64)
go WritePacktes(aliceConn, nPackets, sendChan)
go ReadPackets(bobConn, nPackets, conf.Latency, recvChan)
<-sendChan
aliceConn.Close()
bobConn.Close()
<-recvChan
}