Skip to content

Commit

Permalink
Internalises events notification channel in testing cache controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
manadart committed Jun 21, 2019
1 parent b78e03e commit b345347
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
8 changes: 3 additions & 5 deletions apiserver/facades/agent/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ type loggerSuite struct {
resources *common.Resources
authorizer apiservertesting.FakeAuthorizer

change cache.ModelChange
ctrl *cachetest.TestController
events <-chan interface{}
capture func(change interface{})
}

Expand All @@ -55,14 +53,14 @@ func (s *loggerSuite) SetUpTest(c *gc.C) {
}

s.ctrl = cachetest.NewTestController(cachetest.ModelEvents)
s.events = s.ctrl.Init(c)
s.ctrl.Init(c)

// Add the current model to the controller.
m := cachetest.ModelChangeFromState(c, s.State)
s.ctrl.SendChange(m)

// Ensure it is processed before we create the logger API.
s.ctrl.NextChange(c, s.events)
_ = s.ctrl.NextChange(c)

s.AddCleanup(func(c *gc.C) { workertest.CleanKill(c, s.ctrl.Controller) })

Expand Down Expand Up @@ -116,7 +114,7 @@ func (s *loggerSuite) setLoggingConfig(c *gc.C, loggingConfig string) {
m := cachetest.ModelChangeFromState(c, s.State)
m.Config["logging-config"] = loggingConfig
s.ctrl.SendChange(m)
s.ctrl.NextChange(c, s.events)
_ = s.ctrl.NextChange(c)
}

func (s *loggerSuite) TestWatchLoggingConfig(c *gc.C) {
Expand Down
20 changes: 9 additions & 11 deletions core/cache/cachetest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type TestController struct {

matchers []func(interface{}) bool
changes chan interface{}
events chan interface{}
}

// NewTestController returns creates and returns a new test controller
Expand All @@ -35,15 +36,15 @@ func NewTestController(matchers ...func(interface{}) bool) *TestController {
}
}

// Init instantiates the inner cache controller and returns a channel for
// synchronising tests. Based on the input matchers, cache events for those
// types will be sent on the channel when the cache processes them.
// Init instantiates the inner cache controller and sets up event
// synchronisation based on the input matchers.
// Changes sent to the cache can be waited on by using the `NextChange` method.
//
// NOTE: It is recommended to perform this initialisation in the actual test
// method rather than `SetupSuite` or `SetupTest` as different gc.C references
// are supplied to each of those methods.
func (tc *TestController) Init(c *gc.C, matchers ...func(interface{}) bool) <-chan interface{} {
events := make(chan interface{})
func (tc *TestController) Init(c *gc.C, matchers ...func(interface{}) bool) {
tc.events = make(chan interface{})
matchers = append(tc.matchers, matchers...)

notify := func(change interface{}) {
Expand All @@ -58,7 +59,7 @@ func (tc *TestController) Init(c *gc.C, matchers ...func(interface{}) bool) <-ch
if send {
c.Logf("sending %#v", change)
select {
case events <- change:
case tc.events <- change:
case <-time.After(testing.LongWait):
c.Fatalf("change not processed by test")
}
Expand All @@ -72,8 +73,6 @@ func (tc *TestController) Init(c *gc.C, matchers ...func(interface{}) bool) <-ch
})
c.Assert(err, jc.ErrorIsNil)
tc.Controller = cc

return events
}

// UpdateModel updates the current model for the input state in the cache.
Expand Down Expand Up @@ -102,11 +101,10 @@ func (tc *TestController) SendChange(change interface{}) {

// NextChange returns the next change processed by the cache that satisfies a
// matcher, or fails the test with a time-out.
// This method should receive the channel returned by a call to `Init`.
func (tc *TestController) NextChange(c *gc.C, changes <-chan interface{}) interface{} {
func (tc *TestController) NextChange(c *gc.C) interface{} {
var obtained interface{}
select {
case obtained = <-changes:
case obtained = <-tc.events:
case <-time.After(testing.LongWait):
c.Fatalf("change not processed by test")
}
Expand Down

0 comments on commit b345347

Please sign in to comment.