github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/retrystrategy/fixture_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Copyright 2016 Cloudbase Solutions 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package retrystrategy_test 6 7 import ( 8 "time" 9 10 "github.com/juju/names" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/params" 16 coretesting "github.com/juju/juju/testing" 17 "github.com/juju/juju/watcher" 18 "github.com/juju/juju/worker" 19 "github.com/juju/juju/worker/retrystrategy" 20 "github.com/juju/juju/worker/workertest" 21 ) 22 23 type fixture struct { 24 testing.Stub 25 } 26 27 func newFixture(c *gc.C, errs ...error) *fixture { 28 fix := &fixture{} 29 c.Assert(nil, jc.ErrorIsNil) 30 fix.SetErrors(errs...) 31 return fix 32 } 33 34 func (fix *fixture) Run(c *gc.C, test func(worker.Worker)) { 35 stubRetryStrategy := params.RetryStrategy{ 36 ShouldRetry: true, 37 } 38 stubTag := stubTag{} 39 stubFacade := newStubFacade(c, &fix.Stub, stubRetryStrategy, stubTag) 40 stubConfig := retrystrategy.WorkerConfig{ 41 Facade: stubFacade, 42 AgentTag: stubTag, 43 RetryStrategy: stubRetryStrategy, 44 } 45 46 w, err := retrystrategy.NewRetryStrategyWorker(stubConfig) 47 c.Assert(err, jc.ErrorIsNil) 48 done := make(chan struct{}) 49 go func() { 50 defer close(done) 51 defer worker.Stop(w) 52 test(w) 53 }() 54 55 select { 56 case <-done: 57 case <-time.After(coretesting.LongWait): 58 c.Fatalf("test timed out") 59 } 60 } 61 62 type stubFacade struct { 63 c *gc.C 64 stub *testing.Stub 65 watcher *stubWatcher 66 count int 67 shouldBounce bool 68 initialStrategy params.RetryStrategy 69 stubTag names.Tag 70 } 71 72 func newStubFacade(c *gc.C, stub *testing.Stub, initialStrategy params.RetryStrategy, stubTag names.Tag) *stubFacade { 73 return &stubFacade{ 74 c: c, 75 stub: stub, 76 watcher: newStubWatcher(), 77 count: 0, 78 initialStrategy: initialStrategy, 79 stubTag: stubTag, 80 } 81 } 82 83 // WatchRetryStrategy is part of the retrystrategy Facade 84 func (f *stubFacade) WatchRetryStrategy(agentTag names.Tag) (watcher.NotifyWatcher, error) { 85 f.c.Assert(agentTag, gc.Equals, f.stubTag) 86 f.stub.AddCall("WatchRetryStrategy", agentTag) 87 err := f.stub.NextErr() 88 if err != nil { 89 return nil, err 90 } 91 return f.watcher, nil 92 } 93 94 // RetryStrategy is part of the retrystrategy Facade 95 func (f *stubFacade) RetryStrategy(agentTag names.Tag) (params.RetryStrategy, error) { 96 f.c.Assert(agentTag, gc.Equals, f.stubTag) 97 f.stub.AddCall("RetryStrategy", agentTag) 98 f.count = f.count + 1 99 // Change the strategy after 2 handles 100 if f.count == 2 { 101 f.initialStrategy.ShouldRetry = !f.initialStrategy.ShouldRetry 102 } 103 return f.initialStrategy, f.stub.NextErr() 104 } 105 106 type stubWatcher struct { 107 worker.Worker 108 notifyChan <-chan struct{} 109 } 110 111 func newStubWatcher() *stubWatcher { 112 changes := make(chan struct{}, 3) 113 changes <- struct{}{} 114 changes <- struct{}{} 115 changes <- struct{}{} 116 return &stubWatcher{ 117 Worker: workertest.NewErrorWorker(nil), 118 notifyChan: changes, 119 } 120 } 121 122 // Changes is part of the watcher.NotifyWatcher interface 123 func (w *stubWatcher) Changes() watcher.NotifyChannel { 124 return w.notifyChan 125 } 126 127 type stubTag struct { 128 names.Tag 129 }