-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams_map_incoming_test.go
310 lines (276 loc) · 9.74 KB
/
streams_map_incoming_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
package quic
import (
"context"
"errors"
"math/rand"
"time"
"github.com/imannamdari/quic-go/internal/protocol"
"github.com/imannamdari/quic-go/internal/wire"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type mockGenericStream struct {
num protocol.StreamNum
closed bool
closeErr error
sendWindow protocol.ByteCount
}
func (s *mockGenericStream) closeForShutdown(err error) {
s.closed = true
s.closeErr = err
}
func (s *mockGenericStream) updateSendWindow(limit protocol.ByteCount) {
s.sendWindow = limit
}
var _ = Describe("Streams Map (incoming)", func() {
var (
m *incomingStreamsMap[*mockGenericStream]
newItemCounter int
mockSender *MockStreamSender
maxNumStreams uint64
)
streamType := []protocol.StreamType{protocol.StreamTypeUni, protocol.StreamTypeUni}[rand.Intn(2)]
// check that the frame can be serialized and deserialized
checkFrameSerialization := func(f wire.Frame) {
b, err := f.Append(nil, protocol.Version1)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
_, frame, err := wire.NewFrameParser(false).ParseNext(b, protocol.Encryption1RTT, protocol.Version1)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
Expect(f).To(Equal(frame))
}
BeforeEach(func() { maxNumStreams = 5 })
JustBeforeEach(func() {
newItemCounter = 0
mockSender = NewMockStreamSender(mockCtrl)
m = newIncomingStreamsMap(
streamType,
func(num protocol.StreamNum) *mockGenericStream {
newItemCounter++
return &mockGenericStream{num: num}
},
maxNumStreams,
mockSender.queueControlFrame,
)
})
It("opens all streams up to the id on GetOrOpenStream", func() {
_, err := m.GetOrOpenStream(4)
Expect(err).ToNot(HaveOccurred())
Expect(newItemCounter).To(Equal(4))
})
It("starts opening streams at the right position", func() {
// like the test above, but with 2 calls to GetOrOpenStream
_, err := m.GetOrOpenStream(2)
Expect(err).ToNot(HaveOccurred())
Expect(newItemCounter).To(Equal(2))
_, err = m.GetOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
Expect(newItemCounter).To(Equal(5))
})
It("accepts streams in the right order", func() {
_, err := m.GetOrOpenStream(2) // open streams 1 and 2
Expect(err).ToNot(HaveOccurred())
str, err := m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(str.num).To(Equal(protocol.StreamNum(1)))
str, err = m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(str.num).To(Equal(protocol.StreamNum(2)))
})
It("allows opening the maximum stream ID", func() {
str, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Expect(str.num).To(Equal(protocol.StreamNum(1)))
})
It("errors when trying to get a stream ID higher than the maximum", func() {
_, err := m.GetOrOpenStream(6)
Expect(err).To(HaveOccurred())
Expect(err.(streamError).TestError()).To(MatchError("peer tried to open stream 6 (current limit: 5)"))
})
It("blocks AcceptStream until a new stream is available", func() {
strChan := make(chan *mockGenericStream)
go func() {
defer GinkgoRecover()
str, err := m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
strChan <- str
}()
Consistently(strChan).ShouldNot(Receive())
str, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Expect(str.num).To(Equal(protocol.StreamNum(1)))
var acceptedStr *mockGenericStream
Eventually(strChan).Should(Receive(&acceptedStr))
Expect(acceptedStr.num).To(Equal(protocol.StreamNum(1)))
})
It("unblocks AcceptStream when the context is canceled", func() {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
defer GinkgoRecover()
_, err := m.AcceptStream(ctx)
Expect(err).To(MatchError("context canceled"))
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
cancel()
Eventually(done).Should(BeClosed())
})
It("unblocks AcceptStream when it is closed", func() {
testErr := errors.New("test error")
done := make(chan struct{})
go func() {
defer GinkgoRecover()
_, err := m.AcceptStream(context.Background())
Expect(err).To(MatchError(testErr))
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
m.CloseWithError(testErr)
Eventually(done).Should(BeClosed())
})
It("errors AcceptStream immediately if it is closed", func() {
testErr := errors.New("test error")
m.CloseWithError(testErr)
_, err := m.AcceptStream(context.Background())
Expect(err).To(MatchError(testErr))
})
It("closes all streams when CloseWithError is called", func() {
str1, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
str2, err := m.GetOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
testErr := errors.New("test err")
m.CloseWithError(testErr)
Expect(str1.closed).To(BeTrue())
Expect(str1.closeErr).To(MatchError(testErr))
Expect(str2.closed).To(BeTrue())
Expect(str2.closeErr).To(MatchError(testErr))
})
It("deletes streams", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
_, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
str, err := m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(str.num).To(Equal(protocol.StreamNum(1)))
Expect(m.DeleteStream(1)).To(Succeed())
str, err = m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
It("waits until a stream is accepted before actually deleting it", func() {
_, err := m.GetOrOpenStream(2)
Expect(err).ToNot(HaveOccurred())
Expect(m.DeleteStream(2)).To(Succeed())
str, err := m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(str.num).To(Equal(protocol.StreamNum(1)))
// when accepting this stream, it will get deleted, and a MAX_STREAMS frame is queued
mockSender.EXPECT().queueControlFrame(gomock.Any())
str, err = m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(str.num).To(Equal(protocol.StreamNum(2)))
})
It("doesn't return a stream queued for deleting from GetOrOpenStream", func() {
str, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Expect(str).ToNot(BeNil())
Expect(m.DeleteStream(1)).To(Succeed())
str, err = m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
// when accepting this stream, it will get deleted, and a MAX_STREAMS frame is queued
mockSender.EXPECT().queueControlFrame(gomock.Any())
str, err = m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(str).ToNot(BeNil())
})
It("errors when deleting a non-existing stream", func() {
err := m.DeleteStream(1337)
Expect(err).To(HaveOccurred())
Expect(err.(streamError).TestError()).To(MatchError("tried to delete unknown incoming stream 1337"))
})
It("sends MAX_STREAMS frames when streams are deleted", func() {
// open a bunch of streams
_, err := m.GetOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
// accept all streams
for i := 0; i < 5; i++ {
_, err := m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
}
mockSender.EXPECT().queueControlFrame(gomock.Any()).Do(func(f wire.Frame) {
msf := f.(*wire.MaxStreamsFrame)
Expect(msf.Type).To(BeEquivalentTo(streamType))
Expect(msf.MaxStreamNum).To(Equal(protocol.StreamNum(maxNumStreams + 1)))
checkFrameSerialization(f)
})
Expect(m.DeleteStream(3)).To(Succeed())
mockSender.EXPECT().queueControlFrame(gomock.Any()).Do(func(f wire.Frame) {
Expect(f.(*wire.MaxStreamsFrame).MaxStreamNum).To(Equal(protocol.StreamNum(maxNumStreams + 2)))
checkFrameSerialization(f)
})
Expect(m.DeleteStream(4)).To(Succeed())
})
Context("using high stream limits", func() {
BeforeEach(func() { maxNumStreams = uint64(protocol.MaxStreamCount) - 2 })
It("doesn't send MAX_STREAMS frames if they would overflow 2^60 (the maximum stream count)", func() {
// open a bunch of streams
_, err := m.GetOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
// accept all streams
for i := 0; i < 5; i++ {
_, err := m.AcceptStream(context.Background())
Expect(err).ToNot(HaveOccurred())
}
mockSender.EXPECT().queueControlFrame(gomock.Any()).Do(func(f wire.Frame) {
Expect(f.(*wire.MaxStreamsFrame).MaxStreamNum).To(Equal(protocol.MaxStreamCount - 1))
checkFrameSerialization(f)
})
Expect(m.DeleteStream(4)).To(Succeed())
mockSender.EXPECT().queueControlFrame(gomock.Any()).Do(func(f wire.Frame) {
Expect(f.(*wire.MaxStreamsFrame).MaxStreamNum).To(Equal(protocol.MaxStreamCount))
checkFrameSerialization(f)
})
Expect(m.DeleteStream(3)).To(Succeed())
// at this point, we can't increase the stream limit any further, so no more MAX_STREAMS frames will be sent
Expect(m.DeleteStream(2)).To(Succeed())
Expect(m.DeleteStream(1)).To(Succeed())
})
})
Context("randomized tests", func() {
const num = 1000
BeforeEach(func() { maxNumStreams = num })
It("opens and accepts streams", func() {
rand.Seed(GinkgoRandomSeed())
ids := make([]protocol.StreamNum, num)
for i := 0; i < num; i++ {
ids[i] = protocol.StreamNum(i + 1)
}
rand.Shuffle(len(ids), func(i, j int) { ids[i], ids[j] = ids[j], ids[i] })
const timeout = 5 * time.Second
done := make(chan struct{}, 2)
go func() {
defer GinkgoRecover()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for i := 0; i < num; i++ {
_, err := m.AcceptStream(ctx)
Expect(err).ToNot(HaveOccurred())
}
done <- struct{}{}
}()
go func() {
defer GinkgoRecover()
for i := 0; i < num; i++ {
_, err := m.GetOrOpenStream(ids[i])
Expect(err).ToNot(HaveOccurred())
}
done <- struct{}{}
}()
Eventually(done, timeout*3/2).Should(Receive())
Eventually(done, timeout*3/2).Should(Receive())
})
})
})