-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_test.go
172 lines (121 loc) · 3.32 KB
/
file_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
package safeio
import (
"errors"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestOpenFile_ok(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "safeio")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
name := filepath.Join(tmpDir, "test1.txt")
mode := os.FileMode(0644)
f, err := OpenFile(name, mode)
require.NoError(t, err)
defer f.Close() // to prevent leaks
data := []byte("hello world\n")
n, err := f.Write(data)
require.NoError(t, err)
require.Equal(t, 12, n)
err = f.Commit()
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
// verify
read, err := os.ReadFile(name)
require.NoError(t, err)
require.Equal(t, data, read)
fi, err := os.Stat(name)
require.NoError(t, err)
require.Equal(t, mode, fi.Mode().Perm())
// this was the only file
list, err := os.ReadDir(tmpDir)
require.NoError(t, err)
require.Equal(t, 1, len(list))
}
func TestOpenFile_elective_abort(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "safeio")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
name := filepath.Join(tmpDir, "test1.txt")
mode := os.FileMode(0644)
f, err := OpenFile(name, mode)
require.NoError(t, err)
defer f.Close() // to prevent leaks
data := []byte("hello world\n")
n, err := f.Write(data)
require.NoError(t, err)
require.Equal(t, 12, n)
// no commit
err = f.Close()
require.NoError(t, err)
// verify
_, err = os.ReadFile(name)
require.True(t, os.IsNotExist(err))
// no files
list, err := os.ReadDir(tmpDir)
require.NoError(t, err)
require.Equal(t, 0, len(list))
}
var testErrDiskBroke = errors.New("disk broke!")
func TestOpenFile_writeErrorOnCommit_abort(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "safeio")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
name := filepath.Join(tmpDir, "test1.txt")
mode := os.FileMode(0644)
f, err := OpenFile(name, mode)
require.NoError(t, err)
defer f.Close() // to prevent leaks
data := []byte("hello world\n")
n, err := f.Write(data)
require.NoError(t, err)
require.Equal(t, 12, n)
// simulate disk wonkiness
f.setErr(testErrDiskBroke)
err = f.Commit()
require.Equal(t, testErrDiskBroke, err)
err = f.Close()
require.Equal(t, testErrDiskBroke, err)
// verify
_, err = os.ReadFile(name)
require.True(t, os.IsNotExist(err))
// no files
list, err := os.ReadDir(tmpDir)
require.NoError(t, err)
require.Equal(t, 0, len(list))
}
func TestOpenFile_writeErrorOnWrite_abort(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "safeio")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
name := filepath.Join(tmpDir, "test1.txt")
mode := os.FileMode(0644)
f, err := OpenFile(name, mode)
require.NoError(t, err)
defer f.Close() // to prevent leaks
data := []byte("hello world\n")
// write half before the error
n, err := f.Write(data[0:6])
require.NoError(t, err)
require.Equal(t, 6, n)
// simulate disk wonkiness
f.setErr(testErrDiskBroke)
// and try to write half after the error
n, err = f.Write(data[6:])
require.Equal(t, testErrDiskBroke, err)
require.Equal(t, 0, n)
err = f.Commit()
require.Equal(t, testErrDiskBroke, err)
err = f.Close()
require.Equal(t, testErrDiskBroke, err)
// verify
_, err = os.ReadFile(name)
require.True(t, os.IsNotExist(err))
// no files
list, err := os.ReadDir(tmpDir)
require.NoError(t, err)
require.Equal(t, 0, len(list))
}