~rogpeppe/juju-core/323-machineagent-api-client

« back to all changes in this revision

Viewing changes to testing/locking_test.go

  • Committer: Roger Peppe
  • Date: 2013-06-21 09:29:15 UTC
  • mfrom: (1307.1.8 trunk)
  • Revision ID: roger.peppe@canonical.com-20130621092915-4fq4w2xaq68ns50s
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package testing
 
5
 
 
6
import (
 
7
        "errors"
 
8
        . "launchpad.net/gocheck"
 
9
        "sync"
 
10
)
 
11
 
 
12
type LockingSuite struct{}
 
13
 
 
14
var _ = Suite(&LockingSuite{})
 
15
 
 
16
func (LockingSuite) TestTestLockingFunctionPassesCorrectLock(c *C) {
 
17
        lock := sync.Mutex{}
 
18
        function := func() {
 
19
                lock.Lock()
 
20
                lock.Unlock()
 
21
        }
 
22
        // TestLockingFunction succeeds.
 
23
        TestLockingFunction(&lock, function)
 
24
}
 
25
 
 
26
func (LockingSuite) TestTestLockingFunctionDetectsDisobeyedLock(c *C) {
 
27
        lock := sync.Mutex{}
 
28
        function := func() {}
 
29
        c.Check(
 
30
                func() { TestLockingFunction(&lock, function) },
 
31
                Panics,
 
32
                errors.New("function did not obey lock"))
 
33
}
 
34
 
 
35
func (LockingSuite) TestTestLockingFunctionDetectsFailureToReleaseLock(c *C) {
 
36
        lock := sync.Mutex{}
 
37
        defer lock.Unlock()
 
38
        function := func() {
 
39
                lock.Lock()
 
40
        }
 
41
        c.Check(
 
42
                func() { TestLockingFunction(&lock, function) },
 
43
                Panics,
 
44
                errors.New("function did not release lock"))
 
45
}