github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/singular/flag_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package singular_test 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/worker/singular" 15 "github.com/juju/juju/worker/workertest" 16 ) 17 18 type FlagSuite struct { 19 testing.IsolationSuite 20 } 21 22 var _ = gc.Suite(&FlagSuite{}) 23 24 func (s *FlagSuite) TestClaimError(c *gc.C) { 25 var stub testing.Stub 26 stub.SetErrors(errors.New("squish")) 27 28 worker, err := singular.NewFlagWorker(singular.FlagConfig{ 29 Facade: newStubFacade(&stub), 30 Clock: &fakeClock{}, 31 Duration: time.Hour, 32 }) 33 c.Check(worker, gc.IsNil) 34 c.Check(err, gc.ErrorMatches, "squish") 35 } 36 37 func (s *FlagSuite) TestClaimFailure(c *gc.C) { 38 fix := newFixture(c, errClaimDenied, nil) 39 fix.Run(c, func(flag *singular.FlagWorker, _ *testing.Clock, _ func()) { 40 c.Check(flag.Check(), jc.IsFalse) 41 workertest.CheckAlive(c, flag) 42 }) 43 fix.CheckClaimWait(c) 44 } 45 46 func (s *FlagSuite) TestClaimFailureWaitError(c *gc.C) { 47 fix := newFixture(c, errClaimDenied, errors.New("glug")) 48 fix.Run(c, func(flag *singular.FlagWorker, _ *testing.Clock, unblock func()) { 49 c.Check(flag.Check(), jc.IsFalse) 50 unblock() 51 err := workertest.CheckKilled(c, flag) 52 c.Check(err, gc.ErrorMatches, "glug") 53 }) 54 fix.CheckClaimWait(c) 55 } 56 57 func (s *FlagSuite) TestClaimFailureWaitSuccess(c *gc.C) { 58 fix := newFixture(c, errClaimDenied, nil) 59 fix.Run(c, func(flag *singular.FlagWorker, _ *testing.Clock, unblock func()) { 60 c.Check(flag.Check(), jc.IsFalse) 61 unblock() 62 err := workertest.CheckKilled(c, flag) 63 c.Check(errors.Cause(err), gc.Equals, singular.ErrRefresh) 64 }) 65 fix.CheckClaimWait(c) 66 } 67 68 func (s *FlagSuite) TestClaimSuccess(c *gc.C) { 69 fix := newFixture(c, nil, errors.New("should not happen")) 70 fix.Run(c, func(flag *singular.FlagWorker, clock *testing.Clock, unblock func()) { 71 <-clock.Alarms() 72 clock.Advance(29 * time.Second) 73 workertest.CheckAlive(c, flag) 74 }) 75 fix.CheckClaims(c, 1) 76 } 77 78 func (s *FlagSuite) TestClaimSuccessThenFailure(c *gc.C) { 79 fix := newFixture(c, nil, errClaimDenied) 80 fix.Run(c, func(flag *singular.FlagWorker, clock *testing.Clock, unblock func()) { 81 <-clock.Alarms() 82 clock.Advance(30 * time.Second) 83 err := workertest.CheckKilled(c, flag) 84 c.Check(errors.Cause(err), gc.Equals, singular.ErrRefresh) 85 }) 86 fix.CheckClaims(c, 2) 87 } 88 89 func (s *FlagSuite) TestClaimSuccessesThenError(c *gc.C) { 90 fix := newFixture(c) 91 fix.Run(c, func(flag *singular.FlagWorker, clock *testing.Clock, unblock func()) { 92 <-clock.Alarms() 93 clock.Advance(time.Minute) 94 <-clock.Alarms() 95 clock.Advance(time.Minute) 96 workertest.CheckAlive(c, flag) 97 }) 98 fix.CheckClaims(c, 3) 99 }