@cryptotaxi247 / kubo / commits / 483ccf9f5

better test exercises saved buf

Juan Batiz-Benet committed Oct 30, 2014 at 04:33 UTC 483ccf9f5fcf94fbc94da0a7db593bd3a6da1e16
1 file changed +13 -7
util/util_test.go
+13 -7
@@ -2,8 +2,6 @@ package util
2
3 import (
4 "bytes"
5 - "fmt"
6 - "io/ioutil"
5 "math/rand"
6 "testing"
7
@@ -33,6 +31,7 @@ func TestKey(t *testing.T) {
31 func TestByteChanReader(t *testing.T) {
32
33 var data bytes.Buffer
34 + var data2 bytes.Buffer
35 dch := make(chan []byte, 8)
36 randr := NewTimeSeededRand()
37
@@ -42,19 +41,26 @@ func TestByteChanReader(t *testing.T) {
41 chunk := make([]byte, rand.Intn(100000)+10)
42 randr.Read(chunk)
43 data.Write(chunk)
45 - fmt.Printf("chunk: %6.d %v\n", len(chunk), chunk[:10])
44 + // fmt.Printf("chunk: %6.d %v\n", len(chunk), chunk[:10])
45 dch <- chunk
46 }
47 }()
48
49 read := NewByteChanReader(dch)
51 - out, err := ioutil.ReadAll(read)
52 - if err != nil {
53 - t.Fatal(err)
50 +
51 + // read in random, weird sizes to exercise saving buffer.
52 + for {
53 + buf := make([]byte, rand.Intn(10)*10)
54 + n, err := read.Read(buf)
55 + data2.Write(buf[:n])
56 + // fmt.Printf("read: %6.d\n", n)
57 + if err != nil {
58 + break
59 + }
60 }
61
62 // fmt.Printf("lens: %d == %d\n", len(out), len(data.Bytes()))
57 - if !bytes.Equal(out, data.Bytes()) {
63 + if !bytes.Equal(data2.Bytes(), data.Bytes()) {
64 t.Fatal("Reader failed to stream correct bytes")
65 }
66 }