~mterry/snappy/selftest-reboot-notice

« back to all changes in this revision

Viewing changes to priv/filelock_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:
 
1
// -*- Mode: Go; indent-tabs-mode: t -*-
 
2
 
 
3
/*
 
4
 * Copyright (C) 2014-2015 Canonical Ltd
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 3 as
 
8
 * published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 */
 
19
 
 
20
package priv
 
21
 
 
22
import (
 
23
        "path/filepath"
 
24
 
 
25
        "launchpad.net/snappy/helpers"
 
26
 
 
27
        . "gopkg.in/check.v1"
 
28
)
 
29
 
 
30
type FileLockTestSuite struct {
 
31
}
 
32
 
 
33
var _ = Suite(&FileLockTestSuite{})
 
34
 
 
35
func (ts *FileLockTestSuite) TestFileLock(c *C) {
 
36
        lockfile := filepath.Join(c.MkDir(), "lock")
 
37
 
 
38
        c.Assert(helpers.FileExists(lockfile), Equals, false)
 
39
 
 
40
        lock := NewFileLock(lockfile)
 
41
        c.Assert(lock, Not(IsNil))
 
42
        c.Assert(lock.Filename, Equals, lockfile)
 
43
        c.Assert(lock.realFile, IsNil)
 
44
 
 
45
        err := lock.Unlock()
 
46
        c.Assert(err, Equals, ErrNotLocked)
 
47
 
 
48
        // can only test non-blocking in a single process.
 
49
        err = lock.Lock(false)
 
50
        c.Assert(err, IsNil)
 
51
 
 
52
        c.Assert(helpers.FileExists(lockfile), Equals, true)
 
53
        c.Assert(lock.Filename, Equals, lockfile)
 
54
        c.Assert(lock.realFile, Not(IsNil))
 
55
 
 
56
        err = lock.Lock(false)
 
57
        c.Assert(err, Equals, ErrAlreadyLocked)
 
58
 
 
59
        err = lock.Unlock()
 
60
        c.Assert(err, IsNil)
 
61
 
 
62
        c.Assert(helpers.FileExists(lockfile), Equals, false)
 
63
        c.Assert(lock.Filename, Equals, "")
 
64
        c.Assert(lock.realFile, IsNil)
 
65
}