...

Source file src/time/mono_test.go

Documentation: time

		 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  }
	 110  
	 111  func TestMonotonicSub(t *testing.T) {
	 112  	t1 := Unix(1483228799, 995e6)
	 113  	SetMono(&t1, 123456789012345)
	 114  
	 115  	t2 := Unix(1483228799, 5e6)
	 116  	SetMono(&t2, 123456789012345+10e6)
	 117  
	 118  	t3 := Unix(1483228799, 995e6)
	 119  	SetMono(&t3, 123456789012345+1e9)
	 120  
	 121  	t1w := t1.AddDate(0, 0, 0)
	 122  	if GetMono(&t1w) != 0 {
	 123  		t.Fatalf("AddDate didn't strip monotonic clock reading")
	 124  	}
	 125  	t2w := t2.AddDate(0, 0, 0)
	 126  	if GetMono(&t2w) != 0 {
	 127  		t.Fatalf("AddDate didn't strip monotonic clock reading")
	 128  	}
	 129  	t3w := t3.AddDate(0, 0, 0)
	 130  	if GetMono(&t3w) != 0 {
	 131  		t.Fatalf("AddDate didn't strip monotonic clock reading")
	 132  	}
	 133  
	 134  	sub := func(txs, tys string, tx, txw, ty, tyw Time, d, dw Duration) {
	 135  		check := func(expr string, d, want Duration) {
	 136  			if d != want {
	 137  				t.Errorf("%s = %v, want %v", expr, d, want)
	 138  			}
	 139  		}
	 140  		check(txs+".Sub("+tys+")", tx.Sub(ty), d)
	 141  		check(txs+"w.Sub("+tys+")", txw.Sub(ty), dw)
	 142  		check(txs+".Sub("+tys+"w)", tx.Sub(tyw), dw)
	 143  		check(txs+"w.Sub("+tys+"w)", txw.Sub(tyw), dw)
	 144  	}
	 145  	sub("t1", "t1", t1, t1w, t1, t1w, 0, 0)
	 146  	sub("t1", "t2", t1, t1w, t2, t2w, -10*Millisecond, 990*Millisecond)
	 147  	sub("t1", "t3", t1, t1w, t3, t3w, -1000*Millisecond, 0)
	 148  
	 149  	sub("t2", "t1", t2, t2w, t1, t1w, 10*Millisecond, -990*Millisecond)
	 150  	sub("t2", "t2", t2, t2w, t2, t2w, 0, 0)
	 151  	sub("t2", "t3", t2, t2w, t3, t3w, -990*Millisecond, -990*Millisecond)
	 152  
	 153  	sub("t3", "t1", t3, t3w, t1, t1w, 1000*Millisecond, 0)
	 154  	sub("t3", "t2", t3, t3w, t2, t2w, 990*Millisecond, 990*Millisecond)
	 155  	sub("t3", "t3", t3, t3w, t3, t3w, 0, 0)
	 156  
	 157  	cmp := func(txs, tys string, tx, txw, ty, tyw Time, c, cw int) {
	 158  		check := func(expr string, b, want bool) {
	 159  			if b != want {
	 160  				t.Errorf("%s = %v, want %v", expr, b, want)
	 161  			}
	 162  		}
	 163  		check(txs+".After("+tys+")", tx.After(ty), c > 0)
	 164  		check(txs+"w.After("+tys+")", txw.After(ty), cw > 0)
	 165  		check(txs+".After("+tys+"w)", tx.After(tyw), cw > 0)
	 166  		check(txs+"w.After("+tys+"w)", txw.After(tyw), cw > 0)
	 167  
	 168  		check(txs+".Before("+tys+")", tx.Before(ty), c < 0)
	 169  		check(txs+"w.Before("+tys+")", txw.Before(ty), cw < 0)
	 170  		check(txs+".Before("+tys+"w)", tx.Before(tyw), cw < 0)
	 171  		check(txs+"w.Before("+tys+"w)", txw.Before(tyw), cw < 0)
	 172  
	 173  		check(txs+".Equal("+tys+")", tx.Equal(ty), c == 0)
	 174  		check(txs+"w.Equal("+tys+")", txw.Equal(ty), cw == 0)
	 175  		check(txs+".Equal("+tys+"w)", tx.Equal(tyw), cw == 0)
	 176  		check(txs+"w.Equal("+tys+"w)", txw.Equal(tyw), cw == 0)
	 177  	}
	 178  
	 179  	cmp("t1", "t1", t1, t1w, t1, t1w, 0, 0)
	 180  	cmp("t1", "t2", t1, t1w, t2, t2w, -1, +1)
	 181  	cmp("t1", "t3", t1, t1w, t3, t3w, -1, 0)
	 182  
	 183  	cmp("t2", "t1", t2, t2w, t1, t1w, +1, -1)
	 184  	cmp("t2", "t2", t2, t2w, t2, t2w, 0, 0)
	 185  	cmp("t2", "t3", t2, t2w, t3, t3w, -1, -1)
	 186  
	 187  	cmp("t3", "t1", t3, t3w, t1, t1w, +1, 0)
	 188  	cmp("t3", "t2", t3, t3w, t2, t2w, +1, +1)
	 189  	cmp("t3", "t3", t3, t3w, t3, t3w, 0, 0)
	 190  }
	 191  
	 192  func TestMonotonicOverflow(t *testing.T) {
	 193  	t1 := Now().Add(-30 * Second)
	 194  	d := Until(t1)
	 195  	if d < -35*Second || -30*Second < d {
	 196  		t.Errorf("Until(Now().Add(-30s)) = %v, want roughly -30s (-35s to -30s)", d)
	 197  	}
	 198  
	 199  	t1 = Now().Add(30 * Second)
	 200  	d = Until(t1)
	 201  	if d < 25*Second || 30*Second < d {
	 202  		t.Errorf("Until(Now().Add(-30s)) = %v, want roughly 30s (25s to 30s)", d)
	 203  	}
	 204  
	 205  	t0 := Now()
	 206  	t1 = t0.Add(Duration(1<<63 - 1))
	 207  	if GetMono(&t1) != 0 {
	 208  		t.Errorf("Now().Add(maxDuration) has monotonic clock reading (%v => %v %d %d)", t0.String(), t1.String(), t0.Unix(), t1.Unix())
	 209  	}
	 210  	t2 := t1.Add(-Duration(1<<63 - 1))
	 211  	d = Since(t2)
	 212  	if d < -10*Second || 10*Second < d {
	 213  		t.Errorf("Since(Now().Add(max).Add(-max)) = %v, want [-10s, 10s]", d)
	 214  	}
	 215  
	 216  	t0 = Now()
	 217  	t1 = t0.Add(1 * Hour)
	 218  	Sleep(100 * Millisecond)
	 219  	t2 = Now().Add(-5 * Second)
	 220  	if !t1.After(t2) {
	 221  		t.Errorf("Now().Add(1*Hour).After(Now().Add(-5*Second)) = false, want true\nt1=%v\nt2=%v", t1, t2)
	 222  	}
	 223  	if t2.After(t1) {
	 224  		t.Errorf("Now().Add(-5*Second).After(Now().Add(1*Hour)) = true, want false\nt1=%v\nt2=%v", t1, t2)
	 225  	}
	 226  	if t1.Before(t2) {
	 227  		t.Errorf("Now().Add(1*Hour).Before(Now().Add(-5*Second)) = true, want false\nt1=%v\nt2=%v", t1, t2)
	 228  	}
	 229  	if !t2.Before(t1) {
	 230  		t.Errorf("Now().Add(-5*Second).Before(Now().Add(1*Hour)) = false, want true\nt1=%v\nt2=%v", t1, t2)
	 231  	}
	 232  }
	 233  
	 234  var monotonicStringTests = []struct {
	 235  	mono int64
	 236  	want string
	 237  }{
	 238  	{0, "m=+0.000000000"},
	 239  	{123456789, "m=+0.123456789"},
	 240  	{-123456789, "m=-0.123456789"},
	 241  	{123456789000, "m=+123.456789000"},
	 242  	{-123456789000, "m=-123.456789000"},
	 243  	{9e18, "m=+9000000000.000000000"},
	 244  	{-9e18, "m=-9000000000.000000000"},
	 245  	{-1 << 63, "m=-9223372036.854775808"},
	 246  }
	 247  
	 248  func TestMonotonicString(t *testing.T) {
	 249  	t1 := Now()
	 250  	t.Logf("Now() = %v", t1)
	 251  
	 252  	for _, tt := range monotonicStringTests {
	 253  		t1 := Now()
	 254  		SetMono(&t1, tt.mono)
	 255  		s := t1.String()
	 256  		got := s[strings.LastIndex(s, " ")+1:]
	 257  		if got != tt.want {
	 258  			t.Errorf("with mono=%d: got %q; want %q", tt.mono, got, tt.want)
	 259  		}
	 260  	}
	 261  }
	 262  

View as plain text