From 37ce1863c09ded76fbfd51969b6d46811528d7f9 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Thu, 30 Oct 2014 02:49:39 -0700 Subject: [PATCH] test splitting is deterministic. (it is) --- importer/chunk/splitting_test.go | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 importer/chunk/splitting_test.go diff --git a/importer/chunk/splitting_test.go b/importer/chunk/splitting_test.go new file mode 100644 index 000000000..0ecb143cb --- /dev/null +++ b/importer/chunk/splitting_test.go @@ -0,0 +1,53 @@ +package chunk + +import ( + "bytes" + "crypto/rand" + "testing" +) + +func randBuf(t *testing.T, size int) []byte { + buf := make([]byte, size) + if _, err := rand.Read(buf); err != nil { + t.Fatal("failed to read enough randomness") + } + return buf +} + +func copyBuf(buf []byte) []byte { + cpy := make([]byte, len(buf)) + copy(cpy, buf) + return cpy +} + +func TestSizeSplitterIsDeterministic(t *testing.T) { + + test := func() { + bufR := randBuf(t, 10000000) // crank this up to satisfy yourself. + bufA := copyBuf(bufR) + bufB := copyBuf(bufR) + + chunksA := DefaultSplitter.Split(bytes.NewReader(bufA)) + chunksB := DefaultSplitter.Split(bytes.NewReader(bufB)) + + for n := 0; ; n++ { + a, moreA := <-chunksA + b, moreB := <-chunksB + + if !moreA { + if moreB { + t.Fatal("A ended, B didnt.") + } + return + } + + if !bytes.Equal(a, b) { + t.Fatalf("chunk %d not equal", n) + } + } + } + + for run := 0; run < 1; run++ { // crank this up to satisfy yourself. + test() + } +}