github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/lease/skew_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"
     8  
     9  	"github.com/juju/testing"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/state/lease"
    13  )
    14  
    15  type SkewSuite struct {
    16  	testing.IsolationSuite
    17  }
    18  
    19  var _ = gc.Suite(&SkewSuite{})
    20  
    21  func (s *SkewSuite) TestZero(c *gc.C) {
    22  	now := time.Now()
    23  
    24  	// The zero Skew should act as unskewed.
    25  	skew := lease.Skew{}
    26  
    27  	c.Check(skew.Earliest(now), gc.Equals, now)
    28  	c.Check(skew.Latest(now), gc.Equals, now)
    29  }
    30  
    31  func (s *SkewSuite) TestApparentPastWrite(c *gc.C) {
    32  	now := time.Now()
    33  	c.Logf("now: %s", now)
    34  	oneSecondAgo := now.Add(-time.Second)
    35  	threeSecondsAgo := now.Add(-3 * time.Second)
    36  	nineSecondsAgo := now.Add(-9 * time.Second)
    37  	sixSecondsLater := now.Add(6 * time.Second)
    38  	eightSecondsLater := now.Add(8 * time.Second)
    39  
    40  	// Where T is the current local time:
    41  	// between T-3 and T-1, we read T-9 from the remote clock.
    42  	skew := lease.Skew{
    43  		LastWrite: nineSecondsAgo,
    44  		Beginning: threeSecondsAgo,
    45  		End:       oneSecondAgo,
    46  	}
    47  
    48  	// If the remote wrote a long time ago -- say, 20 minutes ago it thought it
    49  	// was 9 seconds ago -- its clock could be arbitrarily far ahead of ours.
    50  	// But we know that when we started reading, 3 seconds ago, it might not
    51  	// have seen a time later than 9 seconds ago; so right now, three seconds
    52  	// after that, it might not have seen a time later than 6 seconds ago.
    53  	c.Check(skew.Earliest(now), gc.DeepEquals, sixSecondsLater)
    54  
    55  	// If the remote wrote at the very last moment -- exactly one second ago,
    56  	// it thought it was nine seconds ago -- it could have a clock a full 8
    57  	// seconds behind ours. If so, the *latest* time at which it *might* still
    58  	// think it's before now is 8 seconds in the future.
    59  	c.Check(skew.Latest(now), gc.DeepEquals, eightSecondsLater)
    60  }
    61  
    62  func (s *SkewSuite) TestApparentFutureWrite(c *gc.C) {
    63  	now := time.Now()
    64  	c.Logf("now: %s", now)
    65  	oneSecondAgo := now.Add(-time.Second)
    66  	threeSecondsAgo := now.Add(-3 * time.Second)
    67  	tenSecondsAgo := now.Add(-10 * time.Second)
    68  	twelveSecondsAgo := now.Add(-12 * time.Second)
    69  	nineSecondsLater := now.Add(9 * time.Second)
    70  
    71  	// Where T is the current local time:
    72  	// between T-3 and T-1, we read T+9 from the remote clock.
    73  	skew := lease.Skew{
    74  		LastWrite: nineSecondsLater,
    75  		Beginning: threeSecondsAgo,
    76  		End:       oneSecondAgo,
    77  	}
    78  
    79  	// If the remote wrote a long time ago -- say, 20 minutes ago it thought
    80  	// it was nine seconds after now -- its clock could be arbitrarily far
    81  	// ahead of ours. But we know that when we started reading, 3 seconds ago,
    82  	// it might not have seen a time later than 9 seconds in the future; so
    83  	// right now, three seconds after that, it might not have seen a time later
    84  	// than twelve seconds in the future.
    85  	c.Check(skew.Earliest(now), gc.DeepEquals, twelveSecondsAgo)
    86  
    87  	// If the remote wrote at the very last moment -- exactly one second ago,
    88  	// it thought it was 9 seconds in the future -- it could have a clock a
    89  	// full 10 seconds ahead of ours. If so, the *latest* time at which it
    90  	// might still have thought it was before now is ten seconds in the past.
    91  	c.Check(skew.Latest(now), gc.DeepEquals, tenSecondsAgo)
    92  }
    93  
    94  func (s *SkewSuite) TestBracketedWrite(c *gc.C) {
    95  	now := time.Now()
    96  	c.Logf("now: %s", now)
    97  	oneSecondAgo := now.Add(-time.Second)
    98  	twoSecondsAgo := now.Add(-2 * time.Second)
    99  	threeSecondsAgo := now.Add(-3 * time.Second)
   100  	fiveSecondsAgo := now.Add(-5 * time.Second)
   101  	oneSecondLater := now.Add(time.Second)
   102  
   103  	// Where T is the current local time:
   104  	// between T-5 and T-1, we read T-2 from the remote clock.
   105  	skew := lease.Skew{
   106  		LastWrite: twoSecondsAgo,
   107  		Beginning: fiveSecondsAgo,
   108  		End:       oneSecondAgo,
   109  	}
   110  
   111  	// If the remote wrote a long time ago -- say, 20 minutes ago it thought
   112  	// it was two seconds before now -- its clock could be arbitrarily far
   113  	// ahead of ours. But we know that when we started reading, 5 seconds ago,
   114  	// it might not have seen a time later than 2 seconds in the past; so
   115  	// right now, five seconds after that, it might not have seen a time later
   116  	// than three seconds in the future.
   117  	c.Check(skew.Earliest(now), gc.DeepEquals, threeSecondsAgo)
   118  
   119  	// If the remote wrote at the very last moment -- exactly one second ago,
   120  	// it thought it was 2 seconds in the past -- it could have a clock one
   121  	// second behind ours. If so, the *latest* time at which it might still
   122  	// have thought it was before now is one second in the future.
   123  	c.Check(skew.Latest(now), gc.DeepEquals, oneSecondLater)
   124  }
   125  
   126  func (s *SkewSuite) TestMixedTimezones(c *gc.C) {
   127  	here := time.FixedZone("here", -3600)
   128  	there := time.FixedZone("there", -7200)
   129  	elsewhere := time.FixedZone("elsewhere", -10800)
   130  
   131  	// This is a straight copy of TestBracketedWrite, with strange timezones
   132  	// inserted to check that they don't affect the results at all.
   133  	now := time.Now()
   134  	c.Logf("now: %s", now)
   135  	oneSecondAgo := now.Add(-time.Second)
   136  	twoSecondsAgo := now.Add(-2 * time.Second)
   137  	threeSecondsAgo := now.Add(-3 * time.Second)
   138  	fiveSecondsAgo := now.Add(-5 * time.Second)
   139  	oneSecondLater := now.Add(time.Second)
   140  
   141  	// Where T is the current local time:
   142  	// between T-5 and T-1, we read T-2 from the remote clock.
   143  	skew := lease.Skew{
   144  		LastWrite: twoSecondsAgo.In(here),
   145  		Beginning: fiveSecondsAgo.In(there),
   146  		End:       oneSecondAgo.In(elsewhere),
   147  	}
   148  
   149  	// If the remote wrote a long time ago -- say, 20 minutes ago it thought
   150  	// it was two seconds before now -- its clock could be arbitrarily far
   151  	// ahead of ours. But we know that when we started reading, 5 seconds ago,
   152  	// it might not have seen a time later than 2 seconds in the past; so
   153  	// right now, five seconds after that, it might not have seen a time later
   154  	// than three seconds in the future.
   155  	c.Check(skew.Earliest(now), gc.DeepEquals, threeSecondsAgo.In(there))
   156  
   157  	// If the remote wrote at the very last moment -- exactly one second ago,
   158  	// it thought it was 2 seconds in the past -- it could have a clock one
   159  	// second behind ours. If so, the *latest* time at which it might still
   160  	// have thought it was before now is one second in the future.
   161  	c.Check(skew.Latest(now), gc.DeepEquals, oneSecondLater.In(elsewhere))
   162  }