51
func (ts *PrivTestSuite) TestFileLock(c *C) {
52
lockfile := lockfileName()
54
c.Assert(helpers.FileExists(lockfile), Equals, false)
56
lock := NewFileLock(lockfile)
57
c.Assert(lock, Not(IsNil))
58
c.Assert(lock.Filename, Equals, lockfile)
59
c.Assert(lock.realFile, IsNil)
62
c.Assert(err, Equals, ErrNotLocked)
64
// can only test non-blocking in a single process.
65
err = lock.Lock(false)
68
c.Assert(helpers.FileExists(lockfile), Equals, true)
69
c.Assert(lock.Filename, Equals, lockfile)
70
c.Assert(lock.realFile, Not(IsNil))
72
err = lock.Lock(false)
73
c.Assert(err, Equals, ErrAlreadyLocked)
78
c.Assert(helpers.FileExists(lockfile), Equals, false)
79
c.Assert(lock.Filename, Equals, "")
80
c.Assert(lock.realFile, IsNil)
83
func (ts *PrivTestSuite) TestMutex(c *C) {
52
func (ts *PrivTestSuite) TestPrivMutex(c *C) {
84
53
lockfile := lockfileName()
86
55
c.Assert(helpers.FileExists(lockfile), Equals, false)
111
80
c.Assert(helpers.FileExists(lockfile), Equals, false)
114
func (ts *PrivTestSuite) TestPriv(c *C) {
83
func (ts *PrivTestSuite) TestPrivMutexIsNotRoot(c *C) {
115
84
lockfile := lockfileName()
117
86
isRoot = func() bool {
127
96
c.Assert(privMutex.TryLock(), DeepEquals, ErrNeedRoot)
128
97
c.Assert(privMutex.Unlock(), DeepEquals, ErrNeedRoot)
100
func (ts *PrivTestSuite) TestWithPrivMutexSimple(c *C) {
102
err := WithMutex(func() error {
107
c.Assert(called, Equals, true)
110
func (ts *PrivTestSuite) TestWithPrivMutexErrOnLockHeld(c *C) {
111
var err, err1, err2 error
114
slowFunc := func() error {
115
time.Sleep(time.Millisecond * 100)
121
err1 = WithMutex(slowFunc)
123
err2 = WithMutex(slowFunc)
125
// find which err is set (depends on the order in which go
126
// runs the goroutine)
133
// only one functions errored
134
c.Assert(err1 != nil && err2 != nil, Equals, false)
135
// the other returned a proper error
136
c.Assert(err, Equals, ErrAlreadyLocked)
137
// and we did not call it too often
138
c.Assert(callCount, Equals, 1)