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