github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/common/watch_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/names"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/apiserver/params"
    15  	apiservertesting "github.com/juju/juju/apiserver/testing"
    16  	"github.com/juju/juju/state"
    17  	statetesting "github.com/juju/juju/state/testing"
    18  )
    19  
    20  type agentEntityWatcherSuite struct{}
    21  
    22  var _ = gc.Suite(&agentEntityWatcherSuite{})
    23  
    24  type fakeAgentEntityWatcher struct {
    25  	state.Entity
    26  	fetchError
    27  }
    28  
    29  func (a *fakeAgentEntityWatcher) Watch() state.NotifyWatcher {
    30  	w := apiservertesting.NewFakeNotifyWatcher()
    31  	// Simulate initial event.
    32  	w.C <- struct{}{}
    33  	return w
    34  }
    35  
    36  func (*agentEntityWatcherSuite) TestWatch(c *gc.C) {
    37  	st := &fakeState{
    38  		entities: map[names.Tag]entityWithError{
    39  			u("x/0"): &fakeAgentEntityWatcher{fetchError: "x0 fails"},
    40  			u("x/1"): &fakeAgentEntityWatcher{},
    41  			u("x/2"): &fakeAgentEntityWatcher{},
    42  		},
    43  	}
    44  	getCanWatch := func() (common.AuthFunc, error) {
    45  		x0 := u("x/0")
    46  		x1 := u("x/1")
    47  		return func(tag names.Tag) bool {
    48  			return tag == x0 || tag == x1
    49  		}, nil
    50  	}
    51  	resources := common.NewResources()
    52  	a := common.NewAgentEntityWatcher(st, resources, getCanWatch)
    53  	entities := params.Entities{[]params.Entity{
    54  		{"unit-x-0"}, {"unit-x-1"}, {"unit-x-2"}, {"unit-x-3"},
    55  	}}
    56  	result, err := a.Watch(entities)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(result, gc.DeepEquals, params.NotifyWatchResults{
    59  		Results: []params.NotifyWatchResult{
    60  			{Error: &params.Error{Message: "x0 fails"}},
    61  			{"1", nil},
    62  			{Error: apiservertesting.ErrUnauthorized},
    63  			{Error: apiservertesting.ErrUnauthorized},
    64  		},
    65  	})
    66  }
    67  
    68  func (*agentEntityWatcherSuite) TestWatchError(c *gc.C) {
    69  	getCanWatch := func() (common.AuthFunc, error) {
    70  		return nil, fmt.Errorf("pow")
    71  	}
    72  	resources := common.NewResources()
    73  	a := common.NewAgentEntityWatcher(
    74  		&fakeState{},
    75  		resources,
    76  		getCanWatch,
    77  	)
    78  	_, err := a.Watch(params.Entities{[]params.Entity{{"x0"}}})
    79  	c.Assert(err, gc.ErrorMatches, "pow")
    80  }
    81  
    82  func (*agentEntityWatcherSuite) TestWatchNoArgsNoError(c *gc.C) {
    83  	getCanWatch := func() (common.AuthFunc, error) {
    84  		return nil, fmt.Errorf("pow")
    85  	}
    86  	resources := common.NewResources()
    87  	a := common.NewAgentEntityWatcher(
    88  		&fakeState{},
    89  		resources,
    90  		getCanWatch,
    91  	)
    92  	result, err := a.Watch(params.Entities{})
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	c.Assert(result.Results, gc.HasLen, 0)
    95  }
    96  
    97  type multiNotifyWatcherSuite struct{}
    98  
    99  var _ = gc.Suite(&multiNotifyWatcherSuite{})
   100  
   101  func (*multiNotifyWatcherSuite) TestMultiNotifyWatcher(c *gc.C) {
   102  	w0 := apiservertesting.NewFakeNotifyWatcher()
   103  	w0.C <- struct{}{}
   104  	w1 := apiservertesting.NewFakeNotifyWatcher()
   105  	w1.C <- struct{}{}
   106  
   107  	mw := common.NewMultiNotifyWatcher(w0, w1)
   108  	defer statetesting.AssertStop(c, mw)
   109  
   110  	wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw)
   111  	wc.AssertOneChange()
   112  
   113  	w0.C <- struct{}{}
   114  	wc.AssertOneChange()
   115  	w1.C <- struct{}{}
   116  	wc.AssertOneChange()
   117  
   118  	w0.C <- struct{}{}
   119  	w1.C <- struct{}{}
   120  	wc.AssertOneChange()
   121  }
   122  
   123  func (*multiNotifyWatcherSuite) TestMultiNotifyWatcherStop(c *gc.C) {
   124  	w0 := apiservertesting.NewFakeNotifyWatcher()
   125  	w0.C <- struct{}{}
   126  	w1 := apiservertesting.NewFakeNotifyWatcher()
   127  	w1.C <- struct{}{}
   128  
   129  	mw := common.NewMultiNotifyWatcher(w0, w1)
   130  	wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw)
   131  	wc.AssertOneChange()
   132  	statetesting.AssertCanStopWhenSending(c, mw)
   133  	wc.AssertClosed()
   134  }
   135  
   136  type nopSyncStarter struct{}
   137  
   138  func (nopSyncStarter) StartSync() {}