forked from cloudwego/netpoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_test.go
390 lines (349 loc) · 8.6 KB
/
connection_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
// Copyright 2021 CloudWeGo Authors
//
// Licensed 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 netpoll
import (
"context"
"errors"
"fmt"
"math/rand"
"runtime"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
)
func TestConnectionWrite(t *testing.T) {
var cycle, caps = 10000, 256
var msg, buf = make([]byte, caps), make([]byte, caps)
var wg sync.WaitGroup
wg.Add(1)
var count int32
var expect = int32(cycle * caps)
var opts = &options{}
opts.onRequest = func(ctx context.Context, connection Connection) error {
n, err := connection.Read(buf)
MustNil(t, err)
if atomic.AddInt32(&count, int32(n)) >= expect {
wg.Done()
}
return nil
}
r, w := GetSysFdPairs()
var rconn, wconn = &connection{}, &connection{}
rconn.init(&netFD{fd: r}, opts)
wconn.init(&netFD{fd: w}, opts)
for i := 0; i < cycle; i++ {
n, err := wconn.Write(msg)
MustNil(t, err)
Equal(t, n, len(msg))
}
wg.Wait()
Equal(t, atomic.LoadInt32(&count), expect)
rconn.Close()
}
func TestConnectionLargeWrite(t *testing.T) {
// ci machine don't have 4GB memory, so skip test
t.Skipf("skip large write test for ci job")
var totalSize = 1024 * 1024 * 1024 * 4
var wg sync.WaitGroup
wg.Add(1)
var opts = &options{}
opts.onRequest = func(ctx context.Context, connection Connection) error {
if connection.Reader().Len() < totalSize {
return nil
}
_, err := connection.Reader().Next(totalSize)
MustNil(t, err)
err = connection.Reader().Release()
MustNil(t, err)
wg.Done()
return nil
}
r, w := GetSysFdPairs()
var rconn, wconn = &connection{}, &connection{}
rconn.init(&netFD{fd: r}, opts)
wconn.init(&netFD{fd: w}, opts)
msg := make([]byte, totalSize/4)
for i := 0; i < 4; i++ {
_, err := wconn.Writer().WriteBinary(msg)
MustNil(t, err)
}
wg.Wait()
rconn.Close()
}
func TestConnectionRead(t *testing.T) {
r, w := GetSysFdPairs()
var rconn, wconn = &connection{}, &connection{}
rconn.init(&netFD{fd: r}, nil)
wconn.init(&netFD{fd: w}, nil)
var size = 256
var cycleTime = 100000
var msg = make([]byte, size)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < cycleTime; i++ {
buf, err := rconn.Reader().Next(size)
MustNil(t, err)
Equal(t, len(buf), size)
rconn.Reader().Release()
}
}()
for i := 0; i < cycleTime; i++ {
n, err := wconn.Write(msg)
MustNil(t, err)
Equal(t, n, len(msg))
}
wg.Wait()
rconn.Close()
}
func TestConnectionReadAfterClosed(t *testing.T) {
r, w := GetSysFdPairs()
var rconn = &connection{}
rconn.init(&netFD{fd: r}, nil)
var size = 256
var msg = make([]byte, size)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
var buf, err = rconn.Reader().Next(size)
MustNil(t, err)
Equal(t, len(buf), size)
}()
time.Sleep(time.Millisecond)
sysWrite(w, msg)
syscall.Close(w)
wg.Wait()
}
func TestConnectionWaitReadHalfPacket(t *testing.T) {
r, w := GetSysFdPairs()
var rconn = &connection{}
rconn.init(&netFD{fd: r}, nil)
var size = pagesize * 2
var msg = make([]byte, size)
// write half packet
sysWrite(w, msg[:size/2])
// wait poller reads buffer
for rconn.inputBuffer.Len() <= 0 {
runtime.Gosched()
}
// wait read full packet
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
var buf, err = rconn.Reader().Next(size)
Equal(t, atomic.LoadInt64(&rconn.waitReadSize), int64(0))
MustNil(t, err)
Equal(t, len(buf), size)
}()
// write left half packet
for atomic.LoadInt64(&rconn.waitReadSize) <= 0 {
runtime.Gosched()
}
Equal(t, atomic.LoadInt64(&rconn.waitReadSize), int64(size))
sysWrite(w, msg[size/2:])
wg.Wait()
}
func TestReadTimer(t *testing.T) {
read := time.NewTimer(time.Second)
MustTrue(t, read.Stop())
time.Sleep(time.Millisecond)
Equal(t, len(read.C), 0)
}
func TestReadTrigger(t *testing.T) {
trigger := make(chan int, 1)
select {
case trigger <- 0:
default:
}
Equal(t, len(trigger), 1)
}
func writeAll(fd fdtype, buf []byte) error {
for len(buf) > 0 {
n, err := sysWrite(fd, buf)
if n < 0 {
return err
}
buf = buf[n:]
}
return nil
}
// Large packet write test. The socket buffer is 2MB by default, here to verify
// whether Connection.Close can be executed normally after socket output buffer is full.
func TestLargeBufferWrite(t *testing.T) {
ln, err := CreateListener("tcp", ":1234")
MustNil(t, err)
trigger := make(chan fdtype)
defer close(trigger)
go func() {
for {
conn, err := ln.Accept()
if conn == nil && err == nil {
continue
}
trigger <- conn.(*netFD).fd
<-trigger
err = ln.Close()
MustNil(t, err)
return
}
}()
conn, err := DialConnection("tcp", ":1234", time.Second)
MustNil(t, err)
rfd := <-trigger
var wg sync.WaitGroup
wg.Add(1)
bufferSize := 2 * 1024 * 1024
//start large buffer writing
go func() {
defer wg.Done()
for i := 0; i < 129; i++ {
_, err := conn.Writer().Malloc(bufferSize)
MustNil(t, err)
err = conn.Writer().Flush()
if i < 128 {
MustNil(t, err)
}
}
}()
time.Sleep(time.Millisecond * 50)
buf := make([]byte, 1024)
for i := 0; i < 128*bufferSize; {
j, err := sysRead(rfd, buf)
if err == syscall.Errno(10035) && runtime.GOOS == "windows" {
continue
}
i += j
MustNil(t, err)
}
// close success
err = conn.Close()
MustNil(t, err)
wg.Wait()
}
// TestConnectionLargeMemory is used to verify the memory usage in the large package scenario.
func TestConnectionLargeMemory(t *testing.T) {
var start, end runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&start)
r, w := GetSysFdPairs()
var rconn = &connection{}
rconn.init(&netFD{fd: r}, nil)
var wg sync.WaitGroup
defer wg.Wait()
var rn, wn = 1024, 1 * 1024 * 1024
wg.Add(1)
go func() {
defer wg.Done()
rconn.Reader().Next(wn)
}()
var msg = make([]byte, rn)
for i := 0; i < wn/rn; i++ {
n, err := sysWrite(w, msg)
if err != nil {
panic(err)
}
if n != rn {
panic(fmt.Sprintf("n[%d]!=rn[%d]", n, rn))
}
time.Sleep(time.Millisecond)
}
runtime.ReadMemStats(&end)
alloc := end.TotalAlloc - start.TotalAlloc
var limit uint64
if runtime.GOOS == "windows" {
limit = uint64(100 * 1024 * 1024)
} else {
limit = uint64(4 * 1024 * 1024)
}
if alloc > limit {
panic(fmt.Sprintf("alloc[%d] out of memory %d", alloc, limit))
}
}
// TestSetTCPNoDelay is used to verify the connection initialization set the TCP_NODELAY correctly
func TestSetTCPNoDelay(t *testing.T) {
fd, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
MustNil(t, err)
conn := &connection{}
conn.init(&netFD{network: "tcp", fd: fd}, nil)
n, _ := sysGetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY)
MustTrue(t, n > 0)
err = setTCPNoDelay(fd, false)
MustNil(t, err)
n, _ = sysGetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY)
MustTrue(t, n == 0)
}
func TestConnectionUntil(t *testing.T) {
r, w := GetSysFdPairs()
rconn, wconn := &connection{}, &connection{}
rconn.init(&netFD{fd: r}, nil)
wconn.init(&netFD{fd: w}, nil)
loopSize := 100000
msg := make([]byte, 1002)
msg[500], msg[1001] = '\n', '\n'
go func() {
for i := 0; i < loopSize; i++ {
n, err := wconn.Write(msg)
MustNil(t, err)
MustTrue(t, n == len(msg))
}
wconn.Write(msg[:100])
wconn.Close()
}()
for i := 0; i < loopSize*2; i++ {
buf, err := rconn.Reader().Until('\n')
MustNil(t, err)
Equal(t, len(buf), 501)
rconn.Reader().Release()
}
buf, err := rconn.Reader().Until('\n')
Equal(t, len(buf), 100)
MustTrue(t, errors.Is(err, ErrEOF))
}
func TestBookSizeLargerThanMaxSize(t *testing.T) {
r, w := GetSysFdPairs()
var rconn, wconn = &connection{}, &connection{}
rconn.init(&netFD{fd: r}, nil)
wconn.init(&netFD{fd: w}, nil)
var length = 25
dataCollection := make([][]byte, length)
for i := 0; i < length; i++ {
dataCollection[i] = make([]byte, 2<<i)
for j := 0; j < 2<<i; j++ {
dataCollection[i][j] = byte(rand.Intn(256))
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < length; i++ {
buf, err := rconn.Reader().Next(2 << i)
MustNil(t, err)
Equal(t, string(buf), string(dataCollection[i]))
rconn.Reader().Release()
}
}()
for i := 0; i < length; i++ {
n, err := wconn.Write(dataCollection[i])
MustNil(t, err)
Equal(t, n, 2<<i)
}
wg.Wait()
rconn.Close()
}