-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
161 lines (154 loc) · 4.16 KB
/
cache_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 cachelog
import (
"bytes"
"math/rand"
"os"
"os/exec"
"runtime"
"testing"
)
func TestCache(t *testing.T) {
bug = t.Errorf
tmp := t.TempDir()
c, err := Open(tmp, nil)
if err != nil {
t.Fatalf("Failed to initialize empty cache: %v", err)
}
buf := make([]byte, 1024)
missing, err := c.Get([]byte("test.txt"), 0, buf)
if err != nil {
t.Errorf("Failed to Get() on empty cache: %v", err)
}
if len(missing) != 1 {
t.Errorf("Get(): unexpected number of missing ranges: %d, want 1", len(missing))
}
if missing[0].Start != 0 || missing[0].End != 1024 {
t.Errorf("Get(): incorrect missing Entry: [%d, %d] want [0, 1024]", missing[0].Start, missing[0].End)
}
for i := 0; len(buf) > i; i++ {
buf[i] = byte(i % 256)
}
if err := c.Put([]byte("test.txt"), 512, buf); err != nil {
t.Fatalf("Put() failed: %v", err)
}
buf = make([]byte, 1024)
missing, err = c.Get([]byte("test.txt"), 0, buf)
if err != nil {
t.Errorf("Failed to Get() on empty cache: %v", err)
}
if len(missing) != 1 {
t.Errorf("Get(): unexpected number of missing ranges: %d, want 1", len(missing))
}
if missing[0].Start != 0 || missing[0].End != 512 {
t.Errorf("Get(): incorrect missing Entry: [%d, %d] want [0, 512]", missing[0].Start, missing[0].End)
}
for i := int(missing[0].End); len(buf) > i; i++ {
if buf[i] != byte(i%256) {
t.Errorf("Put()+Get() returned incorrect data at buf offset %d", i)
break
}
}
if err := c.Delete([]byte("test.txt"), 768, 64); err != nil {
t.Fatalf("Put() failed: %v", err)
}
c = nil
runtime.GC() // Ensure os.File's get closed.
c, err = Open(tmp, nil)
if err != nil {
t.Fatalf("Failed to open cache: %v", err)
}
buf = make([]byte, 1024)
missing, err = c.Get([]byte("test.txt"), 0, buf)
if err != nil {
t.Errorf("Failed to Get() on empty cache: %v", err)
}
if len(missing) != 2 {
t.Errorf("Get(): unexpected number of missing ranges: %d, want 2", len(missing))
}
if missing[0].Start != 0 || missing[0].End != 512 {
t.Errorf("Get(): incorrect missing Entry: [%d, %d] want [0, 512]", missing[0].Start, missing[0].End)
}
if missing[1].Start != 768 || missing[1].End != 768+64 {
t.Errorf("Get(): incorrect missing Entry: [%d, %d] want [768, 832]", missing[1].Start, missing[1].End)
}
for i := int(missing[0].End); len(buf) > i; i++ {
if i == int(missing[1].Start) {
i = int(missing[1].End)
}
if buf[i] != byte(i%256) {
t.Errorf("Put()+Get() returned incorrect data at buf offset %d", i)
break
}
}
}
func TestChaos(t *testing.T) {
bug = t.Errorf
files := [][]byte{
make([]byte, 1024*1024),
make([]byte, 2*1024*1024),
}
fileNames := []string{
"file1.txt",
"filetwo.txt",
}
tmp := t.TempDir()
cfg := &Config{
MaxGarbageRatio: 0.95,
LogFileSize: 4 * 1024 * 1024,
}
c, err := Open(tmp, cfg)
if err != nil {
t.Fatalf("Failed to initialize empty cache: %v", err)
}
for i := 0; 10000 > i; i++ {
if i%500 == 0 {
c = nil
runtime.GC() // Ensure os.File's get closed.
c, err = Open(tmp, cfg)
if err != nil {
t.Fatalf("Failed to open cache: %v", err)
}
}
f := rand.Intn(len(fileNames))
fn := fileNames[f]
fd := files[f]
switch rand.Intn(3) {
case 0:
o := rand.Intn(len(fd))
s := rand.Intn((len(fd) - o) / 4)
buf := make([]byte, s)
rand.Read(buf)
if err := c.Put([]byte(fn), int64(o), buf); err != nil {
t.Fatalf("Put() failed: %v", err)
}
copy(fd[o:], buf)
case 1:
o := rand.Intn(len(fd))
s := rand.Intn((len(fd) - o) / 4)
if err := c.Delete([]byte(fn), int64(o), s); err != nil {
t.Fatalf("Delete() failed: %v", err)
}
copy(fd[o:], make([]byte, s))
case 2:
o := rand.Intn(len(fd))
s := rand.Intn(len(fd) - o)
buf := make([]byte, s)
_, err := c.Get([]byte(fn), int64(o), buf)
if err != nil {
t.Fatalf("Get() failed: %v", err)
}
if !bytes.Equal(buf, fd[o:o+s]) {
for j := 0; len(buf) > j; j++ {
if buf[j] != fd[o+j] {
t.Fatalf("Get(%s, %d, %d) returned wrong data at offset %d: cache: %d; check: %d", fn, o, s, o+j, buf[j], fd[o+j])
}
}
t.Fatalf("Get(%s, %d, %d) returned wrong data", fn, o, s)
}
}
}
cmd := exec.Command("find", tmp, "-ls")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}