forked from apache/dubbo-getty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
485 lines (433 loc) · 14 KB
/
client_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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package getty
import (
"bytes"
"net"
"net/http"
"os"
"strconv"
"sync"
"testing"
"time"
)
import (
"github.com/stretchr/testify/assert"
)
type PackageHandler struct{}
func (h *PackageHandler) Read(ss Session, data []byte) (interface{}, int, error) {
return nil, 0, nil
}
func (h *PackageHandler) Write(ss Session, pkg interface{}) ([]byte, error) {
return nil, nil
}
type MessageHandler struct {
lock sync.Mutex
array []Session
}
func newMessageHandler() *MessageHandler {
return &MessageHandler{}
}
func (h *MessageHandler) SessionNumber() int {
h.lock.Lock()
connNum := len(h.array)
h.lock.Unlock()
return connNum
}
func (h *MessageHandler) OnOpen(session Session) error {
h.lock.Lock()
defer h.lock.Unlock()
h.array = append(h.array, session)
return nil
}
func (h *MessageHandler) OnError(session Session, err error) {}
func (h *MessageHandler) OnClose(session Session) {}
func (h *MessageHandler) OnMessage(session Session, pkg interface{}) {}
func (h *MessageHandler) OnCron(session Session) {}
type Package struct{}
func (p Package) String() string {
return ""
}
func (p Package) Marshal() (*bytes.Buffer, error) { return nil, nil }
func (p *Package) Unmarshal(buf *bytes.Buffer) (int, error) { return 0, nil }
func newSessionCallback(session Session, handler *MessageHandler) error {
var pkgHandler PackageHandler
session.SetName("hello-client-session")
session.SetMaxMsgLen(128 * 1024) // max message package length 128k
session.SetPkgHandler(&pkgHandler)
session.SetEventListener(handler)
session.SetReadTimeout(3e9)
session.SetWriteTimeout(3e9)
session.SetCronPeriod((int)(30e9 / 1e6))
session.SetWaitTime(3e9)
return nil
}
func TestTCPClient(t *testing.T) {
listenLocalServer := func() (net.Listener, error) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
return nil, err
}
go http.Serve(listener, nil)
return listener, nil
}
listener, err := listenLocalServer()
assert.Nil(t, err)
assert.NotNil(t, listener)
addr := listener.Addr().(*net.TCPAddr)
t.Logf("server addr: %v", addr)
clt := NewTCPClient(
WithServerAddress(addr.String()),
WithReconnectInterval(5e8),
WithConnectionNumber(1),
)
assert.NotNil(t, clt)
assert.True(t, clt.ID() > 0)
// assert.Equal(t, clt.endPointType, TCP_CLIENT)
var msgHandler MessageHandler
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}
clt.RunEventLoop(cb)
time.Sleep(1e9)
assert.Equal(t, 1, msgHandler.SessionNumber())
ss := msgHandler.array[0]
ss.SetCompressType(CompressNone)
conn := ss.(*session).Connection.(*gettyTCPConn)
assert.True(t, conn.compress == CompressNone)
beforeWriteBytes := conn.writeBytes
beforeWritePkgNum := conn.writePkgNum
l, err := conn.send([]byte("hello"))
assert.Nil(t, err)
assert.True(t, l == 5)
beforeWritePkgNum.Add(1)
beforeWriteBytes.Add(5)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
assert.Equal(t, beforeWriteBytes, conn.writeBytes)
l, err = ss.WriteBytes([]byte("hello"))
assert.Nil(t, err)
assert.True(t, l == 5)
beforeWriteBytes.Add(5)
beforeWritePkgNum.Add(1)
assert.Equal(t, beforeWriteBytes, conn.writeBytes)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
var pkgs [][]byte
pkgs = append(pkgs, []byte("hello"), []byte("hello"))
l, err = conn.send(pkgs)
assert.Nil(t, err)
assert.True(t, l == 10)
beforeWritePkgNum.Add(2)
beforeWriteBytes.Add(10)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
assert.Equal(t, beforeWriteBytes, conn.writeBytes)
ss.SetCompressType(CompressSnappy)
l, err = ss.WriteBytesArray(pkgs...)
assert.Nil(t, err)
assert.True(t, l == 10)
beforeWritePkgNum.Add(2)
beforeWriteBytes.Add(10)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
assert.Equal(t, beforeWriteBytes, conn.writeBytes)
assert.True(t, conn.compress == CompressSnappy)
batchSize := 128 * 1023
source := make([]byte, batchSize)
for i := 0; i < batchSize; i++ {
source[i] = 't'
}
l, err = ss.WriteBytes(source)
assert.Nil(t, err)
assert.True(t, l == batchSize)
beforeWriteBytes.Add(uint32(batchSize))
beforeWritePkgNum.Add(uint32(batchSize/16/1024) + 1)
assert.Equal(t, beforeWriteBytes, conn.writeBytes)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
batchSize = 32 * 1024
source = make([]byte, batchSize)
for i := 0; i < batchSize; i++ {
source[i] = 't'
}
l, err = ss.WriteBytes(source)
assert.Nil(t, err)
assert.True(t, l == batchSize)
beforeWriteBytes.Add(uint32(batchSize))
beforeWritePkgNum.Add(2)
assert.Equal(t, beforeWriteBytes, conn.writeBytes)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
clt.Close()
assert.True(t, clt.IsClosed())
}
func TestUDPClient(t *testing.T) {
var (
err error
conn *net.UDPConn
sendLen int
totalLen int
)
func() {
ip := net.ParseIP("127.0.0.1")
srcAddr := &net.UDPAddr{IP: ip, Port: 0}
conn, err = net.ListenUDP("udp", srcAddr)
assert.Nil(t, err)
assert.NotNil(t, conn)
}()
defer conn.Close()
addr := conn.LocalAddr()
t.Logf("server addr: %v", addr)
clt := NewUDPClient(
WithServerAddress(addr.String()),
WithReconnectInterval(5e8),
WithConnectionNumber(1),
)
assert.NotNil(t, clt)
assert.True(t, clt.ID() > 0)
// assert.Equal(t, clt.endPointType, UDP_CLIENT)
var msgHandler MessageHandler
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}
clt.RunEventLoop(cb)
time.Sleep(1e9)
assert.Equal(t, 1, msgHandler.SessionNumber())
ss := msgHandler.array[0]
totalLen, sendLen, err = ss.WritePkg(nil, 0)
assert.NotNil(t, err)
assert.True(t, sendLen == 0)
assert.True(t, totalLen == 0)
totalLen, sendLen, err = ss.WritePkg([]byte("hello"), 0)
assert.NotNil(t, err)
assert.True(t, sendLen == 0)
assert.True(t, totalLen == 0)
l, err := ss.WriteBytes([]byte("hello"))
assert.Zero(t, l)
assert.NotNil(t, err)
l, err = ss.WriteBytesArray([]byte("hello"))
assert.Zero(t, l)
assert.NotNil(t, err)
l, err = ss.WriteBytesArray([]byte("hello"), []byte("world"))
assert.Zero(t, l)
assert.NotNil(t, err)
ss.SetCompressType(CompressNone)
host, port, _ := net.SplitHostPort(addr.String())
if len(host) < 8 {
host = "127.0.0.1"
}
remotePort, _ := strconv.Atoi(port)
serverAddr := net.UDPAddr{IP: net.ParseIP(host), Port: remotePort}
udpCtx := UDPContext{
Pkg: "hello",
PeerAddr: &serverAddr,
}
t.Logf("udp context:%s", udpCtx)
udpConn := ss.(*session).Connection.(*gettyUDPConn)
_, err = udpConn.send(udpCtx)
assert.NotNil(t, err)
udpCtx.Pkg = []byte("hello")
beforeWriteBytes := udpConn.writeBytes
_, err = udpConn.send(udpCtx)
beforeWriteBytes.Add(5)
assert.Equal(t, beforeWriteBytes, udpConn.writeBytes)
assert.Nil(t, err)
beforeWritePkgNum := udpConn.writePkgNum
totalLen, sendLen, err = ss.WritePkg(udpCtx, 0)
beforeWritePkgNum.Add(1)
assert.Equal(t, beforeWritePkgNum, udpConn.writePkgNum)
assert.Nil(t, err)
assert.True(t, sendLen == 0)
assert.True(t, totalLen == 0)
clt.Close()
assert.True(t, clt.IsClosed())
msgHandler.array[0].Reset()
assert.Nil(t, msgHandler.array[0].Conn())
// ss.WritePkg([]byte("hello"), 0)
}
func TestNewWSClient(t *testing.T) {
var (
server Server
serverMsgHandler MessageHandler
)
addr := "127.0.0.1:65000"
path := "/hello"
func() {
server = NewWSServer(
WithLocalAddress(addr),
WithWebsocketServerPath(path),
)
newServerSession := func(session Session) error {
return newSessionCallback(session, &serverMsgHandler)
}
go server.RunEventLoop(newServerSession)
}()
time.Sleep(1e9)
client := NewWSClient(
WithServerAddress("ws://"+addr+path),
WithConnectionNumber(1),
)
var msgHandler MessageHandler
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}
client.RunEventLoop(cb)
time.Sleep(1e9)
assert.Equal(t, 1, msgHandler.SessionNumber())
ss := msgHandler.array[0]
ss.SetCompressType(CompressNone)
conn := ss.(*session).Connection.(*gettyWSConn)
assert.True(t, conn.compress == CompressNone)
err := conn.handlePing("hello")
assert.Nil(t, err)
l, err := conn.send("hello")
assert.NotNil(t, err)
assert.True(t, l == 0)
beforeWriteBytes := conn.writeBytes
_, err = conn.send([]byte("hello"))
assert.Nil(t, err)
beforeWriteBytes.Add(5)
assert.Equal(t, beforeWriteBytes, conn.writeBytes)
beforeWritePkgNum := conn.writePkgNum
l, err = ss.WriteBytes([]byte("hello"))
assert.Nil(t, err)
assert.True(t, l == 5)
beforeWritePkgNum.Add(1)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
l, err = ss.WriteBytesArray([]byte("hello"), []byte("hello"))
assert.Nil(t, err)
assert.True(t, l == 10)
beforeWritePkgNum.Add(2)
assert.Equal(t, beforeWritePkgNum, conn.writePkgNum)
err = conn.writePing()
assert.Nil(t, err)
ss.SetReader(nil)
assert.Nil(t, ss.(*session).reader)
ss.SetWriter(nil)
assert.Nil(t, ss.(*session).writer)
assert.Nil(t, ss.(*session).GetAttribute("hello"))
client.Close()
assert.True(t, client.IsClosed())
server.Close()
assert.True(t, server.IsClosed())
}
var (
WssServerCRT = []byte(`-----BEGIN CERTIFICATE-----
MIICHjCCAYegAwIBAgIQKpKqamBqmZ0hfp8sYb4uNDANBgkqhkiG9w0BAQsFADAS
MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw
MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB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ajeJFujpl2jOqnb5PuKJ7fn5HQKGB0qSq3IHsFua2WONXcTW
Vn4gS0k50IaDpW+yl+ArIo0QwbjPIAcFysX10p9dVO7A1uEpHbRDzefem6r9uVGk
i7dOLEoC8hkfk6nJsNEIEqu6
-----END CERTIFICATE-----`)
WssClientCRTFile = "/tmp/client.crt"
)
func DownloadFile(filepath string, content []byte) error {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = out.Write(content)
return err
}
func TestNewWSSClient(t *testing.T) {
var (
err error
server Server
serverMsgHandler MessageHandler
)
os.Remove(WssServerCRTFile)
err = DownloadFile(WssServerCRTFile, WssServerCRT)
assert.Nil(t, err)
defer os.Remove(WssServerCRTFile)
os.Remove(WssServerKEYFile)
err = DownloadFile(WssServerKEYFile, WssServerKEY)
assert.Nil(t, err)
defer os.Remove(WssServerKEYFile)
os.Remove(WssClientCRTFile)
err = DownloadFile(WssClientCRTFile, WssClientCRT)
assert.Nil(t, err)
defer os.Remove(WssClientCRTFile)
addr := "127.0.0.1:63450"
path := "/hello"
func() {
server = NewWSSServer(
WithLocalAddress(addr),
WithWebsocketServerPath(path),
WithWebsocketServerCert(WssServerCRTFile),
WithWebsocketServerPrivateKey(WssServerKEYFile),
)
newServerSession := func(session Session) error {
return newSessionCallback(session, &serverMsgHandler)
}
go server.RunEventLoop(newServerSession)
}()
time.Sleep(1e9)
client := NewWSSClient(
WithServerAddress("wss://"+addr+path),
WithConnectionNumber(1),
WithRootCertificateFile(WssClientCRTFile),
)
var msgHandler MessageHandler
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}
client.RunEventLoop(cb)
time.Sleep(1e9)
assert.Equal(t, 1, msgHandler.SessionNumber())
client.Close()
assert.True(t, client.IsClosed())
assert.False(t, server.IsClosed())
// time.Sleep(1000e9)
// server.Close()
// assert.True(t, server.IsClosed())
}