Debian Codegraph @

cache/internal/flock/flock_test.go

raw
1 // Copyright 2015 Tim Heckman. All rights reserved.
2 // Copyright 2018 The Gofrs. All rights reserved.
3 // Use of this source code is governed by the BSD 3-Clause
4 // license that can be found in the LICENSE file.
5
6 package flock
7
8 import (
9 "context"
10 "io/ioutil"
11 "os"
12 "runtime"
13 "testing"
14 "time"
15
16 . "gopkg.in/check.v1"
17 )
18
19 type TestSuite struct {
20 path string
21 flock *Flock
22 }
23
24 var _ = Suite(&TestSuite{})
25
26 func Test(t *testing.T) { TestingT(t) }
27
28 func (t *TestSuite) SetUpTest(c *C) {
29 tmpFile, err := ioutil.TempFile(os.TempDir(), "go-flock-")
30 c.Assert(err, IsNil)
31 c.Assert(tmpFile, Not(IsNil))
32
33 t.path = tmpFile.Name()
34
35 defer os.Remove(t.path)
36 tmpFile.Close()
37
38 t.flock = New(t.path)
39 }
40
41 func (t *TestSuite) TearDownTest(c *C) {
42 err := t.flock.Unlock()
43 c.Assert(err, IsNil)
44 os.Remove(t.path)
45 }
46
47 func (t *TestSuite) TestNew(c *C) {
48 f := New(t.path)
49 c.Assert(f, Not(IsNil))
50 c.Check(f.Path(), Equals, t.path)
51 c.Check(f.Locked(), Equals, false)
52 c.Check(f.RLocked(), Equals, false)
53 }
54
55 func (t *TestSuite) TestFlock_Path(c *C) {
56 path := t.flock.Path()
57 c.Check(path, Equals, t.path)
58 }
59
60 func (t *TestSuite) TestFlock_Locked(c *C) {
61 locked := t.flock.Locked()
62 c.Check(locked, Equals, false)
63 }
64
65 func (t *TestSuite) TestFlock_RLocked(c *C) {
66 locked := t.flock.RLocked()
67 c.Check(locked, Equals, false)
68 }
69
70 func (t *TestSuite) TestFlock_String(c *C) {
71 str := t.flock.String()
72 c.Assert(str, Equals, t.path)
73 }
74
75 func (t *TestSuite) TestFlock_TryLock(c *C) {
76 c.Assert(t.flock.Locked(), Equals, false)
77 c.Assert(t.flock.RLocked(), Equals, false)
78
79 var locked bool
80 var err error
81
82 locked, err = t.flock.TryLock()
83 c.Assert(err, IsNil)
84 c.Check(locked, Equals, true)
85 c.Check(t.flock.Locked(), Equals, true)
86 c.Check(t.flock.RLocked(), Equals, false)
87
88 locked, err = t.flock.TryLock()
89 c.Assert(err, IsNil)
90 c.Check(locked, Equals, true)
91
92 // make sure we just return false with no error in cases
93 // where we would have been blocked
94 locked, err = New(t.path).TryLock()
95 c.Assert(err, IsNil)
96 c.Check(locked, Equals, false)
97 }
98
99 func (t *TestSuite) TestFlock_TryRLock(c *C) {
100 c.Assert(t.flock.Locked(), Equals, false)
101 c.Assert(t.flock.RLocked(), Equals, false)
102
103 var locked bool
104 var err error
105
106 locked, err = t.flock.TryRLock()
107 c.Assert(err, IsNil)
108 c.Check(locked, Equals, true)
109 c.Check(t.flock.Locked(), Equals, false)
110 c.Check(t.flock.RLocked(), Equals, true)
111
112 locked, err = t.flock.TryRLock()
113 c.Assert(err, IsNil)
114 c.Check(locked, Equals, true)
115
116 // shared lock should not block.
117 flock2 := New(t.path)
118 locked, err = flock2.TryRLock()
119 c.Assert(err, IsNil)
120 if runtime.GOOS == "aix" {
121 // When using POSIX locks, we can't safely read-lock the same
122 // inode through two different descriptors at the same time:
123 // when the first descriptor is closed, the second descriptor
124 // would still be open but silently unlocked. So a second
125 // TryRLock must return false.
126 c.Check(locked, Equals, false)
127 } else {
128 c.Check(locked, Equals, true)
129 }
130
131 // make sure we just return false with no error in cases
132 // where we would have been blocked
133 err = t.flock.Unlock()
134 c.Assert(err, IsNil)
135 err = flock2.Unlock()
136 c.Assert(err, IsNil)
137 err = t.flock.Lock()
138 c.Assert(err, IsNil)
139 locked, err = New(t.path).TryRLock()
140 c.Assert(err, IsNil)
141 c.Check(locked, Equals, false)
142 }
143
144 func (t *TestSuite) TestFlock_TryLockContext(c *C) {
145 // happy path
146 ctx, cancel := context.WithCancel(context.Background())
147 locked, err := t.flock.TryLockContext(ctx, time.Second)
148 c.Assert(err, IsNil)
149 c.Check(locked, Equals, true)
150
151 // context already canceled
152 cancel()
153 locked, err = New(t.path).TryLockContext(ctx, time.Second)
154 c.Assert(err, Equals, context.Canceled)
155 c.Check(locked, Equals, false)
156
157 // timeout
158 ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
159 defer cancel()
160 locked, err = New(t.path).TryLockContext(ctx, time.Second)
161 c.Assert(err, Equals, context.DeadlineExceeded)
162 c.Check(locked, Equals, false)
163 }
164
165 func (t *TestSuite) TestFlock_TryRLockContext(c *C) {
166 // happy path
167 ctx, cancel := context.WithCancel(context.Background())
168 locked, err := t.flock.TryRLockContext(ctx, time.Second)
169 c.Assert(err, IsNil)
170 c.Check(locked, Equals, true)
171
172 // context already canceled
173 cancel()
174 locked, err = New(t.path).TryRLockContext(ctx, time.Second)
175 c.Assert(err, Equals, context.Canceled)
176 c.Check(locked, Equals, false)
177
178 // timeout
179 err = t.flock.Unlock()
180 c.Assert(err, IsNil)
181 err = t.flock.Lock()
182 c.Assert(err, IsNil)
183 ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
184 defer cancel()
185 locked, err = New(t.path).TryRLockContext(ctx, time.Second)
186 c.Assert(err, Equals, context.DeadlineExceeded)
187 c.Check(locked, Equals, false)
188 }
189
190 func (t *TestSuite) TestFlock_Unlock(c *C) {
191 var err error
192
193 err = t.flock.Unlock()
194 c.Assert(err, IsNil)
195
196 // get a lock for us to unlock
197 locked, err := t.flock.TryLock()
198 c.Assert(err, IsNil)
199 c.Assert(locked, Equals, true)
200 c.Assert(t.flock.Locked(), Equals, true)
201 c.Check(t.flock.RLocked(), Equals, false)
202
203 _, err = os.Stat(t.path)
204 c.Assert(os.IsNotExist(err), Equals, false)
205
206 err = t.flock.Unlock()
207 c.Assert(err, IsNil)
208 c.Check(t.flock.Locked(), Equals, false)
209 c.Check(t.flock.RLocked(), Equals, false)
210 }
211
212 func (t *TestSuite) TestFlock_Lock(c *C) {
213 c.Assert(t.flock.Locked(), Equals, false)
214 c.Check(t.flock.RLocked(), Equals, false)
215
216 var err error
217
218 err = t.flock.Lock()
219 c.Assert(err, IsNil)
220 c.Check(t.flock.Locked(), Equals, true)
221 c.Check(t.flock.RLocked(), Equals, false)
222
223 // test that the short-circuit works
224 err = t.flock.Lock()
225 c.Assert(err, IsNil)
226
227 //
228 // Test that Lock() is a blocking call
229 //
230 ch := make(chan error, 2)
231 gf := New(t.path)
232 defer func() {
233 err := gf.Unlock()
234 c.Assert(err, IsNil)
235 }()
236
237 go func(ch chan<- error) {
238 ch <- nil
239 ch <- gf.Lock()
240 close(ch)
241 }(ch)
242
243 errCh, ok := <-ch
244 c.Assert(ok, Equals, true)
245 c.Assert(errCh, IsNil)
246
247 err = t.flock.Unlock()
248 c.Assert(err, IsNil)
249
250 errCh, ok = <-ch
251 c.Assert(ok, Equals, true)
252 c.Assert(errCh, IsNil)
253 c.Check(t.flock.Locked(), Equals, false)
254 c.Check(t.flock.RLocked(), Equals, false)
255 c.Check(gf.Locked(), Equals, true)
256 c.Check(gf.RLocked(), Equals, false)
257 }
258
259 func (t *TestSuite) TestFlock_RLock(c *C) {
260 c.Assert(t.flock.Locked(), Equals, false)
261 c.Check(t.flock.RLocked(), Equals, false)
262
263 var err error
264
265 err = t.flock.RLock()
266 c.Assert(err, IsNil)
267 c.Check(t.flock.Locked(), Equals, false)
268 c.Check(t.flock.RLocked(), Equals, true)
269
270 // test that the short-circuit works
271 err = t.flock.RLock()
272 c.Assert(err, IsNil)
273
274 //
275 // Test that RLock() is a blocking call
276 //
277 ch := make(chan error, 2)
278 gf := New(t.path)
279 defer func() {
280 err := gf.Unlock()
281 c.Assert(err, IsNil)
282 }()
283
284 go func(ch chan<- error) {
285 ch <- nil
286 ch <- gf.RLock()
287 close(ch)
288 }(ch)
289
290 errCh, ok := <-ch
291 c.Assert(ok, Equals, true)
292 c.Assert(errCh, IsNil)
293
294 err = t.flock.Unlock()
295 c.Assert(err, IsNil)
296
297 errCh, ok = <-ch
298 c.Assert(ok, Equals, true)
299 c.Assert(errCh, IsNil)
300 c.Check(t.flock.Locked(), Equals, false)
301 c.Check(t.flock.RLocked(), Equals, false)
302 c.Check(gf.Locked(), Equals, false)
303 c.Check(gf.RLocked(), Equals, true)
304 }