github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/testing/locking_test.go (about)

     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  	"sync"
     9  
    10  	gc "gopkg.in/check.v1"
    11  )
    12  
    13  type LockingSuite struct{}
    14  
    15  var _ = gc.Suite(&LockingSuite{})
    16  
    17  func (LockingSuite) TestTestLockingFunctionPassesCorrectLock(c *gc.C) {
    18  	lock := sync.Mutex{}
    19  	function := func() {
    20  		lock.Lock()
    21  		lock.Unlock()
    22  	}
    23  	// TestLockingFunction succeeds.
    24  	TestLockingFunction(&lock, function)
    25  }
    26  
    27  func (LockingSuite) TestTestLockingFunctionDetectsDisobeyedLock(c *gc.C) {
    28  	lock := sync.Mutex{}
    29  	function := func() {}
    30  	c.Check(
    31  		func() { TestLockingFunction(&lock, function) },
    32  		gc.Panics,
    33  		errors.New("function did not obey lock"))
    34  }
    35  
    36  func (LockingSuite) TestTestLockingFunctionDetectsFailureToReleaseLock(c *gc.C) {
    37  	lock := sync.Mutex{}
    38  	defer lock.Unlock()
    39  	function := func() {
    40  		lock.Lock()
    41  	}
    42  	c.Check(
    43  		func() { TestLockingFunction(&lock, function) },
    44  		gc.Panics,
    45  		errors.New("function did not release lock"))
    46  }