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

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package txnpruner_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/clock"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	"gopkg.in/juju/worker.v1"
    15  	"gopkg.in/juju/worker.v1/workertest"
    16  
    17  	"github.com/juju/juju/worker/txnpruner"
    18  )
    19  
    20  type ManifoldSuite struct {
    21  	testing.IsolationSuite
    22  	stub   testing.Stub
    23  	config txnpruner.ManifoldConfig
    24  	worker worker.Worker
    25  }
    26  
    27  var _ = gc.Suite(&ManifoldSuite{})
    28  
    29  func (s *ManifoldSuite) SetUpTest(c *gc.C) {
    30  	s.IsolationSuite.SetUpTest(c)
    31  	s.stub.ResetCalls()
    32  	s.config = s.validConfig()
    33  	s.worker = worker.NewRunner(worker.RunnerParams{})
    34  	s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, s.worker) })
    35  }
    36  
    37  func (s *ManifoldSuite) validConfig() txnpruner.ManifoldConfig {
    38  	return txnpruner.ManifoldConfig{
    39  		ClockName:     "clock",
    40  		StateName:     "state",
    41  		PruneInterval: time.Hour,
    42  		NewWorker: func(tp txnpruner.TransactionPruner, interval time.Duration, clock clock.Clock) worker.Worker {
    43  			s.stub.AddCall("NewWorker", tp, interval, clock)
    44  			return s.worker
    45  		},
    46  	}
    47  }
    48  
    49  func (s *ManifoldSuite) TestValid(c *gc.C) {
    50  	c.Check(s.config.Validate(), jc.ErrorIsNil)
    51  }
    52  
    53  func (s *ManifoldSuite) TestMissingClockName(c *gc.C) {
    54  	s.config.ClockName = ""
    55  	s.checkNotValid(c, "empty ClockName not valid")
    56  }
    57  
    58  func (s *ManifoldSuite) TestMissingStateName(c *gc.C) {
    59  	s.config.StateName = ""
    60  	s.checkNotValid(c, "empty StateName not valid")
    61  }
    62  
    63  func (s *ManifoldSuite) TestZeroPruneInterval(c *gc.C) {
    64  	s.config.PruneInterval = 0
    65  	s.checkNotValid(c, "non-positive PruneInterval not valid")
    66  }
    67  
    68  func (s *ManifoldSuite) TestMissingNewWorker(c *gc.C) {
    69  	s.config.NewWorker = nil
    70  	s.checkNotValid(c, "nil NewWorker not valid")
    71  }
    72  
    73  func (s *ManifoldSuite) checkNotValid(c *gc.C, expect string) {
    74  	err := s.config.Validate()
    75  	c.Check(err, gc.ErrorMatches, expect)
    76  	c.Check(err, jc.Satisfies, errors.IsNotValid)
    77  }