-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastcdc_test.go
90 lines (80 loc) · 1.84 KB
/
fastcdc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package fastcdc
import (
"bytes"
"hash/fnv"
"io"
"math/rand"
"testing"
"testing/iotest"
)
func TestCopyIdentical(t *testing.T) {
const datalen = 1<<20 - 1
rnd := rand.New(rand.NewSource(0))
rnd.Seed(0)
h1 := fnv.New64()
_, _ = io.Copy(h1, io.LimitReader(rnd, datalen))
rnd.Seed(0)
h2 := fnv.New64()
_, _ = Copy(h2, io.LimitReader(rnd, datalen))
if h1.Sum64() != h2.Sum64() {
t.Fatal()
}
}
func TestCopyErrReader(t *testing.T) {
_, err := CopyBuffer(io.Discard, iotest.ErrReader(io.ErrClosedPipe), nil)
if err != io.ErrClosedPipe {
t.Fatal()
}
}
func TestCopyRobustness(t *testing.T) {
rnd := rand.New(rand.NewSource(0))
data, _ := io.ReadAll(io.LimitReader(rnd, (1<<20)-1))
buf := make([]byte, 128<<10)
for _, testCase := range []struct {
N string
R io.Reader
}{
{"DataErrReader", iotest.DataErrReader(bytes.NewReader(data))},
{"OneByteReader", iotest.OneByteReader(bytes.NewReader(data))},
{"TimeoutReader", iotest.TimeoutReader(bytes.NewReader(data))},
} {
t.Run(testCase.N, func(t *testing.T) {
n, err := CopyBuffer(io.Discard, testCase.R, buf)
if n != int64(len(data)) || err != nil {
t.Error(n, err)
}
})
}
}
func Benchmark(b *testing.B) {
rnd := rand.New(rand.NewSource(0))
data, _ := io.ReadAll(io.LimitReader(rnd, int64(1<<30)))
buf := make([]byte, 256<<10)
for _, x := range []struct {
Size int
Name string
}{
{1 << 10, "1KB"},
{4 << 10, "4KB"},
{16 << 10, "16KB"},
{64 << 10, "64KB"},
{256 << 10, "256KB"},
{1 << 20, "1MB"},
{4 << 20, "4MB"},
{16 << 20, "16MB"},
{64 << 20, "64MB"},
{256 << 20, "256MB"},
{1 << 30, "1GB"},
} {
x := x
b.Run(x.Name, func(b *testing.B) {
r := bytes.NewReader(data[:x.Size])
b.ResetTimer()
b.SetBytes(int64(x.Size))
for i := 0; i < b.N; i++ {
r.Reset(data[:x.Size])
_, _ = CopyBuffer(io.Discard, r, buf)
}
})
}
}