~mterry/snappy/selftest-reboot-notice

« back to all changes in this revision

Viewing changes to priv/priv_test.go

  • Committer: Snappy Tarmac
  • Author(s): Michael Vogt
  • Date: 2015-06-16 10:32:05 UTC
  • mfrom: (510.1.3 snappy-with-decorator)
  • Revision ID: snappy_tarmac-20150616103205-czpa1fhc2cyr2c9p
Add priv.WithMutex() decorator to avoid duplicating code in cmd/snappy/cmd_*.go by mvo approved by chipaca

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
        "path/filepath"
26
26
        "testing"
 
27
        "time"
27
28
 
28
29
        . "gopkg.in/check.v1"
29
30
)
48
49
        }
49
50
}
50
51
 
51
 
func (ts *PrivTestSuite) TestFileLock(c *C) {
52
 
        lockfile := lockfileName()
53
 
 
54
 
        c.Assert(helpers.FileExists(lockfile), Equals, false)
55
 
 
56
 
        lock := NewFileLock(lockfile)
57
 
        c.Assert(lock, Not(IsNil))
58
 
        c.Assert(lock.Filename, Equals, lockfile)
59
 
        c.Assert(lock.realFile, IsNil)
60
 
 
61
 
        err := lock.Unlock()
62
 
        c.Assert(err, Equals, ErrNotLocked)
63
 
 
64
 
        // can only test non-blocking in a single process.
65
 
        err = lock.Lock(false)
66
 
        c.Assert(err, IsNil)
67
 
 
68
 
        c.Assert(helpers.FileExists(lockfile), Equals, true)
69
 
        c.Assert(lock.Filename, Equals, lockfile)
70
 
        c.Assert(lock.realFile, Not(IsNil))
71
 
 
72
 
        err = lock.Lock(false)
73
 
        c.Assert(err, Equals, ErrAlreadyLocked)
74
 
 
75
 
        err = lock.Unlock()
76
 
        c.Assert(err, IsNil)
77
 
 
78
 
        c.Assert(helpers.FileExists(lockfile), Equals, false)
79
 
        c.Assert(lock.Filename, Equals, "")
80
 
        c.Assert(lock.realFile, IsNil)
81
 
}
82
 
 
83
 
func (ts *PrivTestSuite) TestMutex(c *C) {
 
52
func (ts *PrivTestSuite) TestPrivMutex(c *C) {
84
53
        lockfile := lockfileName()
85
54
 
86
55
        c.Assert(helpers.FileExists(lockfile), Equals, false)
111
80
        c.Assert(helpers.FileExists(lockfile), Equals, false)
112
81
}
113
82
 
114
 
func (ts *PrivTestSuite) TestPriv(c *C) {
 
83
func (ts *PrivTestSuite) TestPrivMutexIsNotRoot(c *C) {
115
84
        lockfile := lockfileName()
116
85
 
117
86
        isRoot = func() bool {
127
96
        c.Assert(privMutex.TryLock(), DeepEquals, ErrNeedRoot)
128
97
        c.Assert(privMutex.Unlock(), DeepEquals, ErrNeedRoot)
129
98
}
 
99
 
 
100
func (ts *PrivTestSuite) TestWithPrivMutexSimple(c *C) {
 
101
        called := false
 
102
        err := WithMutex(func() error {
 
103
                called = true
 
104
                return nil
 
105
        })
 
106
        c.Assert(err, IsNil)
 
107
        c.Assert(called, Equals, true)
 
108
}
 
109
 
 
110
func (ts *PrivTestSuite) TestWithPrivMutexErrOnLockHeld(c *C) {
 
111
        var err, err1, err2 error
 
112
        var callCount int
 
113
 
 
114
        slowFunc := func() error {
 
115
                time.Sleep(time.Millisecond * 100)
 
116
                callCount++
 
117
                return nil
 
118
        }
 
119
 
 
120
        go func() {
 
121
                err1 = WithMutex(slowFunc)
 
122
        }()
 
123
        err2 = WithMutex(slowFunc)
 
124
 
 
125
        // find which err is set (depends on the order in which go
 
126
        // runs the goroutine)
 
127
        if err1 != nil {
 
128
                err = err1
 
129
        } else {
 
130
                err = err2
 
131
        }
 
132
 
 
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)
 
139
}