forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cancelation_test.go
749 lines (684 loc) · 22.4 KB
/
cancelation_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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package self_test
import (
"context"
"fmt"
"io"
"math/rand"
"net"
"sync"
"sync/atomic"
"time"
"github.com/quic-go/quic-go"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Stream Cancellations", func() {
const numStreams = 80
Context("canceling the read side", func() {
var server *quic.Listener
// The server accepts a single connection, and then opens numStreams unidirectional streams.
// On each of these streams, it (tries to) write PRData.
// When done, it sends the number of canceled streams on the channel.
runServer := func(data []byte) <-chan int32 {
numCanceledStreamsChan := make(chan int32)
var err error
server, err = quic.ListenAddr("localhost:0", getTLSConfig(), getQuicConfig(nil))
Expect(err).ToNot(HaveOccurred())
var canceledCounter atomic.Int32
go func() {
defer GinkgoRecover()
var wg sync.WaitGroup
wg.Add(numStreams)
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.OpenUniStreamSync(context.Background())
Expect(err).ToNot(HaveOccurred())
if _, err := str.Write(data); err != nil {
Expect(err).To(Equal(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: quic.StreamErrorCode(str.StreamID()),
Remote: true,
}))
canceledCounter.Add(1)
return
}
if err := str.Close(); err != nil {
Expect(err).To(MatchError(fmt.Sprintf("close called for canceled stream %d", str.StreamID())))
canceledCounter.Add(1)
return
}
}()
}
wg.Wait()
numCanceledStreamsChan <- canceledCounter.Load()
}()
return numCanceledStreamsChan
}
AfterEach(func() {
Expect(server.Close()).To(Succeed())
})
It("downloads when the client immediately cancels most streams", func() {
serverCanceledCounterChan := runServer(PRData)
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: numStreams / 2}),
)
Expect(err).ToNot(HaveOccurred())
var canceledCounter atomic.Int32
var wg sync.WaitGroup
wg.Add(numStreams)
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.AcceptUniStream(context.Background())
Expect(err).ToNot(HaveOccurred())
// cancel around 2/3 of the streams
if rand.Int31()%3 != 0 {
canceledCounter.Add(1)
resetErr := quic.StreamErrorCode(str.StreamID())
str.CancelRead(resetErr)
_, err := str.Read([]byte{0})
Expect(err).To(Equal(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: resetErr,
Remote: false,
}))
return
}
data, err := io.ReadAll(str)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(PRData))
}()
}
wg.Wait()
var serverCanceledCounter int32
Eventually(serverCanceledCounterChan).Should(Receive(&serverCanceledCounter))
Expect(conn.CloseWithError(0, "")).To(Succeed())
clientCanceledCounter := canceledCounter.Load()
// The server will only count a stream as being reset if learns about the cancelation before it finished writing all data.
Expect(clientCanceledCounter).To(BeNumerically(">=", serverCanceledCounter))
fmt.Fprintf(GinkgoWriter, "Canceled reading on %d of %d streams.\n", clientCanceledCounter, numStreams)
Expect(clientCanceledCounter).To(BeNumerically(">", numStreams/10))
Expect(numStreams - clientCanceledCounter).To(BeNumerically(">", numStreams/10))
})
It("downloads when the client cancels streams after reading from them for a bit", func() {
serverCanceledCounterChan := runServer(PRData)
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: numStreams / 2}),
)
Expect(err).ToNot(HaveOccurred())
var canceledCounter atomic.Int32
var wg sync.WaitGroup
wg.Add(numStreams)
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.AcceptUniStream(context.Background())
Expect(err).ToNot(HaveOccurred())
// only read some data from about 1/3 of the streams
if rand.Int31()%3 != 0 {
length := int(rand.Int31n(int32(len(PRData) - 1)))
data, err := io.ReadAll(io.LimitReader(str, int64(length)))
Expect(err).ToNot(HaveOccurred())
str.CancelRead(quic.StreamErrorCode(str.StreamID()))
Expect(data).To(Equal(PRData[:length]))
canceledCounter.Add(1)
return
}
data, err := io.ReadAll(str)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(PRData))
}()
}
wg.Wait()
var serverCanceledCounter int32
Eventually(serverCanceledCounterChan).Should(Receive(&serverCanceledCounter))
Expect(conn.CloseWithError(0, "")).To(Succeed())
clientCanceledCounter := canceledCounter.Load()
// The server will only count a stream as being reset if learns about the cancelation before it finished writing all data.
Expect(clientCanceledCounter).To(BeNumerically(">=", serverCanceledCounter))
fmt.Fprintf(GinkgoWriter, "Canceled reading on %d of %d streams.\n", clientCanceledCounter, numStreams)
Expect(clientCanceledCounter).To(BeNumerically(">", numStreams/10))
Expect(numStreams - clientCanceledCounter).To(BeNumerically(">", numStreams/10))
})
It("allows concurrent Read and CancelRead calls", func() {
// This test is especially valuable when run with race detector,
// see https://github.com/quic-go/quic-go/issues/3239.
serverCanceledCounterChan := runServer(make([]byte, 100)) // make sure the FIN is sent with the STREAM frame
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: numStreams / 2}),
)
Expect(err).ToNot(HaveOccurred())
var wg sync.WaitGroup
wg.Add(numStreams)
var counter atomic.Int32
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.AcceptUniStream(context.Background())
Expect(err).ToNot(HaveOccurred())
done := make(chan struct{})
go func() {
defer GinkgoRecover()
defer close(done)
b := make([]byte, 32)
if _, err := str.Read(b); err != nil {
counter.Add(1)
Expect(err).To(Equal(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: 1234,
Remote: false,
}))
return
}
}()
go str.CancelRead(1234)
Eventually(done).Should(BeClosed())
}()
}
wg.Wait()
Expect(conn.CloseWithError(0, "")).To(Succeed())
numCanceled := counter.Load()
fmt.Fprintf(GinkgoWriter, "canceled %d out of %d streams", numCanceled, numStreams)
Expect(numCanceled).ToNot(BeZero())
Eventually(serverCanceledCounterChan).Should(Receive())
})
})
Context("canceling the write side", func() {
runClient := func(server *quic.Listener) int32 /* number of canceled streams */ {
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: numStreams / 2}),
)
Expect(err).ToNot(HaveOccurred())
var wg sync.WaitGroup
var counter atomic.Int32
wg.Add(numStreams)
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.AcceptUniStream(context.Background())
Expect(err).ToNot(HaveOccurred())
data, err := io.ReadAll(str)
if err != nil {
counter.Add(1)
Expect(err).To(MatchError(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: quic.StreamErrorCode(str.StreamID()),
Remote: true,
}))
return
}
Expect(data).To(Equal(PRData))
}()
}
wg.Wait()
streamCount := counter.Load()
fmt.Fprintf(GinkgoWriter, "Canceled writing on %d of %d streams\n", streamCount, numStreams)
Expect(streamCount).To(BeNumerically(">", numStreams/10))
Expect(numStreams - streamCount).To(BeNumerically(">", numStreams/10))
Expect(conn.CloseWithError(0, "")).To(Succeed())
Expect(server.Close()).To(Succeed())
return streamCount
}
It("downloads when the server cancels some streams immediately", func() {
server, err := quic.ListenAddr("localhost:0", getTLSConfig(), nil)
Expect(err).ToNot(HaveOccurred())
var canceledCounter atomic.Int32
go func() {
defer GinkgoRecover()
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
str, err := conn.OpenUniStreamSync(context.Background())
Expect(err).ToNot(HaveOccurred())
// cancel about 2/3 of the streams
if rand.Int31()%3 != 0 {
str.CancelWrite(quic.StreamErrorCode(str.StreamID()))
canceledCounter.Add(1)
return
}
_, err = str.Write(PRData)
Expect(err).ToNot(HaveOccurred())
Expect(str.Close()).To(Succeed())
}()
}
}()
clientCanceledStreams := runClient(server)
Expect(clientCanceledStreams).To(Equal(canceledCounter.Load()))
})
It("downloads when the server cancels some streams after sending some data", func() {
server, err := quic.ListenAddr("localhost:0", getTLSConfig(), nil)
Expect(err).ToNot(HaveOccurred())
var canceledCounter atomic.Int32
go func() {
defer GinkgoRecover()
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
str, err := conn.OpenUniStreamSync(context.Background())
Expect(err).ToNot(HaveOccurred())
// only write some data from about 1/3 of the streams, then cancel
if rand.Int31()%3 != 0 {
length := int(rand.Int31n(int32(len(PRData) - 1)))
_, err = str.Write(PRData[:length])
Expect(err).ToNot(HaveOccurred())
str.CancelWrite(quic.StreamErrorCode(str.StreamID()))
canceledCounter.Add(1)
return
}
_, err = str.Write(PRData)
Expect(err).ToNot(HaveOccurred())
Expect(str.Close()).To(Succeed())
}()
}
}()
clientCanceledStreams := runClient(server)
Expect(clientCanceledStreams).To(Equal(canceledCounter.Load()))
})
})
Context("canceling both read and write side", func() {
It("downloads data when both sides cancel streams immediately", func() {
server, err := quic.ListenAddr("localhost:0", getTLSConfig(), nil)
Expect(err).ToNot(HaveOccurred())
done := make(chan struct{})
go func() {
defer GinkgoRecover()
var wg sync.WaitGroup
wg.Add(numStreams)
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.OpenUniStreamSync(context.Background())
Expect(err).ToNot(HaveOccurred())
// cancel about half of the streams
if rand.Int31()%2 == 0 {
str.CancelWrite(quic.StreamErrorCode(str.StreamID()))
return
}
if _, err = str.Write(PRData); err != nil {
Expect(err).To(MatchError(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: quic.StreamErrorCode(str.StreamID()),
Remote: true,
}))
return
}
if err := str.Close(); err != nil {
Expect(err).To(MatchError(fmt.Sprintf("close called for canceled stream %d", str.StreamID())))
return
}
}()
}
wg.Wait()
close(done)
}()
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: numStreams / 2}),
)
Expect(err).ToNot(HaveOccurred())
var wg sync.WaitGroup
var counter atomic.Int32
wg.Add(numStreams)
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.AcceptUniStream(context.Background())
Expect(err).ToNot(HaveOccurred())
// cancel around half of the streams
if rand.Int31()%2 == 0 {
str.CancelRead(quic.StreamErrorCode(str.StreamID()))
return
}
data, err := io.ReadAll(str)
if err != nil {
Expect(err).To(MatchError(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: quic.StreamErrorCode(str.StreamID()),
Remote: true,
}))
return
}
counter.Add(1)
Expect(data).To(Equal(PRData))
}()
}
wg.Wait()
count := counter.Load()
Expect(count).To(BeNumerically(">", numStreams/15))
fmt.Fprintf(GinkgoWriter, "Successfully read from %d of %d streams.\n", count, numStreams)
Expect(conn.CloseWithError(0, "")).To(Succeed())
Eventually(done).Should(BeClosed())
Expect(server.Close()).To(Succeed())
})
It("downloads data when both sides cancel streams after a while", func() {
server, err := quic.ListenAddr("localhost:0", getTLSConfig(), nil)
Expect(err).ToNot(HaveOccurred())
done := make(chan struct{})
go func() {
defer GinkgoRecover()
defer close(done)
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
var wg sync.WaitGroup
wg.Add(numStreams)
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.OpenUniStreamSync(context.Background())
Expect(err).ToNot(HaveOccurred())
// cancel about half of the streams
length := len(PRData)
if rand.Int31()%2 == 0 {
length = int(rand.Int31n(int32(len(PRData) - 1)))
}
if _, err = str.Write(PRData[:length]); err != nil {
Expect(err).To(MatchError(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: quic.StreamErrorCode(str.StreamID()),
Remote: true,
}))
return
}
if length < len(PRData) {
str.CancelWrite(quic.StreamErrorCode(str.StreamID()))
} else if err := str.Close(); err != nil {
Expect(err).To(MatchError(fmt.Sprintf("close called for canceled stream %d", str.StreamID())))
return
}
}()
}
wg.Wait()
}()
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: numStreams / 2}),
)
Expect(err).ToNot(HaveOccurred())
var wg sync.WaitGroup
var counter atomic.Int32
wg.Add(numStreams)
for i := 0; i < numStreams; i++ {
go func() {
defer GinkgoRecover()
defer wg.Done()
str, err := conn.AcceptUniStream(context.Background())
Expect(err).ToNot(HaveOccurred())
r := io.Reader(str)
length := len(PRData)
// cancel around half of the streams
if rand.Int31()%2 == 0 {
length = int(rand.Int31n(int32(len(PRData) - 1)))
r = io.LimitReader(str, int64(length))
}
data, err := io.ReadAll(r)
if err != nil {
Expect(err).To(MatchError(&quic.StreamError{
StreamID: str.StreamID(),
ErrorCode: quic.StreamErrorCode(str.StreamID()),
Remote: true,
}))
return
}
Expect(data).To(Equal(PRData[:length]))
if length < len(PRData) {
str.CancelRead(quic.StreamErrorCode(str.StreamID()))
return
}
counter.Add(1)
Expect(data).To(Equal(PRData))
}()
}
wg.Wait()
Eventually(done).Should(BeClosed())
count := counter.Load()
Expect(count).To(BeNumerically(">", numStreams/15))
fmt.Fprintf(GinkgoWriter, "Successfully read from %d of %d streams.\n", count, numStreams)
Expect(conn.CloseWithError(0, "")).To(Succeed())
Expect(server.Close()).To(Succeed())
})
})
Context("canceling the context", func() {
It("downloads data when the receiving peer cancels the context for accepting streams", func() {
server, err := quic.ListenAddr("localhost:0", getTLSConfig(), getQuicConfig(nil))
Expect(err).ToNot(HaveOccurred())
go func() {
defer GinkgoRecover()
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
ticker := time.NewTicker(5 * time.Millisecond)
for i := 0; i < numStreams; i++ {
<-ticker.C
go func() {
defer GinkgoRecover()
str, err := conn.OpenUniStreamSync(context.Background())
Expect(err).ToNot(HaveOccurred())
_, err = str.Write(PRData)
Expect(err).ToNot(HaveOccurred())
Expect(str.Close()).To(Succeed())
}()
}
}()
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: numStreams / 3}),
)
Expect(err).ToNot(HaveOccurred())
var numToAccept int
var counter atomic.Int32
var wg sync.WaitGroup
wg.Add(numStreams)
for numToAccept < numStreams {
ctx, cancel := context.WithCancel(context.Background())
// cancel accepting half of the streams
if rand.Int31()%2 == 0 {
cancel()
} else {
numToAccept++
defer cancel()
}
go func() {
defer GinkgoRecover()
str, err := conn.AcceptUniStream(ctx)
if err != nil {
if err.Error() == "context canceled" {
counter.Add(1)
}
return
}
data, err := io.ReadAll(str)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(PRData))
wg.Done()
}()
}
wg.Wait()
count := counter.Load()
fmt.Fprintf(GinkgoWriter, "Canceled AcceptStream %d times\n", count)
Expect(count).To(BeNumerically(">", numStreams/2))
Expect(conn.CloseWithError(0, "")).To(Succeed())
Expect(server.Close()).To(Succeed())
})
It("downloads data when the sending peer cancels the context for opening streams", func() {
const (
numStreams = 15
maxIncomingStreams = 5
)
server, err := quic.ListenAddr("localhost:0", getTLSConfig(), getQuicConfig(nil))
Expect(err).ToNot(HaveOccurred())
msg := make(chan struct{}, 1)
var numCanceled atomic.Int32
go func() {
defer GinkgoRecover()
defer close(msg)
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
var numOpened int
for numOpened < numStreams {
ctx, cancel := context.WithTimeout(context.Background(), scaleDuration(20*time.Millisecond))
defer cancel()
str, err := conn.OpenUniStreamSync(ctx)
if err != nil {
Expect(err).To(MatchError(context.DeadlineExceeded))
numCanceled.Add(1)
select {
case msg <- struct{}{}:
default:
}
continue
}
numOpened++
go func(str quic.SendStream) {
defer GinkgoRecover()
_, err = str.Write(PRData)
Expect(err).ToNot(HaveOccurred())
Expect(str.Close()).To(Succeed())
}(str)
}
}()
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{MaxIncomingUniStreams: maxIncomingStreams}),
)
Expect(err).ToNot(HaveOccurred())
var wg sync.WaitGroup
wg.Add(numStreams)
for i := 0; i < numStreams; i++ {
<-msg
str, err := conn.AcceptUniStream(context.Background())
Expect(err).ToNot(HaveOccurred())
go func(str quic.ReceiveStream) {
defer GinkgoRecover()
data, err := io.ReadAll(str)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(PRData))
wg.Done()
}(str)
}
wg.Wait()
count := numCanceled.Load()
fmt.Fprintf(GinkgoWriter, "Canceled OpenStreamSync %d times\n", count)
Expect(count).To(BeNumerically(">=", numStreams-maxIncomingStreams))
Expect(conn.CloseWithError(0, "")).To(Succeed())
Expect(server.Close()).To(Succeed())
})
})
It("doesn't run into any errors when streams are canceled all the time", func() {
const maxIncomingStreams = 1000
server, err := quic.ListenAddr(
"localhost:0",
getTLSConfig(),
getQuicConfig(&quic.Config{MaxIncomingStreams: maxIncomingStreams, MaxIdleTimeout: 10 * time.Second}),
)
Expect(err).ToNot(HaveOccurred())
defer server.Close()
var wg sync.WaitGroup
wg.Add(2 * 4 * maxIncomingStreams)
handleStream := func(str quic.Stream) {
str.SetDeadline(time.Now().Add(time.Second))
go func() {
defer wg.Done()
if rand.Int31()%2 == 0 {
defer GinkgoRecover()
io.ReadAll(str)
}
}()
go func() {
defer wg.Done()
if rand.Int31()%2 == 0 {
str.Write([]byte("foobar"))
if rand.Int31()%2 == 0 {
str.Close()
}
}
}()
go func() {
defer wg.Done()
// Make sure we at least send out *something* for the last stream,
// otherwise the peer might never receive this anything for this stream.
if rand.Int31()%2 == 0 || str.StreamID() == 4*(maxIncomingStreams-1) {
str.CancelWrite(1234)
}
}()
go func() {
defer wg.Done()
if rand.Int31()%2 == 0 {
str.CancelRead(1234)
}
}()
}
serverRunning := make(chan struct{})
go func() {
defer GinkgoRecover()
defer close(serverRunning)
conn, err := server.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
for {
str, err := conn.AcceptStream(context.Background())
if err != nil {
// Make sure the connection is closed regularly.
Expect(err).To(BeAssignableToTypeOf(&quic.ApplicationError{}))
return
}
handleStream(str)
}
}()
conn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", server.Addr().(*net.UDPAddr).Port),
getTLSClientConfig(),
getQuicConfig(&quic.Config{}),
)
Expect(err).ToNot(HaveOccurred())
for i := 0; i < maxIncomingStreams; i++ {
str, err := conn.OpenStreamSync(context.Background())
Expect(err).ToNot(HaveOccurred())
handleStream(str)
}
// We don't expect to accept any stream here.
// We're just making sure the connection stays open and there's no error.
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
_, err = conn.AcceptStream(ctx)
Expect(err).To(MatchError(context.DeadlineExceeded))
wg.Wait()
Expect(conn.CloseWithError(0, "")).To(Succeed())
Eventually(serverRunning).Should(BeClosed())
})
})