Commit 483ccf9f authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

better test exercises saved buf

parent adec8dac
...@@ -2,8 +2,6 @@ package util ...@@ -2,8 +2,6 @@ package util
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil"
"math/rand" "math/rand"
"testing" "testing"
...@@ -33,6 +31,7 @@ func TestKey(t *testing.T) { ...@@ -33,6 +31,7 @@ func TestKey(t *testing.T) {
func TestByteChanReader(t *testing.T) { func TestByteChanReader(t *testing.T) {
var data bytes.Buffer var data bytes.Buffer
var data2 bytes.Buffer
dch := make(chan []byte, 8) dch := make(chan []byte, 8)
randr := NewTimeSeededRand() randr := NewTimeSeededRand()
...@@ -42,19 +41,26 @@ func TestByteChanReader(t *testing.T) { ...@@ -42,19 +41,26 @@ func TestByteChanReader(t *testing.T) {
chunk := make([]byte, rand.Intn(100000)+10) chunk := make([]byte, rand.Intn(100000)+10)
randr.Read(chunk) randr.Read(chunk)
data.Write(chunk) data.Write(chunk)
fmt.Printf("chunk: %6.d %v\n", len(chunk), chunk[:10]) // fmt.Printf("chunk: %6.d %v\n", len(chunk), chunk[:10])
dch <- chunk dch <- chunk
} }
}() }()
read := NewByteChanReader(dch) read := NewByteChanReader(dch)
out, err := ioutil.ReadAll(read)
if err != nil { // read in random, weird sizes to exercise saving buffer.
t.Fatal(err) for {
buf := make([]byte, rand.Intn(10)*10)
n, err := read.Read(buf)
data2.Write(buf[:n])
// fmt.Printf("read: %6.d\n", n)
if err != nil {
break
}
} }
// fmt.Printf("lens: %d == %d\n", len(out), len(data.Bytes())) // fmt.Printf("lens: %d == %d\n", len(out), len(data.Bytes()))
if !bytes.Equal(out, data.Bytes()) { if !bytes.Equal(data2.Bytes(), data.Bytes()) {
t.Fatal("Reader failed to stream correct bytes") t.Fatal("Reader failed to stream correct bytes")
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment