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