github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/environ/wait_test.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package environ_test 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/environs" 14 "github.com/juju/juju/environs/config" 15 coretesting "github.com/juju/juju/testing" 16 "github.com/juju/juju/worker/environ" 17 "github.com/juju/juju/worker/workertest" 18 ) 19 20 type WaitSuite struct { 21 coretesting.BaseSuite 22 } 23 24 var _ = gc.Suite(&WaitSuite{}) 25 26 func (s *WaitSuite) TestWaitAborted(c *gc.C) { 27 fix := &fixture{} 28 fix.Run(c, func(context *runContext) { 29 abort := make(chan struct{}) 30 done := make(chan struct{}) 31 go func() { 32 defer close(done) 33 env, err := environ.WaitForEnviron(context.watcher, nil, nil, abort) 34 c.Check(env, gc.IsNil) 35 c.Check(err, gc.Equals, environ.ErrWaitAborted) 36 }() 37 38 close(abort) 39 select { 40 case <-done: 41 case <-time.After(coretesting.LongWait): 42 c.Errorf("timed out waiting for abort") 43 } 44 workertest.CheckAlive(c, context.watcher) 45 }) 46 } 47 48 func (s *WaitSuite) TestWatchClosed(c *gc.C) { 49 fix := &fixture{} 50 fix.Run(c, func(context *runContext) { 51 abort := make(chan struct{}) 52 defer close(abort) 53 54 done := make(chan struct{}) 55 go func() { 56 defer close(done) 57 env, err := environ.WaitForEnviron(context.watcher, nil, nil, abort) 58 c.Check(env, gc.IsNil) 59 c.Check(err, gc.ErrorMatches, "environ config watch closed") 60 }() 61 62 context.CloseNotify() 63 select { 64 case <-done: 65 case <-time.After(coretesting.LongWait): 66 c.Errorf("timed out waiting for failure") 67 } 68 workertest.CheckAlive(c, context.watcher) 69 }) 70 } 71 72 func (s *WaitSuite) TestConfigError(c *gc.C) { 73 fix := &fixture{ 74 observerErrs: []error{ 75 errors.New("biff zonk"), 76 }, 77 } 78 fix.Run(c, func(context *runContext) { 79 abort := make(chan struct{}) 80 defer close(abort) 81 82 done := make(chan struct{}) 83 go func() { 84 defer close(done) 85 env, err := environ.WaitForEnviron(context.watcher, context, nil, abort) 86 c.Check(env, gc.IsNil) 87 c.Check(err, gc.ErrorMatches, "cannot read environ config: biff zonk") 88 }() 89 90 context.SendNotify() 91 select { 92 case <-done: 93 case <-time.After(coretesting.LongWait): 94 c.Errorf("timed out waiting for failure") 95 } 96 workertest.CheckAlive(c, context.watcher) 97 }) 98 } 99 100 func (s *WaitSuite) TestIgnoresBadConfig(c *gc.C) { 101 fix := &fixture{ 102 initialConfig: coretesting.Attrs{ 103 "type": "unknown", 104 }, 105 } 106 newEnvironFunc := func(cfg *config.Config) (environs.Environ, error) { 107 if cfg.Type() == "unknown" { 108 return nil, errors.NotValidf("config") 109 } 110 return &mockEnviron{cfg: cfg}, nil 111 } 112 fix.Run(c, func(context *runContext) { 113 abort := make(chan struct{}) 114 defer close(abort) 115 116 done := make(chan struct{}) 117 go func() { 118 defer close(done) 119 env, err := environ.WaitForEnviron(context.watcher, context, newEnvironFunc, abort) 120 if c.Check(err, jc.ErrorIsNil) { 121 c.Check(env.Config().Name(), gc.Equals, "expected-name") 122 } 123 }() 124 125 context.SendNotify() 126 select { 127 case <-time.After(coretesting.ShortWait): 128 case <-done: 129 c.Errorf("completed unexpectedly") 130 } 131 132 context.SetConfig(c, coretesting.Attrs{ 133 "name": "expected-name", 134 }) 135 context.SendNotify() 136 select { 137 case <-done: 138 case <-time.After(coretesting.LongWait): 139 c.Errorf("timed out waiting for success") 140 } 141 workertest.CheckAlive(c, context.watcher) 142 }) 143 }