github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/state/lease/store_remote_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 12 "github.com/juju/juju/core/lease" 13 ) 14 15 // StoreRemoteSuite checks that stores do not break one another's promises. 16 type StoreRemoteSuite struct { 17 FixtureSuite 18 lease time.Duration 19 localOffset time.Duration 20 globalOffset time.Duration 21 baseline *Fixture 22 skewed *Fixture 23 } 24 25 var _ = gc.Suite(&StoreRemoteSuite{}) 26 27 func (s *StoreRemoteSuite) SetUpTest(c *gc.C) { 28 s.FixtureSuite.SetUpTest(c) 29 30 s.lease = time.Minute 31 32 // the skewed store's clock is 3 hours ahead of the local store's. 33 s.localOffset = 3 * time.Hour 34 35 // the skewed store takes an additional second to observe the global 36 // time advance; when the local store thinks the global time is T, 37 // the remote store thinks it is T-1s. 38 s.globalOffset = -time.Second 39 40 s.baseline = s.EasyFixture(c) 41 err := s.baseline.Store.ClaimLease(key("name"), lease.Request{"holder", s.lease}) 42 c.Assert(err, jc.ErrorIsNil) 43 44 // Remote store, whose local clock is offset significantly from the 45 // local store's, but has a slightly delayed global clock. 46 s.skewed = s.NewFixture(c, FixtureParams{ 47 Id: "remote-store", 48 LocalClockStart: s.baseline.Zero.Add(s.localOffset), 49 GlobalClockOffset: s.globalOffset, 50 }) 51 } 52 53 func (s *StoreRemoteSuite) skewedExpiry() time.Time { 54 return s.baseline.Zero.Add(s.lease + s.localOffset - s.globalOffset) 55 } 56 57 // TestExpiryLocalOffset shows that the expiry time reported for the lease is 58 // offset by the local clock of the store, and the store's observation of 59 // the global clock. 60 func (s *StoreRemoteSuite) TestExpiryOffset(c *gc.C) { 61 c.Check(key("name"), s.skewed.Holder(), "holder") 62 c.Check(key("name"), s.skewed.Expiry(), s.skewedExpiry()) 63 } 64 65 func (s *StoreRemoteSuite) TestExtendRemoteLeaseNoop(c *gc.C) { 66 err := s.skewed.Store.ExtendLease(key("name"), lease.Request{"holder", 10 * time.Second}) 67 c.Check(err, jc.ErrorIsNil) 68 69 c.Check(key("name"), s.skewed.Holder(), "holder") 70 c.Check(key("name"), s.skewed.Expiry(), s.skewedExpiry()) 71 } 72 73 func (s *StoreRemoteSuite) TestExtendRemoteLeaseSimpleExtend(c *gc.C) { 74 leaseDuration := 10 * time.Minute 75 err := s.skewed.Store.ExtendLease(key("name"), lease.Request{"holder", leaseDuration}) 76 c.Check(err, jc.ErrorIsNil) 77 78 c.Check(key("name"), s.skewed.Holder(), "holder") 79 expectExpiry := s.skewed.LocalClock.Now().Add(leaseDuration) 80 c.Check(key("name"), s.skewed.Expiry(), expectExpiry) 81 } 82 83 func (s *StoreRemoteSuite) TestCannotExpireRemoteLeaseEarly(c *gc.C) { 84 s.skewed.LocalClock.Reset(s.skewedExpiry()) 85 err := s.skewed.Store.ExpireLease(key("name")) 86 c.Check(err, gc.Equals, lease.ErrInvalid) 87 } 88 89 func (s *StoreRemoteSuite) TestCanExpireRemoteLease(c *gc.C) { 90 s.skewed.GlobalClock.Reset(s.skewedExpiry().Add(time.Nanosecond)) 91 err := s.skewed.Store.ExpireLease(key("name")) 92 c.Check(err, jc.ErrorIsNil) 93 } 94 95 // ------------------------------------