-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_test.go
144 lines (126 loc) · 3.41 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
package file
import (
"context"
"os"
"runtime"
"testing"
"time"
)
func TestOpen(t *testing.T) {
var err error
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
retryDelay := 50 * time.Millisecond
notexistpath := GetTestFilePath("notexist.txt")
_, err = OpenToRead(notexistpath)
if err == nil {
t.Fatal("no error, want IOError")
}
if _, ok := err.(*IOError); !ok {
t.Fatal("error is not a IOError")
}
_, err = OpenToUpdate(notexistpath)
if err == nil {
t.Fatal("no error, want IOError")
}
if _, ok := err.(*IOError); !ok {
t.Fatal("error is not a IOError")
}
_, err = OpenToReadContext(ctx, retryDelay, notexistpath)
if err == nil {
t.Fatal("no error, want IOError")
}
if _, ok := err.(*IOError); !ok {
t.Fatal("error is not a IOError")
}
_, err = OpenToReadContext(ctx, retryDelay, notexistpath)
if err == nil {
t.Fatal("no error, want IOError")
}
if _, ok := err.(*IOError); !ok {
t.Fatal("error is not a IOError")
}
shpath := GetTestFilePath("lock_sh.txt")
expath := GetTestFilePath("lock_ex.txt")
shfp, _ := os.OpenFile(shpath, os.O_CREATE, 0600)
_ = shfp.Close()
exfp, _ := os.OpenFile(expath, os.O_CREATE, 0600)
_ = exfp.Close()
shfp1, err := OpenToRead(shpath)
defer func() { _ = Close(shfp1) }()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
exfp1, err := OpenToUpdate(expath)
defer func() { _ = Close(exfp1) }()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
shfp2, err := OpenToReadContext(ctx, retryDelay, shpath)
defer func() { _ = Close(shfp2) }()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
switch runtime.GOOS {
case "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "windows":
exfp2, err := OpenToUpdateContext(ctx, retryDelay, expath)
defer func() { _ = Close(exfp2) }()
if err == nil {
t.Fatal("no error, want error for duplicate exclusive lock")
}
if _, ok := err.(*TimeoutError); !ok {
t.Fatal("error is not a TimeoutError")
}
exfp3, err := TryOpenToRead(expath)
defer func() { _ = Close(exfp3) }()
if err == nil {
t.Fatal("no error, want error for duplicate exclusive lock")
}
if _, ok := err.(*LockError); !ok {
t.Fatal("error is not a LockError")
}
exfp4, err := TryOpenToUpdate(expath)
defer func() { _ = Close(exfp4) }()
if err == nil {
t.Fatal("no error, want error for duplicate exclusive lock")
}
if _, ok := err.(*LockError); !ok {
t.Fatal("error is not a LockError")
}
case "solaris":
// maybe write later
}
err = RLock(nil)
if err == nil {
t.Fatal("no error, want error for invalid file descriptor")
}
if _, ok := err.(*LockError); !ok {
t.Fatal("error is not a LockError")
}
cancel()
exfp5, err := OpenToUpdateContext(ctx, retryDelay, expath)
defer func() { _ = Close(exfp5) }()
if err == nil {
t.Fatal("no error, want error for duplicate exclusive lock")
}
if _, ok := err.(*ContextDone); !ok {
t.Fatal("error is not a ContextDone")
}
}
func TestClose(t *testing.T) {
path := GetTestFilePath("closetest.txt")
fp, _ := Create(path)
err := fp.Close()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
switch runtime.GOOS {
case "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris", "windows":
err = Close(fp)
if err == nil {
t.Fatal("no error, want error for invalid file descriptor")
}
if _, ok := err.(*LockError); !ok {
t.Fatal("error is not a LockError")
}
}
}