launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/watcher/helpers_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package watcher_test
     5  
     6  import (
     7  	"launchpad.net/errgo/errors"
     8  
     9  	gc "launchpad.net/gocheck"
    10  	"launchpad.net/tomb"
    11  
    12  	"launchpad.net/juju-core/state/watcher"
    13  )
    14  
    15  type dummyWatcher struct {
    16  	err error
    17  }
    18  
    19  func (w *dummyWatcher) Stop() error {
    20  	return w.err
    21  }
    22  
    23  func (w *dummyWatcher) Err() error {
    24  	return w.err
    25  }
    26  
    27  func (s *FastPeriodSuite) TestStop(c *gc.C) {
    28  	t := &tomb.Tomb{}
    29  	watcher.Stop(&dummyWatcher{nil}, t)
    30  	c.Assert(t.Err(), gc.Equals, tomb.ErrStillAlive)
    31  
    32  	watcher.Stop(&dummyWatcher{errors.New("BLAM")}, t)
    33  	c.Assert(t.Err(), gc.ErrorMatches, "BLAM")
    34  }
    35  
    36  func (s *FastPeriodSuite) TestMustErr(c *gc.C) {
    37  	err := watcher.MustErr(&dummyWatcher{errors.New("POW")})
    38  	c.Assert(err, gc.ErrorMatches, "POW")
    39  
    40  	stillAlive := func() { watcher.MustErr(&dummyWatcher{tomb.ErrStillAlive}) }
    41  	c.Assert(stillAlive, gc.PanicMatches, "watcher is still running")
    42  
    43  	noErr := func() { watcher.MustErr(&dummyWatcher{nil}) }
    44  	c.Assert(noErr, gc.PanicMatches, "watcher was stopped cleanly")
    45  }