github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/workertest/workertest_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package workertest_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/testing"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/worker/workertest"
    14  )
    15  
    16  type Suite struct {
    17  	testing.IsolationSuite
    18  }
    19  
    20  var _ = gc.Suite(&Suite{})
    21  
    22  func (s *Suite) SetUpTest(c *gc.C) {
    23  	s.IsolationSuite.SetUpTest(c)
    24  	s.PatchValue(workertest.KillTimeout, time.Second)
    25  }
    26  
    27  func (s *Suite) CheckFailed(c *gc.C) {
    28  	if c.Failed() {
    29  		c.Succeed()
    30  	} else {
    31  		c.Errorf("expected failure; none observed")
    32  	}
    33  	c.Logf("-------------------------------")
    34  }
    35  
    36  func (s *Suite) TestCheckAliveSuccess(c *gc.C) {
    37  	w := workertest.NewErrorWorker(nil)
    38  	defer workertest.CleanKill(c, w)
    39  
    40  	workertest.CheckAlive(c, w)
    41  }
    42  
    43  func (s *Suite) TestCheckAliveFailure(c *gc.C) {
    44  	w := workertest.NewDeadWorker(nil)
    45  
    46  	workertest.CheckAlive(c, w)
    47  	s.CheckFailed(c)
    48  }
    49  
    50  func (s *Suite) TestCheckKilledSuccess(c *gc.C) {
    51  	expect := errors.New("snifplog")
    52  	w := workertest.NewErrorWorker(expect)
    53  	defer workertest.DirtyKill(c, w)
    54  
    55  	w.Kill()
    56  	err := workertest.CheckKilled(c, w)
    57  	c.Check(err, gc.Equals, expect)
    58  }
    59  
    60  func (s *Suite) TestCheckKilledTimeout(c *gc.C) {
    61  	w := workertest.NewErrorWorker(nil)
    62  	defer workertest.CleanKill(c, w)
    63  
    64  	err := workertest.CheckKilled(c, w)
    65  	s.CheckFailed(c)
    66  	c.Check(err, gc.ErrorMatches, "workertest: worker not stopping")
    67  }
    68  
    69  func (s *Suite) TestCheckKillSuccess(c *gc.C) {
    70  	expect := errors.New("fledbon")
    71  	w := workertest.NewErrorWorker(expect)
    72  	defer workertest.DirtyKill(c, w)
    73  
    74  	err := workertest.CheckKill(c, w)
    75  	c.Check(err, gc.Equals, expect)
    76  }
    77  
    78  func (s *Suite) TestCheckKillTimeout(c *gc.C) {
    79  	w := workertest.NewForeverWorker(nil)
    80  	defer w.ReallyKill()
    81  
    82  	err := workertest.CheckKill(c, w)
    83  	s.CheckFailed(c)
    84  	c.Check(err, gc.ErrorMatches, "workertest: worker not stopping")
    85  }
    86  
    87  func (s *Suite) TestCleanKillSuccess(c *gc.C) {
    88  	w := workertest.NewErrorWorker(nil)
    89  
    90  	workertest.CleanKill(c, w)
    91  }
    92  
    93  func (s *Suite) TestCleanKillFailure(c *gc.C) {
    94  	w := workertest.NewErrorWorker(errors.New("kebdrix"))
    95  
    96  	workertest.CleanKill(c, w)
    97  	s.CheckFailed(c)
    98  }
    99  
   100  func (s *Suite) TestCleanKillTimeout(c *gc.C) {
   101  	w := workertest.NewForeverWorker(nil)
   102  	defer w.ReallyKill()
   103  
   104  	workertest.CleanKill(c, w)
   105  	s.CheckFailed(c)
   106  }
   107  
   108  func (s *Suite) TestDirtyKillSuccess(c *gc.C) {
   109  	w := workertest.NewErrorWorker(errors.New("hifstit"))
   110  
   111  	workertest.DirtyKill(c, w)
   112  }
   113  
   114  func (s *Suite) TestDirtyKillTimeout(c *gc.C) {
   115  	w := workertest.NewForeverWorker(nil)
   116  	defer w.ReallyKill()
   117  
   118  	workertest.DirtyKill(c, w)
   119  	s.CheckFailed(c)
   120  }