forked from gofrs/flock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flock_test.go
151 lines (115 loc) · 2.95 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
// Copyright 2015 Tim Heckman. 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 (
"io/ioutil"
"os"
"testing"
"github.com/theckman/go-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.NewFlock(t.path)
}
func (t *TestSuite) TearDownTest(c *C) {
t.flock.Unlock()
os.Remove(t.path)
}
func (t *TestSuite) TestNewFlock(c *C) {
var f *flock.Flock
f = flock.NewFlock(t.path)
c.Assert(f, Not(IsNil))
c.Check(f.Path(), Equals, t.path)
c.Check(f.Locked(), 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_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)
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)
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.NewFlock(t.path).TryLock()
c.Assert(err, IsNil)
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)
_, 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)
}
func (t *TestSuite) TestFlock_Lock(c *C) {
c.Assert(t.flock.Locked(), Equals, false)
var err error
err = t.flock.Lock()
c.Assert(err, IsNil)
c.Check(t.flock.Locked(), Equals, true)
// 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.NewFlock(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(gf.Locked(), Equals, true)
}