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