2018-03-26 12:32:43 +00:00
|
|
|
package jstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
. "testing"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
type brTest struct {
|
2018-03-26 12:32:43 +00:00
|
|
|
wsSuffix []byte // whitespace
|
|
|
|
body []byte
|
|
|
|
shouldCancel bool
|
|
|
|
intoSize int
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
func randBRTest(minBodySize, maxBodySize int) brTest {
|
2018-03-26 12:32:43 +00:00
|
|
|
var whitespace = []byte{' ', '\n', '\t', '\r'}
|
|
|
|
genWhitespace := func(n int) []byte {
|
|
|
|
ws := make([]byte, n)
|
|
|
|
for i := range ws {
|
|
|
|
ws[i] = whitespace[mtest.Rand.Intn(len(whitespace))]
|
|
|
|
}
|
|
|
|
return ws
|
|
|
|
}
|
|
|
|
|
|
|
|
body := mtest.RandBytes(minBodySize + mtest.Rand.Intn(maxBodySize-minBodySize))
|
2018-05-27 03:38:03 +00:00
|
|
|
return brTest{
|
2018-03-26 12:32:43 +00:00
|
|
|
wsSuffix: genWhitespace(mtest.Rand.Intn(10)),
|
|
|
|
body: body,
|
|
|
|
intoSize: 1 + mtest.Rand.Intn(len(body)+1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
func (bt brTest) msgAndArgs() []interface{} {
|
2018-03-26 12:32:43 +00:00
|
|
|
return []interface{}{"bt:%#v len(body):%d", bt, len(bt.body)}
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
func (bt brTest) mkBytes() []byte {
|
2018-03-26 12:32:43 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
enc := base64.NewEncoder(base64.StdEncoding, buf)
|
|
|
|
|
|
|
|
if bt.shouldCancel {
|
|
|
|
enc.Write(bt.body[:len(bt.body)/2])
|
|
|
|
enc.Close()
|
|
|
|
buf.WriteByte(bbCancel)
|
|
|
|
} else {
|
|
|
|
enc.Write(bt.body)
|
|
|
|
enc.Close()
|
|
|
|
buf.WriteByte(bbEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Write(bt.wsSuffix)
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
func (bt brTest) do(t *T) bool {
|
2018-03-26 12:32:43 +00:00
|
|
|
buf := bytes.NewBuffer(bt.mkBytes())
|
2018-05-27 03:38:03 +00:00
|
|
|
br := newBytesReader(buf)
|
2018-03-26 12:32:43 +00:00
|
|
|
|
|
|
|
into := make([]byte, bt.intoSize)
|
|
|
|
outBuf := new(bytes.Buffer)
|
2018-05-27 03:38:03 +00:00
|
|
|
_, err := io.CopyBuffer(outBuf, br, into)
|
2018-03-26 12:32:43 +00:00
|
|
|
if bt.shouldCancel {
|
|
|
|
return assert.Equal(t, ErrCanceled, err, bt.msgAndArgs()...)
|
|
|
|
}
|
|
|
|
if !assert.NoError(t, err, bt.msgAndArgs()...) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !assert.Equal(t, bt.body, outBuf.Bytes(), bt.msgAndArgs()...) {
|
|
|
|
return false
|
|
|
|
}
|
2018-05-27 03:38:03 +00:00
|
|
|
fullRest := append(br.dr.rest, buf.Bytes()...)
|
2018-03-26 12:32:43 +00:00
|
|
|
if len(bt.wsSuffix) == 0 {
|
|
|
|
return assert.Empty(t, fullRest, bt.msgAndArgs()...)
|
|
|
|
}
|
|
|
|
return assert.Equal(t, bt.wsSuffix, fullRest, bt.msgAndArgs()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestByteBlobReader(t *T) {
|
|
|
|
// some sanity tests
|
2018-05-27 03:38:03 +00:00
|
|
|
brTest{
|
2018-03-26 12:32:43 +00:00
|
|
|
body: []byte{2, 3, 4, 5},
|
|
|
|
intoSize: 4,
|
|
|
|
}.do(t)
|
2018-05-27 03:38:03 +00:00
|
|
|
brTest{
|
2018-03-26 12:32:43 +00:00
|
|
|
body: []byte{2, 3, 4, 5},
|
|
|
|
intoSize: 3,
|
|
|
|
}.do(t)
|
2018-05-27 03:38:03 +00:00
|
|
|
brTest{
|
2018-03-26 12:32:43 +00:00
|
|
|
body: []byte{2, 3, 4, 5},
|
|
|
|
shouldCancel: true,
|
|
|
|
intoSize: 3,
|
|
|
|
}.do(t)
|
|
|
|
|
|
|
|
// fuzz this bitch
|
|
|
|
for i := 0; i < 50000; i++ {
|
2018-05-27 03:38:03 +00:00
|
|
|
bt := randBRTest(0, 1000)
|
2018-03-26 12:32:43 +00:00
|
|
|
if !bt.do(t) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bt.shouldCancel = true
|
|
|
|
if !bt.do(t) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkByteBlobReader(b *B) {
|
|
|
|
type bench struct {
|
2018-05-27 03:38:03 +00:00
|
|
|
bt brTest
|
2018-03-26 12:32:43 +00:00
|
|
|
body []byte
|
|
|
|
buf *bytes.Reader
|
|
|
|
cpBuf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
mkTestSet := func(minBodySize, maxBodySize int) []bench {
|
|
|
|
n := 100
|
|
|
|
benches := make([]bench, n)
|
|
|
|
for i := range benches {
|
2018-05-27 03:38:03 +00:00
|
|
|
bt := randBRTest(minBodySize, maxBodySize)
|
2018-03-26 12:32:43 +00:00
|
|
|
body := bt.mkBytes()
|
|
|
|
benches[i] = bench{
|
|
|
|
bt: bt,
|
|
|
|
body: body,
|
|
|
|
buf: bytes.NewReader(nil),
|
|
|
|
cpBuf: make([]byte, bt.intoSize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return benches
|
|
|
|
}
|
|
|
|
|
|
|
|
testRaw := func(b *B, benches []bench) {
|
|
|
|
j := 0
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if j >= len(benches) {
|
|
|
|
j = 0
|
|
|
|
}
|
|
|
|
benches[j].buf.Reset(benches[j].body)
|
|
|
|
io.CopyBuffer(ioutil.Discard, benches[j].buf, benches[j].cpBuf)
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
testBR := func(b *B, benches []bench) {
|
2018-03-26 12:32:43 +00:00
|
|
|
j := 0
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if j >= len(benches) {
|
|
|
|
j = 0
|
|
|
|
}
|
|
|
|
benches[j].buf.Reset(benches[j].body)
|
2018-05-27 03:38:03 +00:00
|
|
|
br := newBytesReader(benches[j].buf)
|
|
|
|
io.CopyBuffer(ioutil.Discard, br, benches[j].cpBuf)
|
2018-03-26 12:32:43 +00:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
benches := []struct {
|
|
|
|
name string
|
|
|
|
minBodySize, maxBodySize int
|
|
|
|
}{
|
|
|
|
{"small", 0, 1000},
|
|
|
|
{"medium", 1000, 10000},
|
|
|
|
{"large", 10000, 100000},
|
|
|
|
{"xlarge", 100000, 1000000},
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
for i := range benches {
|
|
|
|
b.Run(benches[i].name, func(b *B) {
|
|
|
|
set := mkTestSet(benches[i].minBodySize, benches[i].maxBodySize)
|
|
|
|
b.StartTimer()
|
|
|
|
b.Run("raw", func(b *B) {
|
|
|
|
testRaw(b, set)
|
|
|
|
})
|
2018-05-27 03:38:03 +00:00
|
|
|
b.Run("br", func(b *B) {
|
|
|
|
testBR(b, set)
|
2018-03-26 12:32:43 +00:00
|
|
|
})
|
|
|
|
b.StopTimer()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|