github.com/embeddedgo/x@v0.0.6-0.20191217015414-d79a36f562e7/time/mono_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package time_test 6 7 import ( 8 "strings" 9 "testing" 10 11 . "github.com/embeddedgo/x/time" 12 ) 13 14 func TestHasMonotonicClock(t *testing.T) { 15 yes := func(expr string, tt Time) { 16 if GetMono(&tt) == 0 { 17 t.Errorf("%s: missing monotonic clock reading", expr) 18 } 19 } 20 no := func(expr string, tt Time) { 21 if GetMono(&tt) != 0 { 22 t.Errorf("%s: unexpected monotonic clock reading", expr) 23 } 24 } 25 26 yes("<-After(1)", <-After(1)) 27 ticker := NewTicker(1) 28 yes("<-Tick(1)", <-ticker.C) 29 ticker.Stop() 30 no("Date(2009, 11, 23, 0, 0, 0, 0, UTC)", Date(2009, 11, 23, 0, 0, 0, 0, UTC)) 31 tp, _ := Parse(UnixDate, "Sat Mar 7 11:06:39 PST 2015") 32 no(`Parse(UnixDate, "Sat Mar 7 11:06:39 PST 2015")`, tp) 33 no("Unix(1486057371, 0)", Unix(1486057371, 0)) 34 35 yes("Now()", Now()) 36 37 tu := Unix(1486057371, 0) 38 tm := tu 39 SetMono(&tm, 123456) 40 no("tu", tu) 41 yes("tm", tm) 42 43 no("tu.Add(1)", tu.Add(1)) 44 no("tu.In(UTC)", tu.In(UTC)) 45 no("tu.AddDate(1, 1, 1)", tu.AddDate(1, 1, 1)) 46 no("tu.AddDate(0, 0, 0)", tu.AddDate(0, 0, 0)) 47 no("tu.Local()", tu.Local()) 48 no("tu.UTC()", tu.UTC()) 49 no("tu.Round(2)", tu.Round(2)) 50 no("tu.Truncate(2)", tu.Truncate(2)) 51 52 yes("tm.Add(1)", tm.Add(1)) 53 no("tm.AddDate(1, 1, 1)", tm.AddDate(1, 1, 1)) 54 no("tm.AddDate(0, 0, 0)", tm.AddDate(0, 0, 0)) 55 no("tm.In(UTC)", tm.In(UTC)) 56 no("tm.Local()", tm.Local()) 57 no("tm.UTC()", tm.UTC()) 58 no("tm.Round(2)", tm.Round(2)) 59 no("tm.Truncate(2)", tm.Truncate(2)) 60 } 61 62 func TestMonotonicAdd(t *testing.T) { 63 tm := Unix(1486057371, 123456) 64 SetMono(&tm, 123456789012345) 65 66 t2 := tm.Add(1e8) 67 if t2.Nanosecond() != 100123456 { 68 t.Errorf("t2.Nanosecond() = %d, want 100123456", t2.Nanosecond()) 69 } 70 if GetMono(&t2) != 123456889012345 { 71 t.Errorf("t2.mono = %d, want 123456889012345", GetMono(&t2)) 72 } 73 74 t3 := tm.Add(-9e18) // wall now out of range 75 if t3.Nanosecond() != 123456 { 76 t.Errorf("t3.Nanosecond() = %d, want 123456", t3.Nanosecond()) 77 } 78 if GetMono(&t3) != 0 { 79 t.Errorf("t3.mono = %d, want 0 (wall time out of range for monotonic reading)", GetMono(&t3)) 80 } 81 82 t4 := tm.Add(+9e18) // wall now out of range 83 if t4.Nanosecond() != 123456 { 84 t.Errorf("t4.Nanosecond() = %d, want 123456", t4.Nanosecond()) 85 } 86 if GetMono(&t4) != 0 { 87 t.Errorf("t4.mono = %d, want 0 (wall time out of range for monotonic reading)", GetMono(&t4)) 88 } 89 90 tn := Now() 91 tn1 := tn.Add(1 * Hour) 92 Sleep(100 * Millisecond) 93 d := Until(tn1) 94 if d < 59*Minute { 95 t.Errorf("Until(Now().Add(1*Hour)) = %v, wanted at least 59m", d) 96 } 97 now := Now() 98 if now.After(tn1) { 99 t.Errorf("Now().After(Now().Add(1*Hour)) = true, want false") 100 } 101 if !tn1.After(now) { 102 t.Errorf("Now().Add(1*Hour).After(now) = false, want true") 103 } 104 if tn1.Before(now) { 105 t.Errorf("Now().Add(1*Hour).Before(Now()) = true, want false") 106 } 107 if !now.Before(tn1) { 108 t.Errorf("Now().Before(Now().Add(1*Hour)) = false, want true") 109 } 110 } 111 112 func TestMonotonicSub(t *testing.T) { 113 t1 := Unix(1483228799, 995e6) 114 SetMono(&t1, 123456789012345) 115 116 t2 := Unix(1483228799, 5e6) 117 SetMono(&t2, 123456789012345+10e6) 118 119 t3 := Unix(1483228799, 995e6) 120 SetMono(&t3, 123456789012345+1e9) 121 122 t1w := t1.AddDate(0, 0, 0) 123 if GetMono(&t1w) != 0 { 124 t.Fatalf("AddDate didn't strip monotonic clock reading") 125 } 126 t2w := t2.AddDate(0, 0, 0) 127 if GetMono(&t2w) != 0 { 128 t.Fatalf("AddDate didn't strip monotonic clock reading") 129 } 130 t3w := t3.AddDate(0, 0, 0) 131 if GetMono(&t3w) != 0 { 132 t.Fatalf("AddDate didn't strip monotonic clock reading") 133 } 134 135 sub := func(txs, tys string, tx, txw, ty, tyw Time, d, dw Duration) { 136 check := func(expr string, d, want Duration) { 137 if d != want { 138 t.Errorf("%s = %v, want %v", expr, d, want) 139 } 140 } 141 check(txs+".Sub("+tys+")", tx.Sub(ty), d) 142 check(txs+"w.Sub("+tys+")", txw.Sub(ty), dw) 143 check(txs+".Sub("+tys+"w)", tx.Sub(tyw), dw) 144 check(txs+"w.Sub("+tys+"w)", txw.Sub(tyw), dw) 145 } 146 sub("t1", "t1", t1, t1w, t1, t1w, 0, 0) 147 sub("t1", "t2", t1, t1w, t2, t2w, -10*Millisecond, 990*Millisecond) 148 sub("t1", "t3", t1, t1w, t3, t3w, -1000*Millisecond, 0) 149 150 sub("t2", "t1", t2, t2w, t1, t1w, 10*Millisecond, -990*Millisecond) 151 sub("t2", "t2", t2, t2w, t2, t2w, 0, 0) 152 sub("t2", "t3", t2, t2w, t3, t3w, -990*Millisecond, -990*Millisecond) 153 154 sub("t3", "t1", t3, t3w, t1, t1w, 1000*Millisecond, 0) 155 sub("t3", "t2", t3, t3w, t2, t2w, 990*Millisecond, 990*Millisecond) 156 sub("t3", "t3", t3, t3w, t3, t3w, 0, 0) 157 158 cmp := func(txs, tys string, tx, txw, ty, tyw Time, c, cw int) { 159 check := func(expr string, b, want bool) { 160 if b != want { 161 t.Errorf("%s = %v, want %v", expr, b, want) 162 } 163 } 164 check(txs+".After("+tys+")", tx.After(ty), c > 0) 165 check(txs+"w.After("+tys+")", txw.After(ty), cw > 0) 166 check(txs+".After("+tys+"w)", tx.After(tyw), cw > 0) 167 check(txs+"w.After("+tys+"w)", txw.After(tyw), cw > 0) 168 169 check(txs+".Before("+tys+")", tx.Before(ty), c < 0) 170 check(txs+"w.Before("+tys+")", txw.Before(ty), cw < 0) 171 check(txs+".Before("+tys+"w)", tx.Before(tyw), cw < 0) 172 check(txs+"w.Before("+tys+"w)", txw.Before(tyw), cw < 0) 173 174 check(txs+".Equal("+tys+")", tx.Equal(ty), c == 0) 175 check(txs+"w.Equal("+tys+")", txw.Equal(ty), cw == 0) 176 check(txs+".Equal("+tys+"w)", tx.Equal(tyw), cw == 0) 177 check(txs+"w.Equal("+tys+"w)", txw.Equal(tyw), cw == 0) 178 } 179 180 cmp("t1", "t1", t1, t1w, t1, t1w, 0, 0) 181 cmp("t1", "t2", t1, t1w, t2, t2w, -1, +1) 182 cmp("t1", "t3", t1, t1w, t3, t3w, -1, 0) 183 184 cmp("t2", "t1", t2, t2w, t1, t1w, +1, -1) 185 cmp("t2", "t2", t2, t2w, t2, t2w, 0, 0) 186 cmp("t2", "t3", t2, t2w, t3, t3w, -1, -1) 187 188 cmp("t3", "t1", t3, t3w, t1, t1w, +1, 0) 189 cmp("t3", "t2", t3, t3w, t2, t2w, +1, +1) 190 cmp("t3", "t3", t3, t3w, t3, t3w, 0, 0) 191 } 192 193 func TestMonotonicOverflow(t *testing.T) { 194 t1 := Now().Add(-30 * Second) 195 d := Until(t1) 196 if d < -35*Second || -30*Second < d { 197 t.Errorf("Until(Now().Add(-30s)) = %v, want roughly -30s (-35s to -30s)", d) 198 } 199 200 t1 = Now().Add(30 * Second) 201 d = Until(t1) 202 if d < 25*Second || 30*Second < d { 203 t.Errorf("Until(Now().Add(-30s)) = %v, want roughly 30s (25s to 30s)", d) 204 } 205 206 t0 := Now() 207 t1 = t0.Add(Duration(1<<63 - 1)) 208 if GetMono(&t1) != 0 { 209 t.Errorf("Now().Add(maxDuration) has monotonic clock reading (%v => %v %d %d)", t0.String(), t1.String(), t0.Unix(), t1.Unix()) 210 } 211 t2 := t1.Add(-Duration(1<<63 - 1)) 212 d = Since(t2) 213 if d < -10*Second || 10*Second < d { 214 t.Errorf("Since(Now().Add(max).Add(-max)) = %v, want [-10s, 10s]", d) 215 } 216 217 t0 = Now() 218 t1 = t0.Add(1 * Hour) 219 Sleep(100 * Millisecond) 220 t2 = Now().Add(-5 * Second) 221 if !t1.After(t2) { 222 t.Errorf("Now().Add(1*Hour).After(Now().Add(-5*Second)) = false, want true\nt1=%v\nt2=%v", t1, t2) 223 } 224 if t2.After(t1) { 225 t.Errorf("Now().Add(-5*Second).After(Now().Add(1*Hour)) = true, want false\nt1=%v\nt2=%v", t1, t2) 226 } 227 if t1.Before(t2) { 228 t.Errorf("Now().Add(1*Hour).Before(Now().Add(-5*Second)) = true, want false\nt1=%v\nt2=%v", t1, t2) 229 } 230 if !t2.Before(t1) { 231 t.Errorf("Now().Add(-5*Second).Before(Now().Add(1*Hour)) = false, want true\nt1=%v\nt2=%v", t1, t2) 232 } 233 } 234 235 var monotonicStringTests = []struct { 236 mono int64 237 want string 238 }{ 239 {0, "m=+0.000000000"}, 240 {123456789, "m=+0.123456789"}, 241 {-123456789, "m=-0.123456789"}, 242 {123456789000, "m=+123.456789000"}, 243 {-123456789000, "m=-123.456789000"}, 244 {9e18, "m=+9000000000.000000000"}, 245 {-9e18, "m=-9000000000.000000000"}, 246 {-1 << 63, "m=-9223372036.854775808"}, 247 } 248 249 func TestMonotonicString(t *testing.T) { 250 t1 := Now() 251 t.Logf("Now() = %v", t1) 252 253 for _, tt := range monotonicStringTests { 254 t1 := Now() 255 SetMono(&t1, tt.mono) 256 s := t1.String() 257 got := s[strings.LastIndex(s, " ")+1:] 258 if got != tt.want { 259 t.Errorf("with mono=%d: got %q; want %q", tt.mono, got, tt.want) 260 } 261 } 262 }