-
Notifications
You must be signed in to change notification settings - Fork 2
/
uniconn_test.go
179 lines (154 loc) · 3.85 KB
/
uniconn_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
package mockconn
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
)
// go test -v -run=TestUniConnThroughput
func TestUniConnThroughput(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}
uc, err := NewUniConn(conf)
require.Nil(t, err)
require.NotNil(t, uc)
fmt.Println("target tp is", tp)
sendChan := make(chan []int64)
recvChan := make(chan []int64)
nPacket := 100
go WritePacktes(uc, nPacket, sendChan)
go ReadPackets(uc, nPacket, conf.Latency, recvChan)
<-sendChan
<-recvChan
}
}
// go test -v -run=TestSetReadDeadline
func TestSetReadDeadline(t *testing.T) {
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: uint(16), Latency: 100 * time.Millisecond}
uc, err := NewUniConn(conf)
require.Nil(t, err)
require.NotNil(t, uc)
uc.SetReadDeadline(time.Now().Add(time.Second))
b := make([]byte, 1024)
n, err := uc.Read(b)
require.NotNil(t, err)
require.Equal(t, 0, n)
n, err = uc.Write(b)
require.Nil(t, err)
require.Equal(t, 1024, n)
n, err = uc.Read(b)
require.Nil(t, err)
require.Equal(t, 1024, n)
}
// go test -v -run=TestClose
func TestClose(t *testing.T) {
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: uint(16), Latency: 100 * time.Millisecond}
uc, err := NewUniConn(conf)
require.Nil(t, err)
require.NotNil(t, uc)
b := []byte("hello")
n, err := uc.Write(b)
require.Nil(t, err)
require.Equal(t, len(b), n)
b2 := make([]byte, 1024)
n, err = uc.Read(b2)
require.Nil(t, err)
require.Equal(t, len(b), n)
uc.Write(b)
uc.CloseRead()
n, err = uc.Read(b2)
require.NotNil(t, err)
require.Equal(t, 0, n)
t.Log("After close read, read err ", err)
uc.CloseWrite()
n, err = uc.Write(b)
require.NotNil(t, err)
require.Equal(t, 0, n)
t.Log("After close write, write err ", err)
}
// go test -v -run=TestRateLimiter
func TestRateLimiter(t *testing.T) {
lim := 2000
r := rate.NewLimiter(rate.Limit(lim), 1)
count := 10000
start := time.Now()
for i := 0; i < count; i++ {
r.Wait(context.Background())
}
d := time.Since(start)
fmt.Printf("Count %v took %v, average is %.1f, expected is %v\n",
count, d, float64(count)/d.Seconds(), lim)
}
// go test -v -run=TestCloseRead
func TestCloseRead(t *testing.T) {
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: uint(16), Latency: 50 * time.Millisecond, WriteTimeout: 3 * time.Second}
uc, err := NewUniConn(conf)
require.Nil(t, err)
require.NotNil(t, uc)
writeCh := make(chan struct{})
go func() {
i := 0
for i = 0; i < 1000; i++ {
b := make([]byte, 1024)
n, err := uc.Write(b)
if err != nil {
t.Log("write err", err)
break
} else {
require.Equal(t, n, len(b))
}
}
close(writeCh)
}()
go func() {
i := 0
for i = 0; i < 1000; i++ {
b := make([]byte, 1024)
n, err := uc.Read(b)
if err != nil {
t.Log("read err", err)
break
} else {
require.Equal(t, n, len(b))
}
if i == 100 {
err := uc.CloseRead()
if err != nil {
t.Log("CloseRead err", err)
}
}
}
require.Equal(t, 101, i)
}()
<-writeCh
}
// go test -v -run=TestTimeout
func TestTimeout(t *testing.T) {
conf := &ConnConfig{Addr1: "Alice", Addr2: "Bob", Throughput: uint(16), Latency: 20 * time.Millisecond,
WriteTimeout: 2 * time.Second, ReadTimeout: 3 * time.Second}
uc, err := NewUniConn(conf)
require.Nil(t, err)
require.NotNil(t, uc)
for i := 0; i < 1000; i++ {
b := []byte("hello")
n, err := uc.Write(b)
if err != nil {
require.Equal(t, 0, n)
require.Equal(t, context.DeadlineExceeded, err)
break
}
}
for i := 0; i < 1000; i++ {
b := make([]byte, 1024)
n, err := uc.Read(b)
if err != nil {
require.Equal(t, 0, n)
require.Equal(t, context.DeadlineExceeded, err)
break
}
}
}