github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/context/cache_test.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package context_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/worker/uniter/runner/context"
    14  )
    15  
    16  type settingsResult struct {
    17  	settings params.Settings
    18  	err      error
    19  }
    20  
    21  type RelationCacheSuite struct {
    22  	testing.IsolationSuite
    23  	calls   []string
    24  	results []settingsResult
    25  }
    26  
    27  var _ = gc.Suite(&RelationCacheSuite{})
    28  
    29  func (s *RelationCacheSuite) SetUpTest(c *gc.C) {
    30  	s.calls = []string{}
    31  	s.results = []settingsResult{}
    32  }
    33  
    34  func (s *RelationCacheSuite) ReadSettings(unitName string) (params.Settings, error) {
    35  	result := s.results[len(s.calls)]
    36  	s.calls = append(s.calls, unitName)
    37  	return result.settings, result.err
    38  }
    39  
    40  func (s *RelationCacheSuite) TestCreateEmpty(c *gc.C) {
    41  	cache := context.NewRelationCache(s.ReadSettings, nil)
    42  	c.Assert(cache.MemberNames(), gc.HasLen, 0)
    43  	c.Assert(s.calls, gc.HasLen, 0)
    44  }
    45  
    46  func (s *RelationCacheSuite) TestCreateWithMembers(c *gc.C) {
    47  	cache := context.NewRelationCache(s.ReadSettings, []string{"u/3", "u/2", "u/1"})
    48  	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"u/1", "u/2", "u/3"})
    49  	c.Assert(s.calls, gc.HasLen, 0)
    50  }
    51  
    52  func (s *RelationCacheSuite) TestInvalidateMemberChangesMembership(c *gc.C) {
    53  	cache := context.NewRelationCache(s.ReadSettings, nil)
    54  	cache.InvalidateMember("foo/1")
    55  	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"foo/1"})
    56  	cache.InvalidateMember("foo/2")
    57  	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"foo/1", "foo/2"})
    58  	cache.InvalidateMember("foo/2")
    59  	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"foo/1", "foo/2"})
    60  	c.Assert(s.calls, gc.HasLen, 0)
    61  }
    62  
    63  func (s *RelationCacheSuite) TestRemoveMemberChangesMembership(c *gc.C) {
    64  	cache := context.NewRelationCache(s.ReadSettings, []string{"x/2"})
    65  	cache.RemoveMember("x/1")
    66  	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"x/2"})
    67  	cache.RemoveMember("x/2")
    68  	c.Assert(cache.MemberNames(), gc.HasLen, 0)
    69  	c.Assert(s.calls, gc.HasLen, 0)
    70  }
    71  
    72  func (s *RelationCacheSuite) TestPruneChangesMembership(c *gc.C) {
    73  	cache := context.NewRelationCache(s.ReadSettings, []string{"u/1", "u/2", "u/3"})
    74  	cache.Prune([]string{"u/3", "u/4", "u/5"})
    75  	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"u/3", "u/4", "u/5"})
    76  	c.Assert(s.calls, gc.HasLen, 0)
    77  }
    78  
    79  func (s *RelationCacheSuite) TestSettingsPropagatesError(c *gc.C) {
    80  	s.results = []settingsResult{{
    81  		nil, errors.New("blam"),
    82  	}}
    83  	cache := context.NewRelationCache(s.ReadSettings, nil)
    84  
    85  	settings, err := cache.Settings("whatever")
    86  	c.Assert(settings, gc.IsNil)
    87  	c.Assert(err, gc.ErrorMatches, "blam")
    88  	c.Assert(s.calls, jc.DeepEquals, []string{"whatever"})
    89  }
    90  
    91  func (s *RelationCacheSuite) TestSettingsCachesMemberSettings(c *gc.C) {
    92  	s.results = []settingsResult{{
    93  		params.Settings{"foo": "bar"}, nil,
    94  	}}
    95  	cache := context.NewRelationCache(s.ReadSettings, []string{"x/2"})
    96  
    97  	for i := 0; i < 2; i++ {
    98  		settings, err := cache.Settings("x/2")
    99  		c.Assert(err, jc.ErrorIsNil)
   100  		c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   101  		c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})
   102  	}
   103  }
   104  
   105  func (s *RelationCacheSuite) TestInvalidateMemberUncachesMemberSettings(c *gc.C) {
   106  	s.results = []settingsResult{{
   107  		params.Settings{"foo": "bar"}, nil,
   108  	}, {
   109  		params.Settings{"baz": "qux"}, nil,
   110  	}}
   111  	cache := context.NewRelationCache(s.ReadSettings, []string{"x/2"})
   112  
   113  	settings, err := cache.Settings("x/2")
   114  	c.Assert(err, jc.ErrorIsNil)
   115  	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   116  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})
   117  
   118  	cache.InvalidateMember("x/2")
   119  	settings, err = cache.Settings("x/2")
   120  	c.Assert(err, jc.ErrorIsNil)
   121  	c.Assert(settings, jc.DeepEquals, params.Settings{"baz": "qux"})
   122  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2", "x/2"})
   123  }
   124  
   125  func (s *RelationCacheSuite) TestInvalidateMemberUncachesOtherSettings(c *gc.C) {
   126  	s.results = []settingsResult{{
   127  		params.Settings{"foo": "bar"}, nil,
   128  	}, {
   129  		params.Settings{"baz": "qux"}, nil,
   130  	}}
   131  	cache := context.NewRelationCache(s.ReadSettings, nil)
   132  
   133  	settings, err := cache.Settings("x/2")
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   136  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})
   137  
   138  	cache.InvalidateMember("x/2")
   139  	settings, err = cache.Settings("x/2")
   140  	c.Assert(err, jc.ErrorIsNil)
   141  	c.Assert(settings, jc.DeepEquals, params.Settings{"baz": "qux"})
   142  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2", "x/2"})
   143  }
   144  
   145  func (s *RelationCacheSuite) TestRemoveMemberUncachesMemberSettings(c *gc.C) {
   146  	s.results = []settingsResult{{
   147  		params.Settings{"foo": "bar"}, nil,
   148  	}, {
   149  		params.Settings{"baz": "qux"}, nil,
   150  	}}
   151  	cache := context.NewRelationCache(s.ReadSettings, []string{"x/2"})
   152  
   153  	settings, err := cache.Settings("x/2")
   154  	c.Assert(err, jc.ErrorIsNil)
   155  	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   156  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})
   157  
   158  	cache.RemoveMember("x/2")
   159  	settings, err = cache.Settings("x/2")
   160  	c.Assert(err, jc.ErrorIsNil)
   161  	c.Assert(settings, jc.DeepEquals, params.Settings{"baz": "qux"})
   162  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2", "x/2"})
   163  }
   164  
   165  func (s *RelationCacheSuite) TestSettingsCachesOtherSettings(c *gc.C) {
   166  	s.results = []settingsResult{{
   167  		params.Settings{"foo": "bar"}, nil,
   168  	}}
   169  	cache := context.NewRelationCache(s.ReadSettings, nil)
   170  
   171  	for i := 0; i < 2; i++ {
   172  		settings, err := cache.Settings("x/2")
   173  		c.Assert(err, jc.ErrorIsNil)
   174  		c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   175  		c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})
   176  	}
   177  }
   178  
   179  func (s *RelationCacheSuite) TestPrunePreservesMemberSettings(c *gc.C) {
   180  	s.results = []settingsResult{{
   181  		params.Settings{"foo": "bar"}, nil,
   182  	}}
   183  	cache := context.NewRelationCache(s.ReadSettings, []string{"foo/2"})
   184  
   185  	settings, err := cache.Settings("foo/2")
   186  	c.Assert(err, jc.ErrorIsNil)
   187  	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   188  	c.Assert(s.calls, jc.DeepEquals, []string{"foo/2"})
   189  
   190  	cache.Prune([]string{"foo/2"})
   191  	settings, err = cache.Settings("foo/2")
   192  	c.Assert(err, jc.ErrorIsNil)
   193  	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   194  	c.Assert(s.calls, jc.DeepEquals, []string{"foo/2"})
   195  }
   196  
   197  func (s *RelationCacheSuite) TestPruneUncachesOtherSettings(c *gc.C) {
   198  	s.results = []settingsResult{{
   199  		params.Settings{"foo": "bar"}, nil,
   200  	}, {
   201  		params.Settings{"baz": "qux"}, nil,
   202  	}}
   203  	cache := context.NewRelationCache(s.ReadSettings, nil)
   204  
   205  	settings, err := cache.Settings("x/2")
   206  	c.Assert(err, jc.ErrorIsNil)
   207  	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
   208  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})
   209  
   210  	cache.Prune(nil)
   211  	settings, err = cache.Settings("x/2")
   212  	c.Assert(err, jc.ErrorIsNil)
   213  	c.Assert(settings, jc.DeepEquals, params.Settings{"baz": "qux"})
   214  	c.Assert(s.calls, jc.DeepEquals, []string{"x/2", "x/2"})
   215  }