forked from cloudflare/utahfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockfs_test.go
161 lines (141 loc) · 3.33 KB
/
blockfs_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package utahfs
import (
"testing"
"bytes"
"context"
crand "crypto/rand"
"io"
"math/rand"
"time"
"github.com/cloudflare/utahfs/persistent"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type testData struct {
bf *BlockFile
pos int
data []byte
}
func testBFS(t *testing.T, td *testData) {
bf, pos, data := td.bf, td.pos, td.data
dice := rand.Intn(30)
if len(data) > 0 && dice == 0 { // Seek from start.
off := rand.Int63n(int64(len(data)))
got, err := bf.Seek(off, io.SeekStart)
if err != nil {
t.Fatal(err)
}
pos = int(off)
if got != int64(pos) {
t.Fatalf("%v != %v", got, pos)
}
} else if len(data) > 0 && pos != len(data) && dice == 1 { // Seek from current.
off := rand.Int63n(int64(len(data) - pos))
got, err := bf.Seek(off, io.SeekCurrent)
if err != nil {
t.Fatal(err)
}
pos += int(off)
if got != int64(pos) {
t.Fatalf("%v != %v", got, pos)
}
} else if len(data) > 0 && dice == 2 { // Seek from end.
off := rand.Int63n(int64(len(data)))
got, err := bf.Seek(-off, io.SeekEnd)
if err != nil {
t.Fatal(err)
}
pos = len(data) - int(off)
if got != int64(pos) {
t.Fatalf("%v != %v", got, pos)
}
} else if len(data) > 0 && dice == 3 { // Truncate.
size := rand.Int63n(int64(len(data)))
err := bf.Truncate(size)
if err != nil {
t.Fatal(err)
}
pos = int(size)
data = data[:size]
} else if len(data) > 0 && dice < 20 { // Read.
p := make([]byte, rand.Int63n(256)+1)
n, err := bf.Read(p)
if pos == len(data) {
if err != io.EOF {
t.Fatal("expected EOF when reading at end of file")
} else if n != 0 {
t.Fatal("read past end of file")
}
} else if pos != len(data) {
if err != nil {
t.Fatal(err)
} else if n == 0 {
t.Fatal("read zero bytes even though there was data")
}
}
if n > len(p) || pos+n > len(data) {
t.Fatalf("%v violates bounds", n)
} else if !bytes.Equal(data[pos:pos+n], p[:n]) {
t.Fatal("read unexpected data")
}
pos += n
} else { // Write.
p := make([]byte, rand.Int63n(3*256)+1)
crand.Read(p)
n, err := bf.Write(p)
if err != nil {
t.Fatal(err)
} else if n != len(p) {
t.Fatalf("%v != %v", n, len(p))
}
if pos+n > len(data) {
data = append(data[:pos], p...)
} else {
copy(data[pos:pos+n], p)
}
pos += n
}
td.pos, td.data = pos, data
}
func TestBlockFilesystemSplitPtrs(t *testing.T) {
testBlockFilesystem(t, true)
}
func TestBlockFilesystem(t *testing.T) {
testBlockFilesystem(t, false)
}
func testBlockFilesystem(t *testing.T, splitPtrs bool) {
ctx := context.Background()
store := persistent.NewAppStorage(persistent.NewBlockMemory())
if err := store.Start(ctx); err != nil {
t.Fatal(err)
}
bfs, err := NewBlockFilesystem(store, 3, 256, splitPtrs)
if err != nil {
t.Fatal(err)
}
ptrs := make([]uint64, 0)
files := make(map[uint64]*testData)
for i := 0; i < 100000; i++ {
dice := rand.Intn(10000)
if len(ptrs) == 0 || dice == 0 {
ptr, bf, err := bfs.Create(ctx, persistent.Content)
if err != nil {
t.Fatal(err)
}
ptrs = append(ptrs, ptr)
files[ptr] = &testData{bf, 0, nil}
} else {
ptr := ptrs[rand.Intn(len(ptrs))]
testBFS(t, files[ptr])
}
}
t.Logf("created %v files", len(ptrs))
sum := 0
for _, ptr := range ptrs {
n := len(files[ptr].data)
sum += n
t.Logf("- %x: contains %v bytes", ptr, n)
}
t.Logf("%v bytes total", sum)
}