-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirror_test.go
92 lines (69 loc) · 1.29 KB
/
mirror_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
91
92
package streammux_test
import (
"bytes"
"crypto/sha256"
"io"
"os"
"testing"
"github.com/bh107/streammux"
"github.com/bh107/streammux/pkg/util/testutil"
)
func TestMirror(t *testing.T) {
blkdevs := []io.ReadWriteCloser{
testutil.NewBlockDevice(1 << 20),
testutil.NewFaultyDevice(1<<20, 20),
}
m := streammux.NewMirror(blkdevs...)
m.Open()
data := make([]byte, 1<<20)
f, err := os.Open("/dev/urandom")
if err != nil {
t.Fatal(err)
}
n, err := f.Read(data)
if err != nil || n != len(data) {
t.Fatal(err)
}
origSha256Sum := sha256.Sum256(data)
buf := bytes.NewBuffer(data)
p := make([]byte, 1024)
for {
_, err := buf.Read(p)
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
n, err := m.Write(p)
if err != nil || n != len(p) {
t.Fatal(err)
}
}
// close to reset position
if err := m.Close(); err != nil {
t.Fatal(err)
}
// replace failed drive (launched sync)
m.Replace(1, testutil.NewBlockDevice(1<<20))
// reopen
m.Open()
buf.Reset()
for {
_, err := m.Read(p)
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
_, err = buf.Write(p)
if err != nil {
t.Fatal(err)
}
}
newSha256Sum := sha256.Sum256(buf.Bytes())
if origSha256Sum != newSha256Sum {
t.Fatal("origSha256Sum != newSha256Sum")
}
}