@@ -21,13 +21,34 @@ import (
21
21
type TestSuite struct {
22
22
suite.Suite
23
23
24
+ dir bool
25
+ opts []flock.Option
26
+
24
27
path string
25
28
flock * flock.Flock
26
29
}
27
30
28
- func Test (t * testing.T ) { suite .Run (t , & TestSuite {}) }
31
+ func Test (t * testing.T ) {
32
+ suite .Run (t , & TestSuite {})
33
+ }
34
+
35
+ func Test_dir (t * testing.T ) {
36
+ if runtime .GOOS == "windows" {
37
+ t .Skip ("not supported on Windows" )
38
+ }
39
+
40
+ suite .Run (t , & TestSuite {dir : true , opts : []flock.Option {flock .SetFlag (os .O_RDONLY )}})
41
+ }
29
42
30
43
func (s * TestSuite ) SetupTest () {
44
+ if s .dir {
45
+ s .path = s .T ().TempDir ()
46
+
47
+ s .flock = flock .New (s .path , s .opts ... )
48
+
49
+ return
50
+ }
51
+
31
52
tmpFile , err := os .CreateTemp (s .T ().TempDir (), "go-flock-" )
32
53
s .Require ().NoError (err )
33
54
@@ -41,7 +62,7 @@ func (s *TestSuite) SetupTest() {
41
62
err = os .Remove (s .path )
42
63
s .Require ().NoError (err )
43
64
44
- s .flock = flock .New (s .path )
65
+ s .flock = flock .New (s .path , s . opts ... )
45
66
}
46
67
47
68
func (s * TestSuite ) TearDownTest () {
@@ -50,7 +71,7 @@ func (s *TestSuite) TearDownTest() {
50
71
}
51
72
52
73
func (s * TestSuite ) TestNew () {
53
- f := flock .New (s .path )
74
+ f := flock .New (s .path , s . opts ... )
54
75
s .Require ().NotNil (f )
55
76
56
77
s .Equal (f .Path (), s .path )
@@ -90,7 +111,7 @@ func (s *TestSuite) TestFlock_TryLock() {
90
111
91
112
// make sure we just return false with no error in cases
92
113
// where we would have been blocked
93
- locked , err = flock .New (s .path ).TryLock ()
114
+ locked , err = flock .New (s .path , s . opts ... ).TryLock ()
94
115
s .Require ().NoError (err )
95
116
s .False (locked )
96
117
}
@@ -110,7 +131,7 @@ func (s *TestSuite) TestFlock_TryRLock() {
110
131
s .True (locked )
111
132
112
133
// shared lock should not block.
113
- flock2 := flock .New (s .path )
134
+ flock2 := flock .New (s .path , s . opts ... )
114
135
locked , err = flock2 .TryRLock ()
115
136
s .Require ().NoError (err )
116
137
@@ -131,7 +152,7 @@ func (s *TestSuite) TestFlock_TryRLock() {
131
152
_ = s .flock .Unlock ()
132
153
_ = flock2 .Unlock ()
133
154
_ = s .flock .Lock ()
134
- locked , err = flock .New (s .path ).TryRLock ()
155
+ locked , err = flock .New (s .path , s . opts ... ).TryRLock ()
135
156
s .Require ().NoError (err )
136
157
s .False (locked )
137
158
}
@@ -147,15 +168,15 @@ func (s *TestSuite) TestFlock_TryLockContext() {
147
168
// context already canceled
148
169
cancel ()
149
170
150
- locked , err = flock .New (s .path ).TryLockContext (ctx , time .Second )
171
+ locked , err = flock .New (s .path , s . opts ... ).TryLockContext (ctx , time .Second )
151
172
s .Require ().ErrorIs (err , context .Canceled )
152
173
s .False (locked )
153
174
154
175
// timeout
155
176
ctx , cancel = context .WithTimeout (context .Background (), 10 * time .Millisecond )
156
177
defer cancel ()
157
178
158
- locked , err = flock .New (s .path ).TryLockContext (ctx , time .Second )
179
+ locked , err = flock .New (s .path , s . opts ... ).TryLockContext (ctx , time .Second )
159
180
s .Require ().ErrorIs (err , context .DeadlineExceeded )
160
181
s .False (locked )
161
182
}
@@ -171,7 +192,7 @@ func (s *TestSuite) TestFlock_TryRLockContext() {
171
192
// context already canceled
172
193
cancel ()
173
194
174
- locked , err = flock .New (s .path ).TryRLockContext (ctx , time .Second )
195
+ locked , err = flock .New (s .path , s . opts ... ).TryRLockContext (ctx , time .Second )
175
196
s .Require ().ErrorIs (err , context .Canceled )
176
197
s .False (locked )
177
198
@@ -182,7 +203,7 @@ func (s *TestSuite) TestFlock_TryRLockContext() {
182
203
ctx , cancel = context .WithTimeout (context .Background (), 10 * time .Millisecond )
183
204
defer cancel ()
184
205
185
- locked , err = flock .New (s .path ).TryRLockContext (ctx , time .Second )
206
+ locked , err = flock .New (s .path , s . opts ... ).TryRLockContext (ctx , time .Second )
186
207
s .Require ().ErrorIs (err , context .DeadlineExceeded )
187
208
s .False (locked )
188
209
}
@@ -224,7 +245,7 @@ func (s *TestSuite) TestFlock_Lock() {
224
245
// Test that Lock() is a blocking call
225
246
//
226
247
ch := make (chan error , 2 )
227
- gf := flock .New (s .path )
248
+ gf := flock .New (s .path , s . opts ... )
228
249
defer func () { _ = gf .Unlock () }()
229
250
230
251
go func (ch chan <- error ) {
@@ -266,7 +287,7 @@ func (s *TestSuite) TestFlock_RLock() {
266
287
// Test that RLock() is a blocking call
267
288
//
268
289
ch := make (chan error , 2 )
269
- gf := flock .New (s .path )
290
+ gf := flock .New (s .path , s . opts ... )
270
291
defer func () { _ = gf .Unlock () }()
271
292
272
293
go func (ch chan <- error ) {
0 commit comments