-
Notifications
You must be signed in to change notification settings - Fork 0
/
poc_test.go
66 lines (55 loc) · 1.31 KB
/
poc_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
package poc
import (
"bytes"
"io"
"testing"
"github.com/klauspost/compress/zstd"
)
// CursedReader wraps a reader and returns zero bytes every other read.
// This is used to test the ability of the consumer to handle empty reads without EOF,
// which can happen when reading from a network connection.
type CursedReader struct {
io.Reader
numReads int
}
func (r *CursedReader) Read(p []byte) (n int, err error) {
r.numReads++
if r.numReads%2 == 0 {
return 0, nil
}
return r.Reader.Read(p)
}
func NewCursedReader(r io.Reader) *CursedReader { return &CursedReader{Reader: r} }
func zeroLengthReadTest() error {
// create a buffer with some data; the data doesn't matter
srcBuf := bytes.NewBuffer(make([]byte, 1024))
dstBuf := &bytes.Buffer{}
// compress the data
enc, err := zstd.NewWriter(dstBuf, zstd.WithEncoderLevel(zstd.SpeedFastest))
if err != nil {
return err
}
_, err = enc.Write(srcBuf.Bytes())
if err != nil {
return err
}
err = enc.Close()
if err != nil {
return err
}
// now let's read it back using a cursed reader
dec, err := zstd.NewReader(NewCursedReader(dstBuf))
if err != nil {
return err
}
_, err = io.Copy(io.Discard, dec)
if err != nil {
return err
}
return nil
}
func TestZeroLengthReads(t *testing.T) {
if err := zeroLengthReadTest(); err != nil {
t.Error(err)
}
}