github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/applicationscaler/fixture_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package applicationscaler_test 5 6 import ( 7 "time" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/worker.v1" 13 "gopkg.in/juju/worker.v1/workertest" 14 15 "github.com/juju/juju/core/watcher" 16 coretesting "github.com/juju/juju/testing" 17 "github.com/juju/juju/worker/applicationscaler" 18 ) 19 20 // fixture is used to test the operation of an applicationscaler worker. 21 type fixture struct { 22 testing.Stub 23 } 24 25 func newFixture(c *gc.C, callErrors ...error) *fixture { 26 fix := &fixture{} 27 fix.SetErrors(callErrors...) 28 return fix 29 } 30 31 // Run will create an applicationscaler worker; start recording the calls 32 // it makes; and pass it to the supplied test func, which will be invoked 33 // on a new goroutine. If Run returns, it is safe to inspect the recorded 34 // calls via the embedded testing.Stub. 35 func (fix *fixture) Run(c *gc.C, test func(worker.Worker)) { 36 stubFacade := newFacade(&fix.Stub) 37 scaler, err := applicationscaler.New(applicationscaler.Config{ 38 Facade: stubFacade, 39 }) 40 c.Assert(err, jc.ErrorIsNil) 41 42 done := make(chan struct{}) 43 go func() { 44 defer close(done) 45 defer worker.Stop(scaler) 46 test(scaler) 47 }() 48 select { 49 case <-done: 50 case <-time.After(coretesting.LongWait): 51 c.Fatalf("test func timed out") 52 } 53 } 54 55 // stubFacade implements applicationscaler.Facade and records calls to its 56 // interface methods. 57 type stubFacade struct { 58 stub *testing.Stub 59 watcher *stubWatcher 60 } 61 62 func newFacade(stub *testing.Stub) *stubFacade { 63 return &stubFacade{ 64 stub: stub, 65 watcher: newStubWatcher(), 66 } 67 } 68 69 // Watch is part of the applicationscaler.Facade interface. 70 func (facade *stubFacade) Watch() (watcher.StringsWatcher, error) { 71 facade.stub.AddCall("Watch") 72 err := facade.stub.NextErr() 73 if err != nil { 74 return nil, err 75 } 76 return facade.watcher, nil 77 } 78 79 // Rescale is part of the applicationscaler.Facade interface. 80 func (facade *stubFacade) Rescale(serviceNames []string) error { 81 facade.stub.AddCall("Rescale", serviceNames) 82 return facade.stub.NextErr() 83 } 84 85 // stubWatcher implements watcher.StringsWatcher and supplied canned 86 // data over the Changes() channel. 87 type stubWatcher struct { 88 worker.Worker 89 changes chan []string 90 } 91 92 func newStubWatcher() *stubWatcher { 93 changes := make(chan []string, 3) 94 changes <- []string{"expected", "first"} 95 changes <- []string{"expected", "second"} 96 changes <- []string{"unexpected?"} 97 return &stubWatcher{ 98 Worker: workertest.NewErrorWorker(nil), 99 changes: changes, 100 } 101 } 102 103 // Changes is part of the watcher.StringsWatcher interface. 104 func (stubWatcher *stubWatcher) Changes() watcher.StringsChannel { 105 return stubWatcher.changes 106 }