-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
253 lines (199 loc) · 5.26 KB
/
client_test.go
File metadata and controls
253 lines (199 loc) · 5.26 KB
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
package ws
import (
"net/url"
log "github.com/sirupsen/logrus"
// "net/url"
"testing"
"time"
)
// TestServerWSHandler implement the handler interface
type TestClientWSHandler struct {
clientId string
WSClient
}
func (h TestClientWSHandler) Process(clientId string, msg WSMsg) (response WSMsg, err error) {
log.Info("got new msg ")
switch msg.Type() {
case testEmptyResponse:
response, err = EncodeResponse(msg, nil)
return response, nil
case testTimeout, testNoResponse:
return NoResponse, nil
}
return NoResponse, nil
}
// func (h TestClientWSHandler) Accept(clientId string) error {
// log.Info("WS server running accept ", clientId)
// return nil
// }
var countClosed int
var countRedial int
func (h TestClientWSHandler) Closed(wsConn *WSConn) {
countClosed++
log.Infof("WS client %s nclosed %d", wsConn.ClientId, countClosed)
}
func (h TestClientWSHandler) BeforeRedial() {
countRedial++
log.Infof("WS client before redial count=%d", countRedial)
}
func (h TestClientWSHandler) AfterRedial(wsConn *WSConn) {
log.Infof("WS client after redial %s count %d", wsConn.ClientId, countRedial)
}
func dial(t *testing.T, u url.URL, clientId string) *WSConn {
conn, err := WebSocketDial(u, clientId, TestClientWSHandler{})
if err != nil {
t.Fatalf("Dial: %v", err)
}
return conn
}
var msgToken = "secrettoken"
func Test_ClientDial(t *testing.T) {
s := newServer(t)
defer s.Close()
conn := dial(t, *s.url, "client123DIAL")
defer conn.Close()
msg, err := Encode(testEmptyResponse, &msgToken, &data1)
if err != nil {
t.Fatal("cannot encode", err)
}
response, err := conn.WriteAndWaitResponse(msg)
if err != nil ||
msg.Sequence() != response.Sequence() ||
msg.Type() != response.Type() {
t.Fatal("failed to get response", err, msg, response)
}
log.Info("received successful response")
}
func Test_ClientNormalClosure(t *testing.T) {
s := newServer(t)
defer s.Close()
countClosed = 0
conn := dial(t, *s.url, "client1DUP")
conn2 := dial(t, *s.url, "client12DUP")
conn3 := dial(t, *s.url, "client123DUP")
conn2.Close()
conn.Close()
conn3.Close()
time.Sleep(time.Second * 2)
if countClosed != 3 {
t.Fatal("failed to close ws ", countClosed)
}
}
func Test_ClientDialDuplicated(t *testing.T) {
s := newServer(t)
defer s.Close()
countClosed = 0
AutoRedial = false
defer func() { AutoRedial = true }()
conn := dial(t, *s.url, "client123DUP")
defer conn.Close()
conn2 := dial(t, *s.url, "client123DUP")
defer conn2.Close()
conn3 := dial(t, *s.url, "client123DUP")
defer conn3.Close()
time.Sleep(time.Second * 2)
if countClosed != 2 {
t.Fatal("failed to close ws ", countClosed)
}
}
func Test_ClientRedial(t *testing.T) {
s := newServer(t)
defer s.Close()
countRedial = 0
conn := dial(t, *s.url, "client123REDIAL")
defer conn.Close()
conn2 := dial(t, *s.url, "client1234REDIAL")
defer conn2.Close()
in := simpleMsg{N: 101, Msg: "conn 2 first msg"}
out := simpleAnswer{}
err := conn.RPC(testEmptyResponse, &msgToken, &in, nil)
if err != nil {
t.Fatal("cannot rpc", err)
}
err = conn.RPC(testTimeout, &msgToken, &in, &out)
if err == nil || err != ErrorTimeout {
t.Fatal("cannot rpc timeout", err)
}
n := 3
i := 0
for i = 0; i < n; i++ {
time.Sleep(time.Second * 1)
log.Info("msg ", i)
err = conn.RPC(testEmptyResponse, &msgToken, &in, nil)
if err == nil {
break
}
}
if i >= n {
t.Fatal("cannot redial ", err)
}
err = conn.RPC(testEmptyResponse, &msgToken, &in, nil)
if err != nil {
t.Fatal("cannot rpc2", err)
}
err = conn2.RPC(testEmptyResponse, &msgToken, &in, nil)
if err != nil {
t.Fatal("cannot rpc3", err)
}
time.Sleep(time.Second * 3)
if len(webSocketMap) != 2 || countConnections != 2 || countRedial != 1 {
t.Fatal("wrong total", len(webSocketMap), countConnections, countRedial)
}
}
var (
savedClientPingPeriod time.Duration
savedServerPingPeriod time.Duration
)
func setAndSaveEnv(cPingPeriod time.Duration, serverPingPeriod time.Duration) {
countConnections = 0
countRedial = 0
savedClientPingPeriod = clientPingPeriod
savedServerPingPeriod = pingPeriod
clientPingPeriod = cPingPeriod
pingPeriod = serverPingPeriod
}
func resetEnv() {
pingPeriod = savedServerPingPeriod
clientPingPeriod = savedClientPingPeriod
}
func Test_ClientRedialPINGFailure(t *testing.T) {
setAndSaveEnv(time.Second*2, time.Second*10)
defer resetEnv()
s := newServer(t)
defer s.Close()
conn := dial(t, *s.url, "client123PING")
defer conn.Close()
conn2 := dial(t, *s.url, "client1234PING")
defer conn2.Close()
in := simpleMsg{N: 101, Msg: "conn 2 first msg"}
// out := simpleAnswer{}
err := conn.RPC(testEmptyResponse, &msgToken, &in, nil)
if err != nil {
t.Fatal("cannot rpc", err)
}
n := 3
i := 0
for i = 0; i < n; i++ {
time.Sleep(time.Second * 3)
log.Info("msg ", i)
err = conn.RPC(testEmptyResponse, &msgToken, &in, nil)
if err == nil {
break
}
}
if i >= n {
t.Fatal("cannot redial ", err)
}
err = conn.RPC(testEmptyResponse, &msgToken, &in, nil)
if err != nil {
t.Fatal("cannot rpc2", err)
}
err = conn2.RPC(testEmptyResponse, &msgToken, &in, nil)
if err != nil {
t.Fatal("cannot rpc3", err)
}
time.Sleep(time.Second * 3)
if len(webSocketMap) != 2 || countConnections != 2 || countRedial != 2 {
t.Fatal("wrong total", len(webSocketMap), countConnections, countRedial)
}
}