github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/lease/client_assert_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lease_test
     5  
     6  import (
     7  	"time" // Only used for time types.
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/mgo.v2/txn"
    12  
    13  	"github.com/juju/juju/core/lease"
    14  )
    15  
    16  // ClientAssertSuite tests that AssertOp does what it should.
    17  type ClientAssertSuite struct {
    18  	FixtureSuite
    19  	fix  *Fixture
    20  	info lease.Info
    21  }
    22  
    23  var _ = gc.Suite(&ClientAssertSuite{})
    24  
    25  func (s *ClientAssertSuite) SetUpTest(c *gc.C) {
    26  	s.FixtureSuite.SetUpTest(c)
    27  	s.fix = s.EasyFixture(c)
    28  	err := s.fix.Client.ClaimLease("name", lease.Request{"holder", time.Minute})
    29  	c.Assert(err, jc.ErrorIsNil)
    30  	c.Assert("name", s.fix.Holder(), "holder")
    31  }
    32  
    33  func (s *ClientAssertSuite) TestPassesWhenLeaseHeld(c *gc.C) {
    34  	info := s.fix.Client.Leases()["name"]
    35  
    36  	var ops []txn.Op
    37  	err := info.Trapdoor(&ops)
    38  	c.Check(err, jc.ErrorIsNil)
    39  	err = s.fix.Runner.RunTransaction(ops)
    40  	c.Check(err, jc.ErrorIsNil)
    41  }
    42  
    43  func (s *ClientAssertSuite) TestPassesWhenLeaseStillHeldDespiteWriterChange(c *gc.C) {
    44  	info := s.fix.Client.Leases()["name"]
    45  
    46  	fix2 := s.NewFixture(c, FixtureParams{Id: "other-client"})
    47  	err := fix2.Client.ExtendLease("name", lease.Request{"holder", time.Hour})
    48  	c.Assert(err, jc.ErrorIsNil)
    49  
    50  	var ops []txn.Op
    51  	err = info.Trapdoor(&ops)
    52  	c.Check(err, jc.ErrorIsNil)
    53  	err = s.fix.Runner.RunTransaction(ops)
    54  	c.Check(err, gc.IsNil)
    55  }
    56  
    57  func (s *ClientAssertSuite) TestPassesWhenLeaseStillHeldDespitePassingExpiry(c *gc.C) {
    58  	info := s.fix.Client.Leases()["name"]
    59  
    60  	s.fix.Clock.Advance(time.Hour)
    61  	err := s.fix.Client.Refresh()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  
    64  	var ops []txn.Op
    65  	err = info.Trapdoor(&ops)
    66  	c.Check(err, jc.ErrorIsNil)
    67  	err = s.fix.Runner.RunTransaction(ops)
    68  	c.Check(err, gc.IsNil)
    69  }
    70  
    71  func (s *ClientAssertSuite) TestAbortsWhenLeaseVacant(c *gc.C) {
    72  	info := s.fix.Client.Leases()["name"]
    73  
    74  	s.fix.Clock.Advance(time.Hour)
    75  	err := s.fix.Client.ExpireLease("name")
    76  	c.Assert(err, jc.ErrorIsNil)
    77  
    78  	var ops []txn.Op
    79  	err = info.Trapdoor(&ops)
    80  	c.Check(err, jc.ErrorIsNil)
    81  	err = s.fix.Runner.RunTransaction(ops)
    82  	c.Check(err, gc.Equals, txn.ErrAborted)
    83  }