forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tls_test.go
537 lines (512 loc) · 20.4 KB
/
tls_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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package gnet
import (
"bufio"
tls2 "crypto/tls"
"io"
"math/rand"
"net/http"
_ "net/http/pprof"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/panjf2000/gnet/v2/pkg/logging"
goPool "github.com/panjf2000/gnet/v2/pkg/pool/goroutine"
"github.com/panjf2000/gnet/v2/pkg/tls"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var serverCRT = `-----BEGIN CERTIFICATE-----
MIIDczCCAlugAwIBAgIJAM0/vF9KXTQbMA0GCSqGSIb3DQEBCwUAMFAxCzAJBgNV
BAYTAkNOMQwwCgYDVQQIDANGRUkxFTATBgNVBAcMDERlZmF1bHQgQ2l0eTEcMBoG
A1UECgwTRGVmYXVsdCBDb21wYW55IEx0ZDAeFw0yNDAzMjIwNzIxMTNaFw0yNTAz
MjIwNzIxMTNaMFAxCzAJBgNVBAYTAkNOMQwwCgYDVQQIDANGRUkxFTATBgNVBAcM
DERlZmF1bHQgQ2l0eTEcMBoGA1UECgwTRGVmYXVsdCBDb21wYW55IEx0ZDCCASIw
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAPXZ3mcbFEMmmDaQWyKOMOfPmxQG
lWGdQg9STQard+K/RY36WTYshTHNAQzZX1TqdCdReCSDgGkE5pcb7Pvw1UBa6c2b
BfpbmPwAG7oEQvyqIRgxW65w9ZUHl0A2+Ynvfz6mRJdZ1VRXbUKBZAjdHTwGbOZb
Tdw6niih42i8TUPrgCEq1F/NomiVsZqJTfXXwxsRCo7oZ1Gepb/ljOJ8Q3Iu78Ng
AQHCj+a2oxuRPMydHAhBbdPgQWSvO/C4864cXt6gtetsj4wR99syFX0k6vYeMdmZ
G9OE7jCmvU+F/hIDa60PXYXG4C+rKe01paUMIhrMyhV6Go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EwTRUEZmklb1s82FC8jhly7B9bmMYEDs8QfNdX
rngcyY2rD5ATuODBRks1LO2G7JDx4vJt1Wh04RInEg0=
-----END CERTIFICATE-----
`
var clientKey = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEApUqAiV2secYRjPnNbse5xJZsG4yo9370vKKTI/ZzadUOV7mr
jY4GUr5DAGAUGLKwZ+URvuxKcMz8EDEqMM7/EZL4g2M/YtP0jmGS8l/2OH+w0y9t
a8il6MYq8Qfix72Ur7O4rp4agdwCF3iJbulrLxjF4YRzj9HYkp/VKteffJvDlgeM
fZOe9y691KMjgDkDz62t8GcbFUZ/u1uiucZ5j4M2JtKd2nOYxfhPWYVJVW7jMShe
sZfl96RsrudwanF3Vg1nUSPkycjEcwdJF3TkMed+sFuov9WlL3K4CKNsxKFQR978
pTl36dCu5d/o3PX0EahG+sYma7veY2Q6TwrmEwIDAQABAoIBABjl8BbBFYoCXAJw
XpGhZEpInmBH/+vII/WGR8PTC++VOGPehAj+q9Uq88zKrDxMLmZ4CwRHc68qMY3+
2ewgQivTh7vC53lrlBsed5QrAFviYDBvpHvn7ZXyw+Uq2u5bkFonKFqlVjjZezct
4QXkA1FE2gnuebngUk9D5Ehf0pZiYki3t5+UbZ/2XIlU6qcwYXwTD8N3NDmhKDcn
2q/JatBp/l7gE+RJPYyBPgM9ffw273GraQhMwmeBkP0wfmxN/rntrO1ViuHaxwk9
qS6lBNghBcM8uIpihrm7hf080VqDUte4OG8Z6gP3kLeLg7vBubFUN/weGqRGKcDN
Ifoa9oECgYEA0Sfu/54O2fNSI3UiHrzExmeLpCDTYJAWMNViporSML6V0DnM0Hso
D+flogxns8t4QW1pJ5AlANmm13piavl44eKO5UyE5bIzCzbOi3v75aTgtIOd4M77
7HId0TR62xAeK23j7rBNoIpwNu9vYIYhh8kD3RJ+8DC1akhy2+398X8CgYEAyk+M
TTPZEajkk74Q46vu1ByAVL2OIqQq3dO292vW9z0YKl0WvehKPgjXEmxZtuUdvwAK
3dQgDNaF3kMAx0mluwCoPHFDgMR0tw51q3Jxb3sIgQo9RKUMuhTRzAwZQQW2qVYq
w+moY6rZgEiwjSEqKxBEviAiHAeOLjg5E4HobW0CgYEAlWMZYDerUd5px2uNIDkh
rrgcU/EiKLEhOGXgnSU/9ZLafCwcbrfYKGwwZqrHkvJ8BPc5iQl6dH4AnZ0LGG7w
+R2RhJM/xLOfo53UrmmyxBYcbLIpHH2aEAp944PBUB/V/JzzlPMVrtnSRlqDyYRk
BJD9knP4eGPB35S5fP4iR88CgYAcFp1JHri1jfhmVs4rffSDuI1MtqCkSJy2/az6
fxtyaiYPw6rjpPuhz0JlCWwPS5IssYnQsKHehm5BpJXYFW6QHxA8jBEuMJhvze8i
ZZpAOxm8KLrG7A9vSnKyXL+o2m0454C6gJdjdxYJjI4OmhsrrW+N4hI6SAZCfoXW
IoQtpQKBgF/JXD9cy4CJjJC2RQ4SSEz+yljYdQECeBtQjiodDq7x/l2ELr5gIO1A
VHDSuE+GXiRlWz1OmRpVh6DiXR6wIYdHJ8xw4rBPGtxEeBUoMWL+xpa6OJ1OX/2s
nXNUa+27KshlpdWNlwJOtkeeP7yMQGf7irAelQ/P0a3qmoop8YvC
-----END RSA PRIVATE KEY-----
`
func getServerConfig() *tls.Config {
cert, err := tls.X509KeyPair([]byte(serverCRT), []byte(serverKey))
if nil != err {
panic(err)
}
return &tls.Config{Certificates: []tls.Certificate{cert}}
}
func getClientTLSConfig() *tls.Config {
crt, err := tls.X509KeyPair([]byte(clientCRT), []byte(clientKey))
if err != nil {
panic(err)
}
return &tls.Config{Certificates: []tls.Certificate{crt}, InsecureSkipVerify: true}
}
func getGoClientTLSConfig() *tls2.Config {
crt, err := tls2.X509KeyPair([]byte(clientCRT), []byte(clientKey))
if err != nil {
panic(err)
}
return &tls2.Config{Certificates: []tls2.Certificate{crt}, InsecureSkipVerify: true}
}
type testTLSServer struct {
*testServer
}
func (s *testTLSServer) OnClose(c Conn, err error) (action Action) {
if err != nil {
logging.Debugf("error occurred on closed, %v\n", err)
}
/*if c.LocalAddr().Network() != "udp" {
require.IsType(s.tester, (*tlsConn)(nil), c.Context(), "want *tlsConn")
}*/
atomic.AddInt32(&s.disconnected, 1)
return
}
func (s *testTLSServer) OnTick() (delay time.Duration, action Action) {
delay = 100 * time.Millisecond
if atomic.CompareAndSwapInt32(&s.started, 0, 1) {
for _, protoAddr := range s.addrs {
proto, addr, err := parseProtoAddr(protoAddr)
assert.NoError(s.tester, err)
for i := 0; i < s.nclients; i++ {
atomic.AddInt32(&s.clientActive, 1)
go func() {
startTLSClient(s.tester, proto, addr, s.multicore, s.async)
atomic.AddInt32(&s.clientActive, -1)
}()
}
}
}
if atomic.LoadInt32(&s.clientActive) == 0 {
var streamAddrs int
for _, addr := range s.addrs {
if !strings.HasPrefix(addr, "udp") {
streamAddrs++
}
}
streamConns := s.nclients * streamAddrs
disconnected := atomic.LoadInt32(&s.disconnected)
if int(disconnected) == streamConns && disconnected == atomic.LoadInt32(&s.connected) {
action = Shutdown
s.workerPool.Release()
require.EqualValues(s.tester, 0, s.eng.CountConnections())
}
}
return
}
func TestTLSServer(t *testing.T) {
go func() {
err := http.ListenAndServe(":7070", nil)
if nil != err {
t.Logf("listenAndServe http error: %v\n", err)
}
}()
t.Run("poll-LT", func(t *testing.T) {
t.Run("tcp", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, false, false, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, false, false, true, false, false, 10, LeastConnections)
})
})
t.Run("tcp-async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, false, false, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, false, false, true, true, false, 10, LeastConnections)
})
})
t.Run("tcp-async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, false, false, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, false, false, true, true, true, 10, LeastConnections)
})
})
})
t.Run("poll-ET", func(t *testing.T) {
t.Run("tcp", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, true, false, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, true, false, true, false, false, 10, LeastConnections)
})
})
t.Run("tcp-async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, true, false, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, true, false, true, true, false, 10, LeastConnections)
})
})
t.Run("tcp-async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, true, false, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, true, false, true, true, true, 10, LeastConnections)
})
})
})
t.Run("poll-reuseport-LT", func(t *testing.T) {
t.Run("tcp", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, false, true, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, false, true, true, false, false, 10, LeastConnections)
})
})
t.Run("tcp-async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, false, true, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, false, true, true, true, false, 10, LeastConnections)
})
})
t.Run("tcp-async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, false, true, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, false, true, true, true, true, 10, LeastConnections)
})
})
})
t.Run("poll-reuseport-ET", func(t *testing.T) {
t.Run("tcp", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, true, true, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, true, true, true, false, false, 10, LeastConnections)
})
})
t.Run("tcp-async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, true, true, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, true, true, true, true, false, 10, LeastConnections)
})
})
t.Run("tcp-async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991"}, true, true, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9992"}, true, true, true, true, true, 10, LeastConnections)
})
})
})
t.Run("poll-multi-addrs-LT", func(t *testing.T) {
t.Run("sync", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, false, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, false, true, false, false, 10, LeastConnections)
})
})
t.Run("sync-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, false, false, false, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, false, true, false, true, 10, LeastConnections)
})
})
t.Run("async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, false, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, false, true, true, false, 10, LeastConnections)
})
})
t.Run("async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, false, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, false, true, true, true, 10, LeastConnections)
})
})
})
t.Run("poll-multi-addrs-reuseport-LT", func(t *testing.T) {
t.Run("sync", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, true, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, true, true, false, false, 10, LeastConnections)
})
})
t.Run("sync-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, true, false, false, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, true, true, false, true, 10, LeastConnections)
})
})
t.Run("async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, true, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, true, true, true, false, 10, LeastConnections)
})
})
t.Run("async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, false, true, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, false, true, true, true, true, 10, LeastConnections)
})
})
})
t.Run("poll-multi-addrs-ET", func(t *testing.T) {
t.Run("sync", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, false, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, false, true, false, false, 10, LeastConnections)
})
})
t.Run("sync-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, false, false, false, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, false, true, false, true, 10, LeastConnections)
})
})
t.Run("async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, false, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, false, true, true, false, 10, LeastConnections)
})
})
t.Run("async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, false, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, false, true, true, true, 10, LeastConnections)
})
})
})
t.Run("poll-multi-addrs-reuseport-ET", func(t *testing.T) {
t.Run("sync", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, true, false, false, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, true, true, false, false, 10, LeastConnections)
})
})
t.Run("sync-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, true, false, false, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, true, true, false, true, 10, LeastConnections)
})
})
t.Run("async", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, true, false, true, false, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, true, true, true, false, 10, LeastConnections)
})
})
t.Run("async-writev", func(t *testing.T) {
t.Run("1-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9991", "tcp://:9992"}, true, true, false, true, true, 10, RoundRobin)
})
t.Run("N-loop", func(t *testing.T) {
runTLSServer(t, []string{"tcp://:9995", "tcp://:9996"}, true, true, true, true, true, 10, LeastConnections)
})
})
})
}
func runTLSServer(t *testing.T, addrs []string, et, reuseport, multicore, async, writev bool, nclients int, lb LoadBalancing) {
internal := &testServer{
tester: t,
addrs: addrs,
multicore: multicore,
async: async,
writev: writev,
nclients: nclients,
workerPool: goPool.Default(),
}
ts := &testTLSServer{
testServer: internal,
}
var err error
if len(addrs) > 1 {
err = Rotate(ts,
addrs,
WithEdgeTriggeredIO(et),
WithLockOSThread(async),
WithMulticore(multicore),
WithReusePort(reuseport),
WithTicker(true),
WithTCPKeepAlive(time.Minute),
WithTCPNoDelay(TCPDelay),
WithLoadBalancing(lb),
WithTLSConfig(getServerConfig()))
} else {
err = Run(ts,
addrs[0],
WithEdgeTriggeredIO(et),
WithLockOSThread(async),
WithMulticore(multicore),
WithReusePort(reuseport),
WithTicker(true),
WithTCPKeepAlive(time.Minute),
WithTCPNoDelay(TCPDelay),
WithLoadBalancing(lb),
WithTLSConfig(getServerConfig()))
}
assert.NoError(t, err)
}
func startTLSClient(t *testing.T, network, addr string, multicore, async bool) {
rand.Seed(time.Now().UnixNano())
c, err := tls2.Dial(network, addr, getGoClientTLSConfig())
require.NoError(t, err)
defer c.Close()
rd := bufio.NewReader(c)
if network != "udp" {
msg, err := rd.ReadBytes('\n')
require.NoError(t, err)
require.Equal(t, string(msg), "sweetness\r\n", "bad header")
}
duration := time.Duration((rand.Float64()*2+1)*float64(time.Second)) / 2
logging.Debugf("test duration: %v", duration)
start := time.Now()
for time.Since(start) < duration {
reqData := make([]byte, streamLen)
if network == "udp" {
reqData = reqData[:datagramLen]
}
_, err = rand.Read(reqData)
require.NoError(t, err)
_, err = c.Write(reqData)
require.NoError(t, err)
respData := make([]byte, len(reqData))
_, err = io.ReadFull(rd, respData)
require.NoError(t, err)
if !async {
// require.Equalf(t, reqData, respData, "response mismatch with protocol:%s, multi-core:%t, content of bytes: %d vs %d", network, multicore, string(reqData), string(respData))
require.Equalf(
t,
reqData,
respData,
"response mismatch with protocol:%s, multi-core:%t, length of bytes: %d vs %d",
network,
multicore,
len(reqData),
len(respData),
)
}
}
}