github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/txnpruner/txnpruner_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package txnpruner_test 5 6 import ( 7 "time" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/utils/clock" 12 gc "gopkg.in/check.v1" 13 14 coretesting "github.com/juju/juju/testing" 15 "github.com/juju/juju/worker/txnpruner" 16 ) 17 18 type TxnPrunerSuite struct { 19 coretesting.BaseSuite 20 } 21 22 var _ = gc.Suite(&TxnPrunerSuite{}) 23 24 func (s *TxnPrunerSuite) TestPrunes(c *gc.C) { 25 fakePruner := newFakeTransactionPruner() 26 testClock := testing.NewClock(time.Now()) 27 interval := time.Minute 28 p := txnpruner.New(fakePruner, interval, testClock) 29 defer p.Kill() 30 31 select { 32 case <-testClock.Alarms(): 33 case <-time.After(coretesting.LongWait): 34 c.Fatalf("timed out waiting for worker to stat") 35 } 36 c.Logf("pruner running and waiting: %s (%s)", testClock.Now(), time.Now()) 37 // Show that we prune every minute 38 for i := 0; i < 5; i++ { 39 testClock.Advance(interval) 40 c.Logf("loop %d: %s (%s)", i, testClock.Now(), time.Now()) 41 select { 42 case <-fakePruner.pruneCh: 43 case <-time.After(coretesting.LongWait): 44 c.Fatal("timed out waiting for pruning to happen") 45 } 46 // Now we need to wait for the txn pruner to call clock.After again 47 // before we advance the clock, or it will be waiting for the wrong time. 48 select { 49 case <-testClock.Alarms(): 50 case <-time.After(coretesting.LongWait): 51 c.Fatalf("timed out waiting for worker to loop around") 52 } 53 } 54 } 55 56 func (s *TxnPrunerSuite) TestStops(c *gc.C) { 57 success := make(chan bool) 58 check := func() { 59 p := txnpruner.New(newFakeTransactionPruner(), time.Minute, clock.WallClock) 60 p.Kill() 61 c.Check(p.Wait(), jc.ErrorIsNil) 62 success <- true 63 } 64 go check() 65 66 select { 67 case <-success: 68 case <-time.After(coretesting.LongWait): 69 c.Fatal("timed out waiting for worker to stop") 70 } 71 } 72 73 func newFakeTransactionPruner() *fakeTransactionPruner { 74 return &fakeTransactionPruner{ 75 pruneCh: make(chan bool), 76 } 77 } 78 79 type fakeTransactionPruner struct { 80 pruneCh chan bool 81 } 82 83 // MaybePruneTransactions implements the txnpruner.TransactionPruner 84 // interface. 85 func (p *fakeTransactionPruner) MaybePruneTransactions() error { 86 p.pruneCh <- true 87 return nil 88 }