-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffered_reader_test.go
380 lines (309 loc) · 11.4 KB
/
buffered_reader_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
package adaptivepool
import (
"bytes"
"errors"
"fmt"
"io"
"slices"
"testing"
"testing/iotest"
)
// testData with non-ASCII characters at the beginning to test the ReadRune
// method correctly returning UTF-8 runes.
const testData = `痛苦
I was looking for a job and then I found a job
And Heaven knows, I'm miserable now
`
var _ interface { // assert interfaces from standard library
io.ReadSeekCloser
io.ByteScanner
io.RuneScanner
io.WriterTo
} = (*BufferedReader)(nil)
func (p *ReaderBufferer) stats() Stats {
return p.bufPool.getStats()
}
func TestReaderBufferer(t *testing.T) {
t.Parallel()
errTest := errors.New("hated because of great qualities")
errTest2 := errors.New("loved despite of great faults")
t.Run("Reader: happy path - empty", func(t *testing.T) {
t.Parallel()
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
br, err := brr.Reader(bytes.NewReader(nil))
zero(t, err, "Reader error on empty io.Reader")
equal(t, true, br != nil, "nil Reader")
zero(t, iotest.TestReader(br, nil),
"iotest.TestReader error on non-closed *BufferedReader")
// first call Bytes on br so we later don't put it back into the pool.
// This is because if Bytes is called, then the buffer no longer belongs
// to us
finishAndTestBufferedReader(t, br, false)
st := brr.stats()
zero(t, st.N(), "should not have been put back into the pool")
})
t.Run("Reader: happy path - empty, request alloc", func(t *testing.T) {
t.Parallel()
const reqAlloc = 1024
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
br, err := brr.ReaderWithCost(bytes.NewReader(nil), reqAlloc)
zero(t, err, "Reader error on empty io.Reader")
equal(t, true, br != nil, "nil Reader")
zero(t, iotest.TestReader(br, nil),
"iotest.TestReader error on non-closed *BufferedReader")
b := br.Bytes()
equal(t, reqAlloc, cap(b), "unexpected capacity")
st := brr.stats()
zero(t, st.N(), "should not have been put back into the pool")
})
t.Run("ReadCloser: happy path - non-empty", func(t *testing.T) {
t.Parallel()
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
rc := io.NopCloser(bytes.NewReader([]byte(testData)))
br, err := brr.ReadCloser(rc)
zero(t, err, "Reader error on non-empty io.Reader")
equal(t, true, br != nil, "nil Reader")
zero(t, iotest.TestReader(br, []byte(testData)),
"iotest.TestReader error on non-closed *BufferedReader")
finishAndTestBufferedReader(t, br, true)
st := brr.stats()
equal(t, 1, st.N(), "should have been put back into the pool")
})
t.Run("ReadCloser: happy path - non-empty, request alloc",
func(t *testing.T) {
t.Parallel()
const reqAlloc = 1024
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
rc := io.NopCloser(bytes.NewReader([]byte(testData)))
br, err := brr.ReadCloserWithCost(rc, reqAlloc)
zero(t, err, "Reader error on non-empty io.Reader")
equal(t, true, br != nil, "nil Reader")
b := br.Bytes()
equal(t, reqAlloc, cap(b), "unexpected capacity")
st := brr.stats()
equal(t, 0, st.N(), "should not have been put back into the pool")
})
t.Run("Reader: fail reading", func(t *testing.T) {
t.Parallel()
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
br, err := brr.Reader(iotest.ErrReader(errTest))
equal(t, true, errors.Is(err, errTest), "should have failed reading")
zero(t, br, "should return nil on error")
})
t.Run("ReadCloser: fail reading", func(t *testing.T) {
t.Parallel()
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
rc := io.NopCloser(iotest.ErrReader(errTest))
br, err := brr.ReadCloser(rc)
equal(t, true, errors.Is(err, errTest), "should have failed reading")
zero(t, br, "should return nil on error")
})
t.Run("ReadCloser: fail closing", func(t *testing.T) {
t.Parallel()
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
rc := readCloser{
Reader: bytes.NewReader(nil),
Closer: closerFunc(func() error { return errTest }),
}
br, err := brr.ReadCloser(rc)
equal(t, true, errors.Is(err, errTest), "should have failed closing")
zero(t, br, "should return nil on error")
})
t.Run("ReadCloser: fail reading and closing", func(t *testing.T) {
t.Parallel()
brr := NewReaderBufferer(NormalEstimator{2, 0}, 500)
rc := readCloser{
Reader: iotest.ErrReader(errTest),
Closer: closerFunc(func() error { return errTest2 }),
}
br, err := brr.ReadCloser(rc)
equal(t, true, errors.Is(err, errTest), "should have failed reading")
equal(t, true, errors.Is(err, errTest2), "should have failed closing")
zero(t, br, "should return nil on error")
})
}
type readCloser struct {
io.Reader
io.Closer
}
type closerFunc func() error
func (f closerFunc) Close() error { return f() }
func newTestBufferedReader(buf []byte) *BufferedReader {
return &BufferedReader{
reader: bytes.NewReader(buf),
buf: buf,
release: func([]byte, *bytes.Reader) {},
}
}
func TestBufferedReader(t *testing.T) {
t.Parallel()
t.Run("without data - Close then Bytes", func(t *testing.T) {
t.Parallel()
br := newTestBufferedReader(nil)
zero(t, iotest.TestReader(br, nil),
"iotest.TestReader error on non-closed *BufferedReader")
finishAndTestBufferedReader(t, br, true)
})
t.Run("without data - Bytes then Close", func(t *testing.T) {
t.Parallel()
br := newTestBufferedReader(nil)
zero(t, iotest.TestReader(br, nil),
"iotest.TestReader error on non-closed *BufferedReader")
finishAndTestBufferedReader(t, br, false)
})
t.Run("with data - Close then Bytes", func(t *testing.T) {
t.Parallel()
br := newTestBufferedReader([]byte(testData))
zero(t, iotest.TestReader(br, []byte(testData)),
"iotest.TestReader error on non-closed *BufferedReader")
finishAndTestBufferedReader(t, br, true)
})
t.Run("with data - Bytes then Close", func(t *testing.T) {
t.Parallel()
br := newTestBufferedReader([]byte(testData))
zero(t, iotest.TestReader(br, []byte(testData)),
"iotest.TestReader error on non-closed *BufferedReader")
finishAndTestBufferedReader(t, br, false)
})
}
func finishAndTestBufferedReader(t *testing.T, br *BufferedReader,
// true: call Close first, then Bytes; false: call Bytes first, then Close
closeFirst bool,
) {
finishAndTestBufferedReaderInternal(t, br, closeFirst, true)
}
func finishAndTestBufferedReaderInternal(t *testing.T, br *BufferedReader,
closeFirst bool, runTheOtherAfter bool) {
t.Helper()
_, err := br.Seek(0, io.SeekStart)
zero(t, err, "Seek(0, io.SeekStart)")
if l := br.Len(); l > 0 {
// io.ByteScanner methods
_, err := br.ReadByte()
zero(t, err, "ReadByte on non-empty *BufferedReader")
gotL := br.Len()
equal(t, l-1, gotL, "should have one less byte; want: %d, got: %d", l-1,
gotL)
err = br.UnreadByte()
zero(t, err, "UnreadByte after ReadByte")
gotL = br.Len()
equal(t, l, gotL, "should have the same original length; want: %d, "+
"got: %d", l-1, gotL)
// io.RuneScanner methods
_, s, err := br.ReadRune()
zero(t, err, "ReadRune on non-empty *BufferedReader")
if s < 2 {
t.Fatalf("unexpected rune cost %d from non-empty *BufferedReader "+
"(remember to use test data starting with non-ASCII, wide "+
"characters", s)
}
gotL = br.Len()
wantL := l - s
equal(t, wantL, gotL, "should have %d less byte(s); want: %d, got: %d",
s, wantL, gotL)
err = br.UnreadRune()
zero(t, err, "UnreadRune after ReadRune")
gotL = br.Len()
equal(t, l, gotL, "should have the same original length; want: %d, "+
"got: %d", l-1, gotL)
}
if closeFirst {
zero(t, br.Close(), "close *BufferedReader")
zero(t, br.Close(), "close *BufferedReader for the second time")
} else {
_, err := br.Seek(0, io.SeekStart)
zero(t, err, "Seek(0, io.SeekStart)")
l := br.Len()
buf := new(bytes.Buffer)
gotL, err := br.WriteTo(buf)
zero(t, err, "WriteTo")
equal(t, int64(l), gotL, "unexpected read buf with length %d, "+
"expected length %d; data: %q", gotL, l, buf.String())
wantBuf := buf.Bytes()
gotBuf := br.Bytes()
equal(t, true, slices.Equal(wantBuf, gotBuf), "should return same "+
"data\n\twant: %q\n\tgot: %q", wantBuf, gotBuf)
if !runTheOtherAfter {
zero(t, len(wantBuf), "no data should be read after closing, "+
"got: %q", wantBuf)
zero(t, len(gotBuf), "no data should be returned after closing, "+
"got: %q", gotBuf)
}
}
zero(t, br.Len(), "should have length zero after closing")
zero(t, iotest.TestReader(br, nil),
"iotest.TestReader error on closed *BufferedReader")
// as a closed *BufferedReader will no longer use its internal
// *bytes.Reader, we will test that once closed it behaves the same as an
// empty one, hence we compare all its ouputs. When comparing errors, we
// will prioritize detecting io.EOF, otherwise just make sure they both err
// under the same conditions
isEOF := func(err error) string {
if errors.Is(err, io.EOF) {
return "yes"
}
return fmt.Sprintf("no, it is %v", err)
}
compareErrs := func(want, got error) error {
if (want == nil) != (got == nil) {
return fmt.Errorf("want error: %w; got error: %w", want, got)
}
if errors.Is(want, io.EOF) != errors.Is(got, io.EOF) {
return fmt.Errorf("disagree on io.EOF; want error is io.EOF: %v; "+
"got error is io.EOF: %v", isEOF(want), isEOF(got))
}
return nil
}
emptyBytesReader := bytes.NewReader(nil)
wantInt, wantErr := emptyBytesReader.Read(make([]byte, 10))
gotInt, gotErr := br.Read(make([]byte, 10))
zero(t, compareErrs(wantErr, gotErr), "disagree on Read error after close")
equal(t, wantInt, gotInt, "disagree on Read int after close")
wantInt64, wantErr := emptyBytesReader.Seek(0, io.SeekStart)
gotInt64, gotErr := br.Seek(0, io.SeekStart)
zero(t, compareErrs(wantErr, gotErr),
"disagree on Seek(0, io.SeekStart) error after close")
equal(t, wantInt64, gotInt64,
"disagree on Seek(0, io.SeekStart) int64 after close")
wantInt64, wantErr = emptyBytesReader.Seek(-1, io.SeekStart)
gotInt64, gotErr = br.Seek(-1, io.SeekStart)
zero(t, compareErrs(wantErr, gotErr),
"disagree on Seek(-1, io.SeekStart) error after close")
equal(t, wantInt64, gotInt64,
"disagree on Seek(-1, io.SeekStart) int64 after close")
wantInt64, wantErr = emptyBytesReader.Seek(0, -1)
gotInt64, gotErr = br.Seek(0, -1)
zero(t, compareErrs(wantErr, gotErr),
"disagree on Seek(0, -1) error after close")
equal(t, wantInt64, gotInt64,
"disagree on Seek(0, -1) int64 after close")
wantByte, wantErr := emptyBytesReader.ReadByte()
gotByte, gotErr := br.ReadByte()
zero(t, compareErrs(wantErr, gotErr),
"disagree on ReadByte error after close")
equal(t, wantByte, gotByte,
"disagree on ReadByte byte after close")
wantErr = emptyBytesReader.UnreadByte()
gotErr = br.UnreadByte()
zero(t, compareErrs(wantErr, gotErr),
"disagree on UnreadByte error after close")
wantRune, wantInt, wantErr := emptyBytesReader.ReadRune()
gotRune, gotInt, gotErr := br.ReadRune()
zero(t, compareErrs(wantErr, gotErr),
"disagree on ReadRune error after close")
equal(t, wantInt, gotInt, "disagree on ReadRune int after close")
equal(t, wantRune, gotRune, "disagree on ReadRune rune after close")
wantErr = emptyBytesReader.UnreadRune()
gotErr = br.UnreadRune()
zero(t, compareErrs(wantErr, gotErr),
"disagree on UnreadRune error after close")
wantInt64, wantErr = emptyBytesReader.WriteTo(io.Discard)
gotInt64, gotErr = br.WriteTo(io.Discard)
zero(t, compareErrs(wantErr, gotErr),
"disagree on WriteTo error after close")
equal(t, wantInt64, gotInt64,
"disagree on WriteTo int64 after close")
if runTheOtherAfter {
finishAndTestBufferedReaderInternal(t, br, !closeFirst, false)
}
}