-
Notifications
You must be signed in to change notification settings - Fork 33
/
stream_test.go
521 lines (457 loc) · 11.7 KB
/
stream_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
/*
* Copyright 2023 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 shmipc
import (
"io"
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func newClientServerWithNoCheck(conf *Config) (client *Session, server *Session) {
conn1, conn2 := testConn()
serverOk := make(chan struct{})
go func() {
var err error
server, err = newSession(conf, conn2, false)
if err != nil {
panic(err)
}
close(serverOk)
}()
cconf := testConf()
*cconf = *conf
var err error
client, err = newSession(cconf, conn1, true)
if err != nil {
panic(err)
}
<-serverOk
return client, server
}
// Close 94.4%
func TestStream_Close(t *testing.T) {
c := testConf()
c.QueueCap = 8
client, server := newClientServerWithNoCheck(c)
defer server.Close()
defer client.Close()
hadForceCloseNotifyCh := make(chan struct{})
doneCh := make(chan struct{})
go func() {
s, err := server.AcceptStream()
if err != nil {
t.Fatalf("open stream failed:,err:%s", err.Error())
}
_, err = s.BufferReader().ReadByte()
assert.Equal(t, nil, err)
<-hadForceCloseNotifyCh
_, err = s.BufferReader().ReadByte()
assert.Equal(t, ErrEndOfStream, err)
assert.Equal(t, 1, server.GetActiveStreamCount())
assert.Equal(t, uint32(streamHalfClosed), s.state)
s.Close()
assert.Equal(t, 0, server.GetActiveStreamCount())
assert.Equal(t, uint32(streamClosed), s.state)
close(doneCh)
}()
s, err := client.OpenStream()
if err != nil {
t.Fatalf("open stream failed:,err:%s", err.Error())
}
buf := s.BufferWriter()
_ = buf.WriteByte(1)
err = s.Flush(false)
if err != nil {
t.Fatalf("stream write buf failed:,err:%s", err.Error())
}
s.Close()
_ = buf.WriteString("1")
// try to write after stream closed
err = s.Flush(false)
assert.Equal(t, ErrStreamClosed, err)
time.Sleep(time.Millisecond * 50)
close(hadForceCloseNotifyCh)
<-doneCh
}
func TestStream_ClientFallback(t *testing.T) {
conf := testConf()
conf.ShareMemoryBufferCap = 1 << 20
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
done := make(chan struct{})
mockData := make([][]byte, 2000)
dataSize := 1024
for i := range mockData {
mockData[i] = make([]byte, dataSize)
rand.Read(mockData[i])
}
go func() {
s, err := server.AcceptStream()
if err != nil {
t.Fatalf("server.AcceptStream failed:%s", err.Error())
}
defer s.Close()
reader := s.BufferReader()
for i := range mockData {
get, err := reader.ReadBytes(dataSize)
if err != nil {
panic("ReadBytes failed:" + err.Error())
}
assert.Equal(t, mockData[i], get, "i:%d", i)
}
close(done)
}()
s, err := client.OpenStream()
if err != nil {
t.Fatalf("client.OpenStream failed:%s", err.Error())
}
defer s.Close()
writer := s.sendBuf
for i := range mockData {
if _, err = writer.WriteBytes(mockData[i]); err != nil {
t.Fatalf("Malloc(dataSize) failed:%s", err.Error())
}
}
assert.Equal(t, false, writer.isFromShareMemory())
err = s.Flush(true)
if err != nil {
t.Fatalf("writeBuf failed:%s", err.Error())
}
<-done
}
func TestStream_ServerFallback(t *testing.T) {
conf := testConf()
conf.ShareMemoryBufferCap = 1 << 20
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
done := make(chan struct{})
mockData := make([][]byte, 2000)
dataSize := 1024
for i := range mockData {
mockData[i] = make([]byte, dataSize)
rand.Read(mockData[i])
}
go func() {
s, err := server.AcceptStream()
if err != nil {
t.Fatalf("server.AcceptStream failed:%s", err.Error())
}
defer s.Close()
writer := s.sendBuf
for i := range mockData {
if _, err = writer.WriteBytes(mockData[i]); err != nil {
t.Fatalf("Malloc(dataSize) failed:%s", err.Error())
}
}
assert.Equal(t, false, writer.isFromShareMemory())
err = s.Flush(true)
if err != nil {
t.Fatalf("writeBuf failed:%s", err.Error())
}
close(done)
}()
s, err := client.OpenStream()
writer := s.sendBuf
// build conn
if _, err = writer.WriteBytes(mockData[0]); err != nil {
t.Fatalf("Malloc(dataSize) failed:%s", err.Error())
}
err = s.Flush(true)
if err != nil {
t.Fatalf("writeBuf failed:%s", err.Error())
}
reader := s.BufferReader()
for i := range mockData {
get, err := reader.ReadBytes(dataSize)
if err != nil {
panic("ReadBytes failed:" + err.Error())
}
assert.Equal(t, mockData[i], get, "i:%d", i)
}
if err != nil {
t.Fatalf("client.OpenStream failed:%s", err.Error())
}
defer s.Close()
<-done
}
// TODO
func TestStream_RandomPackageSize(t *testing.T) {
}
// TODO:halfClose 75.0%
func TestStream_HalfClose(t *testing.T) {
conf := testConf()
conf.ShareMemoryBufferCap = 1 << 20
client, server := testClientServerConfig(conf)
defer server.Close()
defer client.Close()
sbuf := make([]byte, 4096)
wg := &sync.WaitGroup{}
acceptor := func(i int) {
defer wg.Done()
stream, err := server.AcceptStream()
if err != nil {
t.Fatalf("err: %v", err)
}
defer stream.Close()
buf := stream.BufferReader()
_, err = buf.Discard(len(sbuf))
if err == ErrStreamClosed {
return
}
if err == io.EOF || err == ErrTimeout {
t.Logf("ret err:%s", err)
return
}
if err != nil {
t.Fatalf("err: %v", err)
}
if buf.Len() != 0 {
t.Fatalf("err!0: %d", buf.Len())
}
// after read
buf.ReleasePreviousRead()
println("smalloc", i)
wbuf := stream.BufferWriter()
_, _ = wbuf.WriteBytes(sbuf)
err = stream.Flush(true)
if err != nil {
t.Fatalf("err: %v", err)
}
println("rdone:", stream.StreamID())
}
sender := func(i int) {
defer wg.Done()
var err error
var buf *linkedBuffer
stream, err := client.OpenStream()
if err != nil {
t.Fatalf("err: %v", err)
}
defer stream.Close()
println("cmalloc", i)
buf = stream.sendBuf
if err != nil {
t.Fatalf("err: %v", err)
return
}
_, _ = buf.WriteBytes(sbuf)
err = stream.Flush(true)
if err != nil {
t.Fatalf("err: %v", err)
}
_, err = stream.BufferReader().ReadByte()
if err != nil {
t.Fatalf("err: %v", err)
}
println("sdone:", stream.StreamID())
}
// 2 is ok, more maybe dead lock, wait shm
for i := 0; i < 2; i++ {
wg.Add(2)
go acceptor(i)
go sender(i)
}
wg.Wait()
}
func TestStream_SendQueueFull(t *testing.T) {
conf := testConf()
conf.QueueCap = 8 // can only contain 1 queue elem(12B)
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
done := make(chan struct{})
dataSize := 10
mockDataLength := 500
mockData := make([][]byte, mockDataLength)
for i := range mockData {
mockData[i] = make([]byte, dataSize)
mockData[i][0] = byte(i)
}
s, err := client.OpenStream()
if err != nil {
t.Fatalf("client.OpenStream failed:%s", err.Error())
}
defer s.Close()
// write in a short time to full the send queue
for i := 0; i < mockDataLength; i++ {
writer := s.BufferWriter()
if _, err = writer.WriteBytes(mockData[i]); err != nil {
t.Fatalf("Malloc(dataSize) failed:%s", err.Error())
}
err = s.Flush(true)
if err != nil {
t.Fatalf("writeBuf failed:%s", err.Error())
}
}
go func() {
s, err := server.AcceptStream()
if err != nil {
t.Fatalf("server.AcceptStream failed:%s", err.Error())
}
defer s.Close()
for i := 0; i < mockDataLength; i++ {
reader := s.BufferReader()
get, err := reader.ReadBytes(dataSize)
if err != nil {
panic("ReadBytes failed:" + err.Error())
}
assert.Equal(t, mockData[i], get, "i:%d", i)
}
close(done)
}()
<-done
}
func TestStream_SendQueueFullTimeout(t *testing.T) {
conf := testConf()
conf.QueueCap = 8 // can only contain 1 queue elem(12B)
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
dataSize := 10
mockDataLength := 500
mockData := make([][]byte, mockDataLength)
for i := range mockData {
mockData[i] = make([]byte, dataSize)
rand.Read(mockData[i])
}
s, err := client.OpenStream()
// if send queue is full, it will trigger timeout immediately
_ = s.SetWriteDeadline(time.Now())
if err != nil {
t.Fatalf("client.OpenStream failed:%s", err.Error())
}
defer s.Close()
// write in a short time to full the send queue
for i := 0; i < mockDataLength; i++ {
writer := s.BufferWriter()
if _, err = writer.WriteBytes(mockData[i]); err != nil {
t.Fatalf("Malloc(dataSize) failed:%s", err.Error())
}
err = s.Flush(true)
if err != nil {
// must be timeout err here
assert.Error(t, ErrTimeout, err)
// reset deadline
s.SetDeadline(time.Now().Add(time.Microsecond * 10))
}
}
}
// reset 92.9%
func TestStream_Reset(t *testing.T) {
conf := testConf()
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
done := make(chan struct{})
notifySend := make(chan struct{})
notifyRead := make(chan struct{})
notifyClosed := make(chan struct{})
// use linked buffer by the way
dataSize := 64<<10 + 1
mockDataLength := 4
mockData := make([][]byte, mockDataLength)
for i := range mockData {
mockData[i] = make([]byte, dataSize)
rand.Read(mockData[i])
}
go func() {
s, err := server.AcceptStream()
if err != nil {
t.Fatalf("server.AcceptStream failed:%s", err.Error())
}
defer s.Close()
reader := s.BufferReader()
for i := 0; i < mockDataLength/2; i++ {
if err != nil {
t.Fatalf("ReadBuf failed:%s", err.Error())
}
get, err := reader.ReadBytes(dataSize)
if err != nil {
panic("ReadBytes failed:" + err.Error())
}
assert.Equal(t, mockData[i], get, "i:%d", i)
}
// read done, reset will be successful
err = s.reset()
assert.Equal(t, nil, err)
close(notifySend)
<-notifyRead
// test reuse
reader = s.BufferReader()
for i := mockDataLength / 2; i < mockDataLength; i++ {
if err != nil {
t.Fatalf("ReadBuf failed:%s", err.Error())
}
get, err := reader.ReadBytes(dataSize)
if err != nil {
panic("ReadBytes failed:" + err.Error())
}
assert.Equal(t, mockData[i], get, "i:%d", i)
}
<-notifyClosed
err = s.reset()
assert.Error(t, ErrStreamClosed, err)
close(done)
}()
s, err := client.OpenStream()
if err != nil {
t.Fatalf("client.OpenStream failed:%s", err.Error())
}
for i := 0; i < mockDataLength; i++ {
if i == mockDataLength/2 {
<-notifySend
}
if _, err := s.BufferWriter().WriteBytes(mockData[i]); err != nil {
t.Fatalf("Malloc(dataSize) failed:%s", err.Error())
}
err = s.Flush(false)
if err != nil {
t.Fatalf("writeBuf failed:%s", err.Error())
}
}
close(notifyRead)
s.Close()
close(notifyClosed)
<-done
}
func TestStream_fillDataToReadBuffer(t *testing.T) {
conf := testConf()
client, server := testClientServerConfig(conf)
defer client.Close()
defer server.Close()
stream, _ := client.OpenStream()
size := 8192
slice := newBufferSlice(nil, make([]byte, size), 0, false)
err := stream.fillDataToReadBuffer(bufferSliceWrapper{fallbackSlice: slice})
assert.Equal(t, 1, len(stream.pendingData.unread))
stream.Close()
err = stream.fillDataToReadBuffer(bufferSliceWrapper{fallbackSlice: slice})
assert.Equal(t, 0, len(stream.pendingData.unread))
assert.Equal(t, nil, err)
//TODO: fillDataToReadBuffer 53.3%
}
// TODO: SwapBufferForReuse 66.7%
func TestStream_SwapBufferForReuse(t *testing.T) {
}
/*
TODO
moveToWithoutLock 88.2%
readMore 56.7%
*/