github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/resumer/resumer_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package resumer_test 5 6 import ( 7 stdtesting "testing" 8 "time" 9 10 gc "launchpad.net/gocheck" 11 12 "github.com/juju/juju/juju/testing" 13 coretesting "github.com/juju/juju/testing" 14 "github.com/juju/juju/worker/resumer" 15 ) 16 17 func TestPackage(t *stdtesting.T) { 18 coretesting.MgoTestPackage(t) 19 } 20 21 type ResumerSuite struct { 22 testing.JujuConnSuite 23 } 24 25 var _ = gc.Suite(&ResumerSuite{}) 26 27 func (s *ResumerSuite) TestRunStopWithState(c *gc.C) { 28 // Test with state ensures that state fulfills the 29 // TransactionResumer interface. 30 rr := resumer.NewResumer(s.State) 31 32 c.Assert(rr.Stop(), gc.IsNil) 33 } 34 35 func (s *ResumerSuite) TestResumerCalls(c *gc.C) { 36 // Shorter interval and mock help to count 37 // the resumer calls in a given timespan. 38 testInterval := 10 * time.Millisecond 39 resumer.SetInterval(testInterval) 40 defer resumer.RestoreInterval() 41 42 tr := &transactionResumerMock{[]time.Time{}} 43 rr := resumer.NewResumer(tr) 44 defer func() { c.Assert(rr.Stop(), gc.IsNil) }() 45 46 time.Sleep(10 * testInterval) 47 48 // Check that a numner of calls has happened with a time 49 // difference somewhere between the interval and twice the 50 // interval. A more precise time behavior cannot be 51 // specified due to the load during the test. 52 c.Assert(len(tr.timestamps) > 0, gc.Equals, true) 53 for i := 1; i < len(tr.timestamps); i++ { 54 diff := tr.timestamps[i].Sub(tr.timestamps[i-1]) 55 56 c.Assert(diff >= testInterval, gc.Equals, true) 57 c.Assert(diff <= 4*testInterval, gc.Equals, true) 58 } 59 } 60 61 // transactionResumerMock is used to check the 62 // calls of ResumeTransactions(). 63 type transactionResumerMock struct { 64 timestamps []time.Time 65 } 66 67 func (tr *transactionResumerMock) ResumeTransactions() error { 68 tr.timestamps = append(tr.timestamps, time.Now()) 69 return nil 70 }