github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/gate/manifold_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package gate_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/worker"
    12  	"github.com/juju/juju/worker/dependency"
    13  	"github.com/juju/juju/worker/gate"
    14  )
    15  
    16  type ManifoldSuite struct {
    17  	testing.IsolationSuite
    18  	manifold dependency.Manifold
    19  	worker   worker.Worker
    20  }
    21  
    22  var _ = gc.Suite(&ManifoldSuite{})
    23  
    24  func (s *ManifoldSuite) SetUpTest(c *gc.C) {
    25  	s.IsolationSuite.SetUpTest(c)
    26  	s.manifold = gate.Manifold()
    27  	w, err := s.manifold.Start(nil)
    28  	c.Assert(err, jc.ErrorIsNil)
    29  	s.worker = w
    30  }
    31  
    32  func (s *ManifoldSuite) TearDownTest(c *gc.C) {
    33  	if s.worker != nil {
    34  		checkStop(c, s.worker)
    35  	}
    36  	s.IsolationSuite.TearDownTest(c)
    37  }
    38  
    39  func (s *ManifoldSuite) TestLocked(c *gc.C) {
    40  	w := waiter(c, s.manifold, s.worker)
    41  	assertLocked(c, w)
    42  }
    43  
    44  func (s *ManifoldSuite) TestUnlock(c *gc.C) {
    45  	u := unlocker(c, s.manifold, s.worker)
    46  	w := waiter(c, s.manifold, s.worker)
    47  
    48  	u.Unlock()
    49  	assertUnlocked(c, w)
    50  }
    51  
    52  func (s *ManifoldSuite) TestUnlockAgain(c *gc.C) {
    53  	u := unlocker(c, s.manifold, s.worker)
    54  	w := waiter(c, s.manifold, s.worker)
    55  
    56  	u.Unlock()
    57  	u.Unlock()
    58  	assertUnlocked(c, w)
    59  }
    60  
    61  func (s *ManifoldSuite) TestSameManifoldWorkersConnected(c *gc.C) {
    62  	worker2, err := s.manifold.Start(nil)
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	defer checkStop(c, worker2)
    65  
    66  	u := unlocker(c, s.manifold, s.worker)
    67  	w := waiter(c, s.manifold, worker2)
    68  
    69  	u.Unlock()
    70  	assertUnlocked(c, w)
    71  }
    72  
    73  func (s *ManifoldSuite) TestLockOutput(c *gc.C) {
    74  	var lock gate.Lock
    75  	err := s.manifold.Output(s.worker, &lock)
    76  	c.Assert(err, jc.ErrorIsNil)
    77  
    78  	w := waiter(c, s.manifold, s.worker)
    79  	assertLocked(c, w)
    80  	lock.Unlock()
    81  	assertUnlocked(c, w)
    82  }
    83  
    84  func (s *ManifoldSuite) TestDifferentManifoldWorkersUnconnected(c *gc.C) {
    85  	manifold2 := gate.Manifold()
    86  	worker2, err := manifold2.Start(nil)
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	defer checkStop(c, worker2)
    89  
    90  	u := unlocker(c, s.manifold, s.worker)
    91  	w := waiter(c, manifold2, worker2)
    92  
    93  	u.Unlock()
    94  	assertLocked(c, w)
    95  }
    96  
    97  func (s *ManifoldSuite) TestAlreadyUnlockedIsUnlocked(c *gc.C) {
    98  	w := gate.AlreadyUnlocked{}
    99  	assertUnlocked(c, w)
   100  }
   101  
   102  func (s *ManifoldSuite) TestManifoldEx(c *gc.C) {
   103  	lock := gate.NewLock()
   104  
   105  	manifold := gate.ManifoldEx(lock)
   106  	var waiter1 gate.Waiter = lock
   107  	var unlocker1 gate.Unlocker = lock
   108  
   109  	worker, err := manifold.Start(nil)
   110  	c.Assert(err, jc.ErrorIsNil)
   111  	defer checkStop(c, worker)
   112  	waiter2 := waiter(c, manifold, worker)
   113  
   114  	assertLocked(c, waiter1)
   115  	assertLocked(c, waiter2)
   116  
   117  	unlocker1.Unlock()
   118  	assertUnlocked(c, waiter1)
   119  	assertUnlocked(c, waiter2)
   120  }
   121  
   122  func unlocker(c *gc.C, m dependency.Manifold, w worker.Worker) gate.Unlocker {
   123  	var unlocker gate.Unlocker
   124  	err := m.Output(w, &unlocker)
   125  	c.Assert(err, jc.ErrorIsNil)
   126  	c.Assert(unlocker, gc.NotNil)
   127  	return unlocker
   128  }
   129  
   130  func waiter(c *gc.C, m dependency.Manifold, w worker.Worker) gate.Waiter {
   131  	var waiter gate.Waiter
   132  	err := m.Output(w, &waiter)
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	c.Assert(waiter, gc.NotNil)
   135  	return waiter
   136  }
   137  
   138  func assertLocked(c *gc.C, waiter gate.Waiter) {
   139  	c.Assert(waiter.IsUnlocked(), jc.IsFalse)
   140  	select {
   141  	case <-waiter.Unlocked():
   142  		c.Fatalf("expected gate to be locked")
   143  	default:
   144  	}
   145  }
   146  
   147  func assertUnlocked(c *gc.C, waiter gate.Waiter) {
   148  	c.Assert(waiter.IsUnlocked(), jc.IsTrue)
   149  	select {
   150  	case <-waiter.Unlocked():
   151  	default:
   152  		c.Fatalf("expected gate to be unlocked")
   153  	}
   154  }
   155  
   156  func checkStop(c *gc.C, w worker.Worker) {
   157  	err := worker.Stop(w)
   158  	c.Check(err, jc.ErrorIsNil)
   159  }