forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streams_map_outgoing_generic_test.go
201 lines (176 loc) · 6.17 KB
/
streams_map_outgoing_generic_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
package quic
import (
"errors"
"github.com/golang/mock/gomock"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/qerr"
"github.com/lucas-clemente/quic-go/internal/wire"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Streams Map (outgoing)", func() {
const firstNewStream protocol.StreamID = 3
var (
m *outgoingItemsMap
newItem func(id protocol.StreamID) item
mockSender *MockStreamSender
)
BeforeEach(func() {
newItem = func(id protocol.StreamID) item {
return &mockGenericStream{id: id}
}
mockSender = NewMockStreamSender(mockCtrl)
m = newOutgoingItemsMap(firstNewStream, newItem, mockSender.queueControlFrame)
})
Context("no stream ID limit", func() {
BeforeEach(func() {
m.SetMaxStream(0xffffffff)
})
It("opens streams", func() {
str, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.(*mockGenericStream).id).To(Equal(firstNewStream))
str, err = m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.(*mockGenericStream).id).To(Equal(firstNewStream + 4))
})
It("doesn't open streams after it has been closed", func() {
testErr := errors.New("close")
m.CloseWithError(testErr)
_, err := m.OpenStream()
Expect(err).To(MatchError(testErr))
})
It("gets streams", func() {
_, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
str, err := m.GetStream(firstNewStream)
Expect(err).ToNot(HaveOccurred())
Expect(str.(*mockGenericStream).id).To(Equal(firstNewStream))
})
It("errors when trying to get a stream that has not yet been opened", func() {
_, err := m.GetStream(firstNewStream)
Expect(err).To(MatchError(qerr.Error(qerr.StreamStateError, "peer attempted to open stream 3")))
})
It("deletes streams", func() {
_, err := m.OpenStream() // opens firstNewStream
Expect(err).ToNot(HaveOccurred())
err = m.DeleteStream(firstNewStream)
Expect(err).ToNot(HaveOccurred())
str, err := m.GetStream(firstNewStream)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
It("errors when deleting a non-existing stream", func() {
err := m.DeleteStream(1337)
Expect(err).To(MatchError("Tried to delete unknown stream 1337"))
})
It("errors when deleting a stream twice", func() {
_, err := m.OpenStream() // opens firstNewStream
Expect(err).ToNot(HaveOccurred())
err = m.DeleteStream(firstNewStream)
Expect(err).ToNot(HaveOccurred())
err = m.DeleteStream(firstNewStream)
Expect(err).To(MatchError("Tried to delete unknown stream 3"))
})
It("closes all streams when CloseWithError is called", func() {
str1, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
str2, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
testErr := errors.New("test err")
m.CloseWithError(testErr)
Expect(str1.(*mockGenericStream).closed).To(BeTrue())
Expect(str1.(*mockGenericStream).closeErr).To(MatchError(testErr))
Expect(str2.(*mockGenericStream).closed).To(BeTrue())
Expect(str2.(*mockGenericStream).closeErr).To(MatchError(testErr))
})
})
Context("with stream ID limits", func() {
It("errors when no stream can be opened immediately", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
_, err := m.OpenStream()
expectTooManyStreamsError(err)
})
It("blocks until a stream can be opened synchronously", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
done := make(chan struct{})
go func() {
defer GinkgoRecover()
str, err := m.OpenStreamSync()
Expect(err).ToNot(HaveOccurred())
Expect(str.(*mockGenericStream).id).To(Equal(firstNewStream))
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
m.SetMaxStream(firstNewStream)
Eventually(done).Should(BeClosed())
})
It("works with stream 0", func() {
m = newOutgoingItemsMap(0, newItem, mockSender.queueControlFrame)
mockSender.EXPECT().queueControlFrame(gomock.Any()).Do(func(f wire.Frame) {
Expect(f.(*wire.StreamsBlockedFrame).StreamLimit).To(BeZero())
})
done := make(chan struct{})
go func() {
defer GinkgoRecover()
str, err := m.OpenStreamSync()
Expect(err).ToNot(HaveOccurred())
Expect(str.(*mockGenericStream).id).To(BeZero())
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
m.SetMaxStream(0)
Eventually(done).Should(BeClosed())
})
It("stops opening synchronously when it is closed", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
testErr := errors.New("test error")
done := make(chan struct{})
go func() {
defer GinkgoRecover()
_, err := m.OpenStreamSync()
Expect(err).To(MatchError(testErr))
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
m.CloseWithError(testErr)
Eventually(done).Should(BeClosed())
})
It("doesn't reduce the stream limit", func() {
m.SetMaxStream(firstNewStream + 4)
m.SetMaxStream(firstNewStream)
_, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
str, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.(*mockGenericStream).id).To(Equal(firstNewStream + 4))
})
It("queues a STREAM_ID_BLOCKED frame if no stream can be opened", func() {
m.SetMaxStream(firstNewStream + 5*4)
// open the 6 allowed streams
for i := 0; i < 6; i++ {
_, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
}
mockSender.EXPECT().queueControlFrame(gomock.Any()).Do(func(f wire.Frame) {
Expect(f.(*wire.StreamsBlockedFrame).StreamLimit).To(BeEquivalentTo(6))
})
_, err := m.OpenStream()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(errTooManyOpenStreams.Error()))
})
It("only sends one STREAM_ID_BLOCKED frame for one stream ID", func() {
m.SetMaxStream(firstNewStream)
mockSender.EXPECT().queueControlFrame(gomock.Any()).Do(func(f wire.Frame) {
Expect(f.(*wire.StreamsBlockedFrame).StreamLimit).To(BeEquivalentTo(1))
})
_, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
// try to open a stream twice, but expect only one STREAM_ID_BLOCKED to be sent
_, err = m.OpenStream()
expectTooManyStreamsError(err)
_, err = m.OpenStream()
expectTooManyStreamsError(err)
})
})
})