forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
242 lines (209 loc) · 4.81 KB
/
error_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"fmt"
"io"
"os"
"sync/atomic"
"testing"
"github.com/petermattis/pebble/vfs"
)
type panicLogger struct{}
func (l panicLogger) Infof(format string, args ...interface{}) {
}
func (l panicLogger) Fatalf(format string, args ...interface{}) {
panic(fmt.Errorf("fatal: "+format, args...))
}
type errorFS struct {
fs vfs.FS
index int32
}
func newErrorFS(index int32) *errorFS {
return &errorFS{
fs: vfs.NewMem(),
index: index,
}
}
func (fs *errorFS) maybeError() error {
if atomic.AddInt32(&fs.index, -1) == -1 {
return fmt.Errorf("injected error")
}
return nil
}
func (fs *errorFS) Create(name string) (vfs.File, error) {
if err := fs.maybeError(); err != nil {
return nil, err
}
f, err := fs.fs.Create(name)
if err != nil {
return nil, err
}
return errorFile{f, fs}, nil
}
func (fs *errorFS) Link(oldname, newname string) error {
if err := fs.maybeError(); err != nil {
return err
}
return fs.fs.Link(oldname, newname)
}
func (fs *errorFS) Open(name string) (vfs.File, error) {
if err := fs.maybeError(); err != nil {
return nil, err
}
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
return errorFile{f, fs}, nil
}
func (fs *errorFS) OpenDir(name string) (vfs.File, error) {
if err := fs.maybeError(); err != nil {
return nil, err
}
f, err := fs.fs.OpenDir(name)
if err != nil {
return nil, err
}
return errorFile{f, fs}, nil
}
func (fs *errorFS) Remove(name string) error {
if _, err := fs.fs.Stat(name); os.IsNotExist(err) {
return nil
}
if err := fs.maybeError(); err != nil {
return err
}
return fs.fs.Remove(name)
}
func (fs *errorFS) Rename(oldname, newname string) error {
if err := fs.maybeError(); err != nil {
return err
}
return fs.fs.Rename(oldname, newname)
}
func (fs *errorFS) MkdirAll(dir string, perm os.FileMode) error {
if err := fs.maybeError(); err != nil {
return err
}
return fs.fs.MkdirAll(dir, perm)
}
func (fs *errorFS) Lock(name string) (io.Closer, error) {
if err := fs.maybeError(); err != nil {
return nil, err
}
return fs.fs.Lock(name)
}
func (fs *errorFS) List(dir string) ([]string, error) {
if err := fs.maybeError(); err != nil {
return nil, err
}
return fs.fs.List(dir)
}
func (fs *errorFS) Stat(name string) (os.FileInfo, error) {
if err := fs.maybeError(); err != nil {
return nil, err
}
return fs.fs.Stat(name)
}
type errorFile struct {
file vfs.File
fs *errorFS
}
func (f errorFile) Close() error {
// We don't inject errors during close as those calls should never fail in
// practice.
return f.file.Close()
}
func (f errorFile) Read(p []byte) (int, error) {
if err := f.fs.maybeError(); err != nil {
return 0, err
}
return f.file.Read(p)
}
func (f errorFile) ReadAt(p []byte, off int64) (int, error) {
if err := f.fs.maybeError(); err != nil {
return 0, err
}
return f.file.ReadAt(p, off)
}
func (f errorFile) Write(p []byte) (int, error) {
if err := f.fs.maybeError(); err != nil {
return 0, err
}
return f.file.Write(p)
}
func (f errorFile) Stat() (os.FileInfo, error) {
if err := f.fs.maybeError(); err != nil {
return nil, err
}
return f.file.Stat()
}
func (f errorFile) Sync() error {
if err := f.fs.maybeError(); err != nil {
return err
}
return f.file.Sync()
}
// TestErrors repeatedly runs a short sequence of operations, injecting FS
// errors at different points, until success is achieved.
func TestErrors(t *testing.T) {
run := func(fs *errorFS) (err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
err = e
} else {
err = fmt.Errorf("%v", r)
}
}
}()
d, err := Open("", &Options{
FS: fs,
Logger: panicLogger{},
})
if err != nil {
return err
}
key := []byte("a")
value := []byte("b")
if err := d.Set(key, value, nil); err != nil {
return err
}
if err := d.Flush(); err != nil {
return err
}
if err := d.Compact(nil, nil); err != nil {
return err
}
iter := d.NewIter(nil)
for valid := iter.First(); valid; valid = iter.Next() {
}
if err := iter.Close(); err != nil {
return err
}
return d.Close()
}
errorCounts := make(map[string]int)
for i := int32(0); ; i++ {
fs := newErrorFS(i)
err := run(fs)
if err == nil {
t.Logf("success %d\n", i)
break
}
errorCounts[err.Error()]++
}
expectedErrors := []string{
"fatal: MANIFEST flush failed: injected error",
"fatal: MANIFEST sync failed: injected error",
"fatal: MANIFEST set current failed: injected error",
"fatal: MANIFEST dirsync failed: injected error",
}
for _, expected := range expectedErrors {
if errorCounts[expected] == 0 {
t.Errorf("expected error %q did not occur", expected)
}
}
}