-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from jbenet/dont-write-twice
blockservice: dont write blocks twice
- Loading branch information
Showing
6 changed files
with
123 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters