github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/importer/chunk/splitting_test.go (about) 1 package chunk 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 u "github.com/ipfs/go-ipfs/util" 9 ) 10 11 func randBuf(t *testing.T, size int) []byte { 12 buf := make([]byte, size) 13 if _, err := u.NewTimeSeededRand().Read(buf); err != nil { 14 t.Fatal("failed to read enough randomness") 15 } 16 return buf 17 } 18 19 func copyBuf(buf []byte) []byte { 20 cpy := make([]byte, len(buf)) 21 copy(cpy, buf) 22 return cpy 23 } 24 25 func TestSizeSplitterIsDeterministic(t *testing.T) { 26 if testing.Short() { 27 t.SkipNow() 28 } 29 30 test := func() { 31 bufR := randBuf(t, 10000000) // crank this up to satisfy yourself. 32 bufA := copyBuf(bufR) 33 bufB := copyBuf(bufR) 34 35 chunksA, _ := Chan(DefaultSplitter(bytes.NewReader(bufA))) 36 chunksB, _ := Chan(DefaultSplitter(bytes.NewReader(bufB))) 37 38 for n := 0; ; n++ { 39 a, moreA := <-chunksA 40 b, moreB := <-chunksB 41 42 if !moreA { 43 if moreB { 44 t.Fatal("A ended, B didnt.") 45 } 46 return 47 } 48 49 if !bytes.Equal(a, b) { 50 t.Fatalf("chunk %d not equal", n) 51 } 52 } 53 } 54 55 for run := 0; run < 1; run++ { // crank this up to satisfy yourself. 56 test() 57 } 58 } 59 60 func TestSizeSplitterFillsChunks(t *testing.T) { 61 if testing.Short() { 62 t.SkipNow() 63 } 64 65 max := 10000000 66 b := randBuf(t, max) 67 r := &clipReader{r: bytes.NewReader(b), size: 4000} 68 chunksize := int64(1024 * 256) 69 c, _ := Chan(NewSizeSplitter(r, chunksize)) 70 71 sofar := 0 72 whole := make([]byte, max) 73 for chunk := range c { 74 75 bc := b[sofar : sofar+len(chunk)] 76 if !bytes.Equal(bc, chunk) { 77 t.Fatalf("chunk not correct: (sofar: %d) %d != %d, %v != %v", sofar, len(bc), len(chunk), bc[:100], chunk[:100]) 78 } 79 80 copy(whole[sofar:], chunk) 81 82 sofar += len(chunk) 83 if sofar != max && len(chunk) < int(chunksize) { 84 t.Fatal("sizesplitter split at a smaller size") 85 } 86 } 87 88 if !bytes.Equal(b, whole) { 89 t.Fatal("splitter did not split right") 90 } 91 } 92 93 type clipReader struct { 94 size int 95 r io.Reader 96 } 97 98 func (s *clipReader) Read(buf []byte) (int, error) { 99 100 // clip the incoming buffer to produce smaller chunks 101 if len(buf) > s.size { 102 buf = buf[:s.size] 103 } 104 105 return s.r.Read(buf) 106 }