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