forked from gofrs/flock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flock_test.go
300 lines (242 loc) · 6.88 KB
/
flock_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2015 Tim Heckman. All rights reserved.
// Copyright 2018 The Gofrs. All rights reserved.
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
package flock_test
import (
"context"
"io/ioutil"
"os"
"runtime"
"testing"
"time"
"github.com/gofrs/flock"
. "gopkg.in/check.v1"
)
type TestSuite struct {
path string
flock *flock.Flock
}
var _ = Suite(&TestSuite{})
func Test(t *testing.T) { TestingT(t) }
func (t *TestSuite) SetUpTest(c *C) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "go-flock-")
c.Assert(err, IsNil)
c.Assert(tmpFile, Not(IsNil))
t.path = tmpFile.Name()
defer os.Remove(t.path)
tmpFile.Close()
t.flock = flock.New(t.path)
}
func (t *TestSuite) TearDownTest(c *C) {
t.flock.Unlock()
os.Remove(t.path)
}
func (t *TestSuite) TestNew(c *C) {
var f *flock.Flock
f = flock.New(t.path)
c.Assert(f, Not(IsNil))
c.Check(f.Path(), Equals, t.path)
c.Check(f.Locked(), Equals, false)
c.Check(f.RLocked(), Equals, false)
}
func (t *TestSuite) TestFlock_Path(c *C) {
var path string
path = t.flock.Path()
c.Check(path, Equals, t.path)
}
func (t *TestSuite) TestFlock_Locked(c *C) {
var locked bool
locked = t.flock.Locked()
c.Check(locked, Equals, false)
}
func (t *TestSuite) TestFlock_RLocked(c *C) {
var locked bool
locked = t.flock.RLocked()
c.Check(locked, Equals, false)
}
func (t *TestSuite) TestFlock_String(c *C) {
var str string
str = t.flock.String()
c.Assert(str, Equals, t.path)
}
func (t *TestSuite) TestFlock_TryLock(c *C) {
c.Assert(t.flock.Locked(), Equals, false)
c.Assert(t.flock.RLocked(), Equals, false)
var locked bool
var err error
locked, err = t.flock.TryLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, true)
c.Check(t.flock.Locked(), Equals, true)
c.Check(t.flock.RLocked(), Equals, false)
locked, err = t.flock.TryLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, true)
// make sure we just return false with no error in cases
// where we would have been blocked
locked, err = flock.New(t.path).TryLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, false)
}
func (t *TestSuite) TestFlock_TryRLock(c *C) {
c.Assert(t.flock.Locked(), Equals, false)
c.Assert(t.flock.RLocked(), Equals, false)
var locked bool
var err error
locked, err = t.flock.TryRLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, true)
c.Check(t.flock.Locked(), Equals, false)
c.Check(t.flock.RLocked(), Equals, true)
locked, err = t.flock.TryRLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, true)
// shared lock should not block.
flock2 := flock.New(t.path)
locked, err = flock2.TryRLock()
c.Assert(err, IsNil)
if runtime.GOOS == "aix" {
// When using POSIX locks, we can't safely read-lock the same
// inode through two different descriptors at the same time:
// when the first descriptor is closed, the second descriptor
// would still be open but silently unlocked. So a second
// TryRLock must return false.
c.Check(locked, Equals, false)
} else {
c.Check(locked, Equals, true)
}
// make sure we just return false with no error in cases
// where we would have been blocked
t.flock.Unlock()
flock2.Unlock()
t.flock.Lock()
locked, err = flock.New(t.path).TryRLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, false)
}
func (t *TestSuite) TestFlock_TryLockContext(c *C) {
// happy path
ctx, cancel := context.WithCancel(context.Background())
locked, err := t.flock.TryLockContext(ctx, time.Second)
c.Assert(err, IsNil)
c.Check(locked, Equals, true)
// context already canceled
cancel()
locked, err = flock.New(t.path).TryLockContext(ctx, time.Second)
c.Assert(err, Equals, context.Canceled)
c.Check(locked, Equals, false)
// timeout
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
locked, err = flock.New(t.path).TryLockContext(ctx, time.Second)
c.Assert(err, Equals, context.DeadlineExceeded)
c.Check(locked, Equals, false)
}
func (t *TestSuite) TestFlock_TryRLockContext(c *C) {
// happy path
ctx, cancel := context.WithCancel(context.Background())
locked, err := t.flock.TryRLockContext(ctx, time.Second)
c.Assert(err, IsNil)
c.Check(locked, Equals, true)
// context already canceled
cancel()
locked, err = flock.New(t.path).TryRLockContext(ctx, time.Second)
c.Assert(err, Equals, context.Canceled)
c.Check(locked, Equals, false)
// timeout
t.flock.Unlock()
t.flock.Lock()
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
locked, err = flock.New(t.path).TryRLockContext(ctx, time.Second)
c.Assert(err, Equals, context.DeadlineExceeded)
c.Check(locked, Equals, false)
}
func (t *TestSuite) TestFlock_Unlock(c *C) {
var err error
err = t.flock.Unlock()
c.Assert(err, IsNil)
// get a lock for us to unlock
locked, err := t.flock.TryLock()
c.Assert(err, IsNil)
c.Assert(locked, Equals, true)
c.Assert(t.flock.Locked(), Equals, true)
c.Check(t.flock.RLocked(), Equals, false)
_, err = os.Stat(t.path)
c.Assert(os.IsNotExist(err), Equals, false)
err = t.flock.Unlock()
c.Assert(err, IsNil)
c.Check(t.flock.Locked(), Equals, false)
c.Check(t.flock.RLocked(), Equals, false)
}
func (t *TestSuite) TestFlock_Lock(c *C) {
c.Assert(t.flock.Locked(), Equals, false)
c.Check(t.flock.RLocked(), Equals, false)
var err error
err = t.flock.Lock()
c.Assert(err, IsNil)
c.Check(t.flock.Locked(), Equals, true)
c.Check(t.flock.RLocked(), Equals, false)
// test that the short-circuit works
err = t.flock.Lock()
c.Assert(err, IsNil)
//
// Test that Lock() is a blocking call
//
ch := make(chan error, 2)
gf := flock.New(t.path)
defer gf.Unlock()
go func(ch chan<- error) {
ch <- nil
ch <- gf.Lock()
close(ch)
}(ch)
errCh, ok := <-ch
c.Assert(ok, Equals, true)
c.Assert(errCh, IsNil)
err = t.flock.Unlock()
c.Assert(err, IsNil)
errCh, ok = <-ch
c.Assert(ok, Equals, true)
c.Assert(errCh, IsNil)
c.Check(t.flock.Locked(), Equals, false)
c.Check(t.flock.RLocked(), Equals, false)
c.Check(gf.Locked(), Equals, true)
c.Check(gf.RLocked(), Equals, false)
}
func (t *TestSuite) TestFlock_RLock(c *C) {
c.Assert(t.flock.Locked(), Equals, false)
c.Check(t.flock.RLocked(), Equals, false)
var err error
err = t.flock.RLock()
c.Assert(err, IsNil)
c.Check(t.flock.Locked(), Equals, false)
c.Check(t.flock.RLocked(), Equals, true)
// test that the short-circuit works
err = t.flock.RLock()
c.Assert(err, IsNil)
//
// Test that RLock() is a blocking call
//
ch := make(chan error, 2)
gf := flock.New(t.path)
defer gf.Unlock()
go func(ch chan<- error) {
ch <- nil
ch <- gf.RLock()
close(ch)
}(ch)
errCh, ok := <-ch
c.Assert(ok, Equals, true)
c.Assert(errCh, IsNil)
err = t.flock.Unlock()
c.Assert(err, IsNil)
errCh, ok = <-ch
c.Assert(ok, Equals, true)
c.Assert(errCh, IsNil)
c.Check(t.flock.Locked(), Equals, false)
c.Check(t.flock.RLocked(), Equals, false)
c.Check(gf.Locked(), Equals, false)
c.Check(gf.RLocked(), Equals, true)
}