~ubuntu-branches/ubuntu/vivid/juju-core/vivid-updates

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/txnpruner/txnpruner_test.go

  • Committer: Package Import Robot
  • Author(s): Curtis C. Hovey
  • Date: 2015-09-29 19:43:29 UTC
  • mfrom: (47.1.4 wily-proposed)
  • Revision ID: package-import@ubuntu.com-20150929194329-9y496tbic30hc7vp
Tags: 1.24.6-0ubuntu1~15.04.1
Backport of 1.24.6 from wily. (LP: #1500916, #1497087)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        jc "github.com/juju/testing/checkers"
 
10
        gc "gopkg.in/check.v1"
 
11
 
 
12
        "github.com/juju/juju/testing"
 
13
        "github.com/juju/juju/worker/txnpruner"
 
14
)
 
15
 
 
16
type TxnPrunerSuite struct {
 
17
        testing.BaseSuite
 
18
}
 
19
 
 
20
var _ = gc.Suite(&TxnPrunerSuite{})
 
21
 
 
22
func (s *TxnPrunerSuite) TestPrunes(c *gc.C) {
 
23
        fakePruner := newFakeTransactionPruner()
 
24
        interval := 10 * time.Millisecond
 
25
        p := txnpruner.New(fakePruner, interval)
 
26
        defer p.Kill()
 
27
 
 
28
        var t0 time.Time
 
29
        for i := 0; i < 5; i++ {
 
30
                select {
 
31
                case <-fakePruner.pruneCh:
 
32
                        t1 := time.Now()
 
33
                        if i > 0 {
 
34
                                // Check that pruning runs at the expected interval
 
35
                                // (but not the first time around as we don't know
 
36
                                // when the worker actually started).
 
37
                                td := t1.Sub(t0)
 
38
                                c.Assert(td >= interval, jc.IsTrue, gc.Commentf("td=%s", td))
 
39
                        }
 
40
                        t0 = t1
 
41
                case <-time.After(testing.LongWait):
 
42
                        c.Fatal("timed out waiting for pruning to happen")
 
43
                }
 
44
        }
 
45
}
 
46
 
 
47
func (s *TxnPrunerSuite) TestStops(c *gc.C) {
 
48
        success := make(chan bool)
 
49
        check := func() {
 
50
                p := txnpruner.New(newFakeTransactionPruner(), time.Minute)
 
51
                p.Kill()
 
52
                c.Assert(p.Wait(), jc.ErrorIsNil)
 
53
                success <- true
 
54
        }
 
55
        go check()
 
56
 
 
57
        select {
 
58
        case <-success:
 
59
        case <-time.After(testing.LongWait):
 
60
                c.Fatal("timed out waiting for worker to stop")
 
61
        }
 
62
}
 
63
 
 
64
func newFakeTransactionPruner() *fakeTransactionPruner {
 
65
        return &fakeTransactionPruner{
 
66
                pruneCh: make(chan bool),
 
67
        }
 
68
}
 
69
 
 
70
type fakeTransactionPruner struct {
 
71
        pruneCh chan bool
 
72
}
 
73
 
 
74
// MaybePruneTransactions implements the txnpruner.TransactionPruner
 
75
// interface.
 
76
func (p *fakeTransactionPruner) MaybePruneTransactions() error {
 
77
        p.pruneCh <- true
 
78
        return nil
 
79
}