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
|
package snappy
import (
"bytes"
"crypto/rand"
"io"
"testing"
"github.com/klauspost/compress/snappy"
goxerialsnappy "github.com/segmentio/kafka-go/compress/snappy/go-xerial-snappy"
)
// Wrap an io.Reader or io.Writer to disable all copy optimizations like
// io.WriterTo or io.ReaderFrom.
// We use this to ensure writes are chunked by io.Copy's internal buffer
// in the tests.
type simpleReader struct{ io.Reader }
type simpleWriter struct{ io.Writer }
func TestXerialReaderSnappy(t *testing.T) {
rawData := new(bytes.Buffer)
rawData.Grow(1024 * 1024)
io.CopyN(rawData, rand.Reader, 1024*1024)
compressedRawData := bytes.NewReader(snappy.Encode(nil, rawData.Bytes()))
decompressedData := new(bytes.Buffer)
io.Copy(decompressedData,
&xerialReader{reader: compressedRawData, decode: snappy.Decode})
b0 := rawData.Bytes()
b1 := decompressedData.Bytes()
if !bytes.Equal(b0, b1) {
t.Error("data mismatch")
}
}
func TestXerialReaderWriter(t *testing.T) {
rawData := new(bytes.Buffer)
rawData.Grow(1024 * 1024)
io.CopyN(rawData, rand.Reader, 1024*1024)
framedData := new(bytes.Buffer)
framedData.Grow(rawData.Len() + 1024)
w := simpleWriter{&xerialWriter{writer: framedData}}
r := simpleReader{bytes.NewReader(rawData.Bytes())}
io.Copy(w, r)
w.Writer.(*xerialWriter).Flush()
unframedData := new(bytes.Buffer)
unframedData.Grow(rawData.Len())
io.Copy(unframedData, &xerialReader{reader: framedData})
b0 := rawData.Bytes()
b1 := unframedData.Bytes()
if !bytes.Equal(b0, b1) {
t.Error("data mismatch")
}
}
func TestXerialFramedCompression(t *testing.T) {
rawData := new(bytes.Buffer)
rawData.Grow(1024 * 1024)
io.CopyN(rawData, rand.Reader, 1024*1024)
framedAndCompressedData := new(bytes.Buffer)
framedAndCompressedData.Grow(rawData.Len())
w := simpleWriter{&xerialWriter{writer: framedAndCompressedData, framed: true, encode: snappy.Encode}}
r := simpleReader{bytes.NewReader(rawData.Bytes())}
io.Copy(w, r)
w.Writer.(*xerialWriter).Flush()
unframedAndDecompressedData := new(bytes.Buffer)
unframedAndDecompressedData.Grow(rawData.Len())
io.Copy(unframedAndDecompressedData,
simpleReader{&xerialReader{reader: framedAndCompressedData, decode: snappy.Decode}})
b0 := rawData.Bytes()
b1 := unframedAndDecompressedData.Bytes()
if !bytes.Equal(b0, b1) {
t.Error("data mismatch")
}
}
func TestXerialFramedCompressionOptimized(t *testing.T) {
rawData := new(bytes.Buffer)
rawData.Grow(1024 * 1024)
io.CopyN(rawData, rand.Reader, 1024*1024)
framedAndCompressedData := new(bytes.Buffer)
framedAndCompressedData.Grow(rawData.Len())
w := &xerialWriter{writer: framedAndCompressedData, framed: true, encode: snappy.Encode}
r := simpleReader{bytes.NewReader(rawData.Bytes())}
io.Copy(w, r)
w.Flush()
unframedAndDecompressedData := new(bytes.Buffer)
unframedAndDecompressedData.Grow(rawData.Len())
io.Copy(unframedAndDecompressedData,
&xerialReader{reader: framedAndCompressedData, decode: snappy.Decode})
b0 := rawData.Bytes()
b1 := unframedAndDecompressedData.Bytes()
if !bytes.Equal(b0, b1) {
t.Error("data mismatch")
}
}
func TestXerialReaderAgainstGoXerialSnappy(t *testing.T) {
rawData := new(bytes.Buffer)
rawData.Grow(1024 * 1024)
io.CopyN(rawData, rand.Reader, 1024*1024)
rawBytes := rawData.Bytes()
framedAndCompressedData := []byte{}
const chunkSize = 999
for i := 0; i < len(rawBytes); i += chunkSize {
j := i + chunkSize
if j > len(rawBytes) {
j = len(rawBytes)
}
framedAndCompressedData = goxerialsnappy.EncodeStream(framedAndCompressedData, rawBytes[i:j])
}
unframedAndDecompressedData := new(bytes.Buffer)
unframedAndDecompressedData.Grow(rawData.Len())
io.Copy(unframedAndDecompressedData,
&xerialReader{reader: bytes.NewReader(framedAndCompressedData), decode: snappy.Decode})
b0 := rawBytes
b1 := unframedAndDecompressedData.Bytes()
if !bytes.Equal(b0, b1) {
t.Error("data mismatch")
}
}
func TestXerialWriterAgainstGoXerialSnappy(t *testing.T) {
rawData := new(bytes.Buffer)
rawData.Grow(1024 * 1024)
io.CopyN(rawData, rand.Reader, 1024*1024)
framedAndCompressedData := new(bytes.Buffer)
framedAndCompressedData.Grow(rawData.Len())
w := &xerialWriter{writer: framedAndCompressedData, framed: true, encode: snappy.Encode}
r := simpleReader{bytes.NewReader(rawData.Bytes())}
io.Copy(w, r)
w.Flush()
unframedAndDecompressedData, err := goxerialsnappy.Decode(framedAndCompressedData.Bytes())
if err != nil {
t.Error(err)
}
b0 := rawData.Bytes()
b1 := unframedAndDecompressedData
if !bytes.Equal(b0, b1) {
t.Error("data mismatch")
}
}
|