1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package charmrevision_test
9
"github.com/juju/errors"
10
"github.com/juju/testing"
11
jc "github.com/juju/testing/checkers"
12
gc "gopkg.in/check.v1"
14
coretesting "github.com/juju/juju/testing"
15
"github.com/juju/juju/worker"
16
"github.com/juju/juju/worker/charmrevision"
19
type WorkerSuite struct {
20
testing.IsolationSuite
23
var _ = gc.Suite(&WorkerSuite{})
25
func (s *WorkerSuite) TestUpdatesImmediately(c *gc.C) {
26
fix := newFixture(time.Minute)
27
fix.cleanTest(c, func(_ worker.Worker) {
31
fix.revisionUpdater.stub.CheckCallNames(c, "UpdateLatestRevisions")
34
func (s *WorkerSuite) TestNoMoreUpdatesUntilPeriod(c *gc.C) {
35
fix := newFixture(time.Minute)
36
fix.cleanTest(c, func(_ worker.Worker) {
38
fix.clock.Advance(time.Minute - time.Nanosecond)
41
fix.revisionUpdater.stub.CheckCallNames(c, "UpdateLatestRevisions")
44
func (s *WorkerSuite) TestUpdatesAfterPeriod(c *gc.C) {
45
fix := newFixture(time.Minute)
46
fix.cleanTest(c, func(_ worker.Worker) {
48
fix.clock.Advance(time.Minute)
52
fix.revisionUpdater.stub.CheckCallNames(c, "UpdateLatestRevisions", "UpdateLatestRevisions")
55
func (s *WorkerSuite) TestImmediateUpdateError(c *gc.C) {
56
fix := newFixture(time.Minute)
57
fix.revisionUpdater.stub.SetErrors(
58
errors.New("no updates for you"),
60
fix.dirtyTest(c, func(w worker.Worker) {
62
c.Check(w.Wait(), gc.ErrorMatches, "no updates for you")
65
fix.revisionUpdater.stub.CheckCallNames(c, "UpdateLatestRevisions")
68
func (s *WorkerSuite) TestDelayedUpdateError(c *gc.C) {
69
fix := newFixture(time.Minute)
70
fix.revisionUpdater.stub.SetErrors(
72
errors.New("no more updates for you"),
74
fix.dirtyTest(c, func(w worker.Worker) {
76
fix.clock.Advance(time.Minute)
78
c.Check(w.Wait(), gc.ErrorMatches, "no more updates for you")
81
fix.revisionUpdater.stub.CheckCallNames(c, "UpdateLatestRevisions", "UpdateLatestRevisions")
84
// workerFixture isolates a charmrevision worker for testing.
85
type workerFixture struct {
86
revisionUpdater mockRevisionUpdater
87
clock *coretesting.Clock
91
func newFixture(period time.Duration) workerFixture {
93
revisionUpdater: newMockRevisionUpdater(),
94
clock: coretesting.NewClock(time.Now()),
99
type testFunc func(worker.Worker)
101
func (fix workerFixture) cleanTest(c *gc.C, test testFunc) {
102
fix.runTest(c, test, true)
105
func (fix workerFixture) dirtyTest(c *gc.C, test testFunc) {
106
fix.runTest(c, test, false)
109
func (fix workerFixture) runTest(c *gc.C, test testFunc, checkWaitErr bool) {
110
w, err := charmrevision.NewWorker(charmrevision.Config{
111
RevisionUpdater: fix.revisionUpdater,
115
c.Assert(err, jc.ErrorIsNil)
117
err := worker.Stop(w)
119
c.Check(err, jc.ErrorIsNil)
125
func (fix workerFixture) waitCall(c *gc.C) {
127
case <-fix.revisionUpdater.calls:
128
case <-time.After(coretesting.LongWait):
129
c.Fatalf("timed out")
133
func (fix workerFixture) waitNoCall(c *gc.C) {
135
case <-fix.revisionUpdater.calls:
136
c.Fatalf("unexpected revisionUpdater call")
137
case <-time.After(coretesting.ShortWait):
141
// mockRevisionUpdater records (and notifies of) calls made to UpdateLatestRevisions.
142
type mockRevisionUpdater struct {
147
func newMockRevisionUpdater() mockRevisionUpdater {
148
return mockRevisionUpdater{
149
stub: &testing.Stub{},
150
calls: make(chan struct{}, 1000),
154
func (mock mockRevisionUpdater) UpdateLatestRevisions() error {
155
mock.stub.AddCall("UpdateLatestRevisions")
156
mock.calls <- struct{}{}
157
return mock.stub.NextErr()