github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/common/cleanup_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_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  	"gopkg.in/juju/worker.v1"
    12  	"gopkg.in/juju/worker.v1/workertest"
    13  
    14  	coretesting "github.com/juju/juju/testing"
    15  	"github.com/juju/juju/worker/common"
    16  )
    17  
    18  type cleanupSuite struct {
    19  	coretesting.BaseSuite
    20  }
    21  
    22  var _ = gc.Suite(&cleanupSuite{})
    23  
    24  func (s *cleanupSuite) TestCleansUpOnce(c *gc.C) {
    25  	var w fakeWorker
    26  	cleanup := func() {
    27  		w.stub.AddCall("cleanup")
    28  	}
    29  	w.stub.SetErrors(errors.Errorf("oops"))
    30  	cw := common.NewCleanupWorker(&w, cleanup)
    31  	c.Assert(cw.Wait(), gc.ErrorMatches, "oops")
    32  	w.stub.CheckCallNames(c, "Wait", "cleanup")
    33  	c.Assert(cw.Wait(), jc.ErrorIsNil)
    34  	// Doesn't call cleanup again.
    35  	w.stub.CheckCallNames(c, "Wait", "cleanup", "Wait")
    36  }
    37  
    38  func (s *cleanupSuite) TestReport(c *gc.C) {
    39  	var w fakeWorker
    40  	cw := common.NewCleanupWorker(&w, func() {})
    41  	defer workertest.CleanKill(c, cw)
    42  
    43  	reporter, ok := cw.(worker.Reporter)
    44  	c.Assert(ok, jc.IsTrue)
    45  	c.Assert(reporter.Report(), jc.DeepEquals, map[string]interface{}{
    46  		"fake": true,
    47  	})
    48  }
    49  
    50  type fakeWorker struct {
    51  	stub testing.Stub
    52  }
    53  
    54  func (w *fakeWorker) Kill() {
    55  	w.stub.AddCall("Kill")
    56  }
    57  
    58  func (w *fakeWorker) Wait() error {
    59  	w.stub.AddCall("Wait")
    60  	return w.stub.NextErr()
    61  }
    62  
    63  func (w *fakeWorker) Report() map[string]interface{} {
    64  	return map[string]interface{}{
    65  		"fake": true,
    66  	}
    67  }