-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket_goji_test.go
384 lines (359 loc) · 9.03 KB
/
websocket_goji_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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2017 Joseph deBlaquiere. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package websocket_client
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"sync"
"sync/atomic"
"testing"
"time"
"goji.io"
"goji.io/pat"
iwebsocket "github.com/kataras/iris/websocket"
)
type gjwsServer struct {
clients []*wsClient
listMutex sync.Mutex
mux *goji.Mux
srv *http.Server
}
func (gjwss *gjwsServer) connect(con ClientConnection) {
gjwss.listMutex.Lock()
defer gjwss.listMutex.Unlock()
// fail compile here if server connection doesn't satisfy interface
validateInterface(con)
// c := &wsClient{con: con, wss: wss}
c := &wsClient{con: con}
gjwss.clients = append(gjwss.clients, c)
// fmt.Printf("Connect # active clients : %d\n", len(wss.clients))
con.OnMessage(c.echoRawMessage)
con.On("echo", c.echoString)
con.On("len", c.lenString)
con.On("reverse", c.reverseString)
con.OnDisconnect(c.disconnect)
}
func (gjwss *gjwsServer) disconnect(wsc *wsClient) {
gjwss.listMutex.Lock()
defer gjwss.listMutex.Unlock()
l := len(gjwss.clients)
if l == 0 {
panic("WSS:trying to delete client from empty list")
}
for p, v := range gjwss.clients {
if v == wsc {
gjwss.clients[p] = gjwss.clients[l-1]
gjwss.clients = gjwss.clients[:l-1]
// fmt.Printf("Disconnect # active clients : %d\n", len(wss.clients))
return
}
}
panic("WSS:trying to delete client not in list")
}
func (gjwss *gjwsServer) index(w http.ResponseWriter, r *http.Request) {
t := time.Now().Unix()
w.Header().Set("Content-Type", "application/json")
response := indexResponse{
RequestIP: r.RemoteAddr,
Time: int64(t),
}
j, _ := json.Marshal(response)
w.Write(j)
}
func (gjwss *gjwsServer) startup() {
r := goji.NewMux()
r.HandleFunc(pat.Get("/index.html"), gjwss.index)
wsh := WSHandler{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Config: iwebsocket.Config{
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
PingPeriod: 9 * 6 * time.Second,
PongTimeout: 60 * time.Second,
ReadBufferSize: 4096,
WriteBufferSize: 4096,
BinaryMessages: true,
},
}
wsh.OnConnect(gjwss.connect)
r.HandleFunc(pat.Get("/echo"), wsh.HandleRequest)
gjwss.srv = &http.Server{
Addr: ":8080",
Handler: r,
}
go gjwss.srv.ListenAndServe()
tries := 0
for {
_, err := http.Get("http://127.0.0.1:8080/")
if err == nil {
break
}
tries += 1
if tries > 30 {
fmt.Println("Server not responding")
return
}
time.Sleep(1 * time.Second)
}
}
func (gjwss *gjwsServer) shutdown() {
// for _, v := range gwss.clients {
// v.con.Disconnect()
// }
gjwss.srv.Close()
time.Sleep(1 * time.Second)
}
func TestGojiClientConnectDisconnect(t *testing.T) {
var gjwss gjwsServer
var client ClientConnection
var err error
connected := true
tries_left := int(10)
gjwss.startup()
d := new(WSDialer)
client = nil
for (client == nil) && (tries_left > 0) {
client, _, err = d.Dial("ws://127.0.0.1:8080/echo", nil, iwebsocket.Config{
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
PingPeriod: 9 * 6 * time.Second,
PongTimeout: 60 * time.Second,
ReadBufferSize: 4096,
WriteBufferSize: 4096,
BinaryMessages: true,
})
if err != nil {
fmt.Println("Dialer error:", err)
if tries_left > 0 {
time.Sleep(1 * time.Second)
tries_left -= 1
} else {
t.Fail()
}
}
}
if client == nil {
fmt.Println("Dialer returned nil client")
t.Fail()
} else {
got_reply := false
client.On("echo_reply", func(s string) {
// fmt.Println("client echo_reply", s)
got_reply = true
})
client.OnDisconnect(func() {
// fmt.Println("client echo_reply", s)
connected = false
})
client.Emit("echo", "hello")
// fmt.Println("Emit complete")
time.Sleep(1 * time.Second)
if !got_reply {
fmt.Println("No echo response")
t.Fail()
}
}
c := gjwss.clients[0]
c.con.Disconnect()
tries_left = 5
for connected && (tries_left > 0) {
time.Sleep(1 * time.Second)
tries_left -= 1
}
if connected {
fmt.Println("Disconnect not received by client")
t.Fail()
}
gjwss.shutdown()
}
func TestGojiMixedMessagesConcurrency(t *testing.T) {
var gjwss gjwsServer
var client ClientConnection
var err error
tries_left := int(10)
gjwss.startup()
d := new(WSDialer)
client = nil
for (client == nil) && (tries_left > 0) {
client, _, err = d.Dial("ws://127.0.0.1:8080/echo", nil, iwebsocket.Config{
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
PingPeriod: 9 * 6 * time.Second,
PongTimeout: 60 * time.Second,
ReadBufferSize: 4096,
WriteBufferSize: 4096,
BinaryMessages: true,
})
if err != nil {
fmt.Println("Dialer error:", err)
if tries_left > 0 {
time.Sleep(1 * time.Second)
tries_left -= 1
} else {
t.Fail()
}
}
}
if client == nil {
fmt.Println("Dialer returned nil client")
t.Fail()
} else {
cycles := int32(500)
echo_count := int32(0)
len_count := int32(0)
reverse_count := int32(0)
raw_count := int32(0)
// fmt.Println("Dial complete")
client.On("echo_reply", func(s string) {
//fmt.Println("client echo_reply", s)
atomic.AddInt32(&echo_count, 1)
})
client.On("len_reply", func(i int) {
// fmt.Printf("client len_reply %d\n", i)
atomic.AddInt32(&len_count, 1)
})
client.On("reverse_reply", func(s string) {
// fmt.Println("client reverse_reply", s)
atomic.AddInt32(&reverse_count, 1)
})
client.OnMessage(func(b []byte) {
// fmt.Println("client raw_reply", hex.EncodeToString(b))
atomic.AddInt32(&raw_count, 1)
})
// fmt.Println("ON complete")
var wg sync.WaitGroup
wg.Add(4)
go func() {
defer wg.Done()
for i := 0; i < int(cycles); i++ {
s := fmt.Sprintf("hello %d", i)
if client.Emit("echo", s) != nil {
fmt.Println("error serializing echo request:", s)
t.Fail()
}
}
}()
go func() {
defer wg.Done()
for i := 0; i < int(cycles); i++ {
s := fmt.Sprintf("hello %d", i)
if client.Emit("reverse", s) != nil {
fmt.Println("error serializing reverse request:", s)
t.Fail()
}
}
}()
go func() {
defer wg.Done()
for i := 0; i < int(cycles); i++ {
s := make([]byte, i, i)
for j := 0; j < i; j++ {
s[j] = byte('a')
}
if client.Emit("len", string(s)) != nil {
fmt.Println("error serializing len request:", string(s))
t.Fail()
}
}
}()
go func() {
defer wg.Done()
for i := 0; i < int(cycles); i++ {
bb := make([]byte, 8)
binary.BigEndian.PutUint64(bb, uint64(i))
if client.EmitMessage(bb) != nil {
fmt.Println("error serializing raw request:", hex.EncodeToString(bb))
t.Fail()
}
}
}()
// ensure all messages sent
wg.Wait()
// fmt.Println("Emit complete")
// wait until we complete or timeout after 1 minute
for i := 0; i < 60; i++ {
if (atomic.LoadInt32(&echo_count) == cycles) &&
(atomic.LoadInt32(&len_count) == cycles) &&
(atomic.LoadInt32(&reverse_count) == cycles) &&
(atomic.LoadInt32(&raw_count) == cycles) {
break
}
time.Sleep(1 * time.Second)
}
// fmt.Printf("echo, len, raw = %d, %d, %d\n", echo_count, len_count, raw_count)
if echo_count != cycles {
fmt.Printf("echo count mismatch, %d != %d\n", echo_count, cycles)
t.Fail()
}
if len_count != cycles {
fmt.Printf("len count mismatch, %d != %d\n", len_count, cycles)
t.Fail()
}
if raw_count != cycles {
fmt.Printf("echo count mismatch, %d != %d\n", raw_count, cycles)
t.Fail()
}
}
gjwss.shutdown()
}
func TestGojiConnectAndWait(t *testing.T) {
var gjwss gjwsServer
var client ClientConnection
var err error
tries_left := int(10)
gjwss.startup()
d := new(WSDialer)
client = nil
for (client == nil) && (tries_left > 0) {
client, _, err = d.Dial("ws://127.0.0.1:8080/echo", nil, iwebsocket.Config{
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
PingPeriod: 9 * 6 * time.Second,
PongTimeout: 60 * time.Second,
ReadBufferSize: 4096,
WriteBufferSize: 4096,
BinaryMessages: true,
})
if err != nil {
fmt.Println("Dialer error:", err)
if tries_left > 0 {
time.Sleep(1 * time.Second)
tries_left -= 1
} else {
t.Fail()
}
}
}
// fail compile here if client doesn't satisfy common interface
validateInterface(client)
if client == nil {
fmt.Println("Dialer returned nil client")
t.Fail()
} else {
// the wait here is longer than the Timeout, so the server will disconnect if
// ping/pong messages are not correctly triggered
for i := 0; i < 65; i++ {
// fmt.Printf("(sleeping) %s\n", time.Now().Format("2006-01-02 15:04:05.000000"))
time.Sleep(1 * time.Second)
}
got_reply := false
client.On("echo_reply", func(s string) {
// fmt.Println("client echo_reply", s)
got_reply = true
})
// fmt.Println("ON complete")
client.Emit("echo", "hello")
// fmt.Println("Emit complete")
time.Sleep(1 * time.Second)
if !got_reply {
fmt.Println("No echo response")
t.Fail()
}
}
gjwss.shutdown()
}