-
Notifications
You must be signed in to change notification settings - Fork 16
/
stream_test.go
195 lines (174 loc) · 6.06 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
package minq
import (
"fmt"
"io"
"runtime"
"testing"
)
type testStreamFixture struct {
t *testing.T
name string
log loggingFunction
r *recvStreamBase
w *sendStreamBase
b []byte
}
func (f *testStreamFixture) read() {
assertX(f.t, f.r.readable, "stream should be readable")
f.b = make([]byte, 1024)
n, err := f.r.read(f.b)
assertNotError(f.t, err, "Should be able to read bytes")
f.b = f.b[:n]
assertX(f.t, f.r.clearReadable(), "should have been readable")
}
func (f *testStreamFixture) readExpectError(exerr error) {
f.b = make([]byte, 1024)
n, err := f.r.read(f.b)
assertError(f.t, err, "Should not be able to read bytes")
assertEquals(f.t, exerr, err)
assertEquals(f.t, 0, n)
}
var kTestString1 = []byte("abcdef")
var kTestString2 = []byte("ghijkl")
func newTestStreamFixture(t *testing.T) *testStreamFixture {
pc, _, _, ok := runtime.Caller(1)
name := "unknown"
if ok {
name = runtime.FuncForPC(pc).Name()
}
log := func(tag string, format string, args ...interface{}) {
fullFormat := fmt.Sprintf("%s: %s", name, format)
logf(tag, fullFormat, args...)
}
fc := flowControl{false, 2048, 0}
return &testStreamFixture{
t: t,
name: name,
log: log,
r: &recvStreamBase{streamCommon: streamCommon{log: log, fc: fc}},
w: &sendStreamBase{streamCommon: streamCommon{log: log, fc: fc}},
b: nil,
}
}
func TestStreamInputOneChunk(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.newFrameData(0, false, kTestString1, &flowControl{false, 2048, 0})
assertNotError(t, err, "Data should be accepted")
assertEquals(t, f.r.fc.used, uint64(len(kTestString1)))
assertEquals(t, RecvStreamStateRecv, f.r.state)
f.read()
assertByteEquals(t, f.b, kTestString1)
}
func TestStreamInputTwoChunks(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.newFrameData(0, false, kTestString1, &flowControl{false, 2048, 0})
assertNotError(t, err, "Data should be accepted")
assertEquals(t, f.r.fc.used, uint64(len(kTestString1)))
f.read()
assertByteEquals(t, f.b, kTestString1)
err = f.r.newFrameData(uint64(len(kTestString1)), false, kTestString2, &flowControl{false, 2048, 0})
assertEquals(t, f.r.fc.used, uint64(len(kTestString1)+len(kTestString2)))
f.read()
assertByteEquals(t, f.b, kTestString2)
}
func TestStreamInputCoalesceChunks(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.newFrameData(0, false, kTestString1[:2], &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
err = f.r.newFrameData(2, false, kTestString1[2:], &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
f.read()
assertByteEquals(t, f.b, kTestString1)
}
func TestStreamInputChunksOverlap(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.newFrameData(0, false, kTestString1[:2], &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
err = f.r.newFrameData(0, false, kTestString1, &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
f.read()
assertByteEquals(t, f.b, kTestString1)
}
func TestStreamInputTwoChunksWrongOrder(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.newFrameData(2, false, kTestString1[2:], &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
assertX(t, !f.r.readable, "Stream not should be readable")
assertEquals(t, f.r.fc.used, uint64(len(kTestString1)))
f.readExpectError(ErrorWouldBlock)
err = f.r.newFrameData(0, false, kTestString1[:2], &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
f.read()
assertByteEquals(t, f.b, kTestString1)
}
func TestStreamInputChunk1FinChunk2(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.newFrameData(0, true, kTestString1, &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
assertEquals(t, RecvStreamStateSizeKnown, f.r.state)
f.read()
assertByteEquals(t, f.b, kTestString1)
assertEquals(t, RecvStreamStateDataRead, f.r.state)
err = f.r.newFrameData(uint64(len(kTestString1)), false, kTestString2, &flowControl{false, 2048, 0})
assertEquals(t, err, ErrorFlowControlError)
assertX(t, !f.r.readable, "Stream not be readable")
f.readExpectError(io.EOF)
}
func TestStreamInputShortFinChunkAfterFin(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.newFrameData(0, true, kTestString1, &flowControl{false, 2048, 0})
assertNotError(t, err, "data should be accepted")
assertEquals(t, RecvStreamStateSizeKnown, f.r.state)
f.read()
err = f.r.newFrameData(0, true, kTestString1[:2], &flowControl{false, 2048, 0})
assertNotError(t, err, "overlapping data can be discarded")
}
func TestStreamReadReset(t *testing.T) {
f := newTestStreamFixture(t)
err := f.r.handleReset(10)
assertNotError(t, err, "should accept the reset")
assertEquals(t, RecvStreamStateResetRecvd, f.r.state)
}
func TestStreamWriteClose(t *testing.T) {
f := newTestStreamFixture(t)
f.w.close()
assertEquals(t, SendStreamStateCloseQueued, f.w.state)
}
func TestStreamIncreaseFlowControl(t *testing.T) {
f := newTestStreamFixture(t)
f.w.processMaxStreamData(2050)
f.w.processMaxStreamData(2000)
assertEquals(t, uint64(2050), f.w.fc.max)
}
func countChunkLens(chunks []streamChunk) int {
ct := 0
for _, ch := range chunks {
ct += len(ch.data)
}
return ct
}
func TestStreamBlockRelease(t *testing.T) {
f := newTestStreamFixture(t)
b := make([]byte, 5000)
connFc := &flowControl{false, uint64(len(b)), 0}
n, err := f.w.write(b, connFc)
assertEquals(t, nil, err)
chunks := f.w.outputWritable()
assertEquals(t, 2048, countChunkLens(chunks))
assertEquals(t, 2048, n)
assertEquals(t, uint64(2048), connFc.used)
// Calling output writable again returns 0 chunks
chunks = f.w.outputWritable()
assertEquals(t, 0, countChunkLens(chunks))
// Writing again blocks
_, err = f.w.write(b[n:], connFc)
assertEquals(t, ErrorWouldBlock, err)
// Increasing the limit should let us write.
f.w.processMaxStreamData(8192)
n, err = f.w.write(b[n:], connFc)
assertNotError(t, err, "Writing works")
assertEquals(t, 2952, n)
assertEquals(t, connFc.max, connFc.used)
chunks = f.w.outputWritable()
assertEquals(t, 2952, countChunkLens(chunks))
}