github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/timer_test.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package uniter_test 5 6 import ( 7 "time" 8 9 gc "gopkg.in/check.v1" 10 11 coretesting "github.com/juju/juju/testing" 12 "github.com/juju/juju/worker/uniter" 13 ) 14 15 type TimerSuite struct{} 16 17 var _ = gc.Suite(&TimerSuite{}) 18 19 func (s *TimerSuite) TestCollectMetricsTimer(c *gc.C) { 20 s.testTimer(c, uniter.ActiveCollectMetricsSignal) 21 } 22 23 func (s *TimerSuite) TestUpdateStatusTimer(c *gc.C) { 24 s.testTimer(c, uniter.UpdateStatusSignal) 25 } 26 27 func (*TimerSuite) testTimer(c *gc.C, s uniter.TimedSignal) { 28 now := time.Now() 29 defaultInterval := coretesting.ShortWait / 5 30 testCases := []struct { 31 about string 32 now time.Time 33 lastRun time.Time 34 interval time.Duration 35 expectSignal bool 36 }{{ 37 "Timer firing after delay.", 38 now, 39 now.Add(-defaultInterval / 2), 40 defaultInterval, 41 true, 42 }, { 43 "Timer firing the first time.", 44 now, 45 time.Unix(0, 0), 46 defaultInterval, 47 true, 48 }, { 49 "Timer not firing soon.", 50 now, 51 now, 52 coretesting.ShortWait * 2, 53 false, 54 }} 55 56 for i, t := range testCases { 57 c.Logf("running test %d", i) 58 sig := s(t.now, t.lastRun, t.interval) 59 select { 60 case <-sig: 61 if !t.expectSignal { 62 c.Errorf("not expecting a signal") 63 } 64 case <-time.After(coretesting.ShortWait): 65 if t.expectSignal { 66 c.Errorf("expected a signal") 67 } 68 } 69 } 70 }