})
}
+// TestMonotonicClock exercises comparing times from within a bubble
+// with ones from outside the bubble.
+func TestMonotonicClock(t *testing.T) {
+ start := time.Now()
+ synctest.Run(func() {
+ time.Sleep(time.Until(start.Round(0)))
+ if got, want := time.Now().In(time.UTC), start.In(time.UTC); !got.Equal(want) {
+ t.Fatalf("time.Now() = %v, want %v", got, want)
+ }
+
+ wait := 1 * time.Second
+ time.Sleep(wait)
+ if got := time.Since(start); got != wait {
+ t.Fatalf("time.Since(start) = %v, want %v", got, wait)
+ }
+ if got := time.Now().Sub(start); got != wait {
+ t.Fatalf("time.Now().Sub(start) = %v, want %v", got, wait)
+ }
+ })
+}
+
func TestRunEmpty(t *testing.T) {
synctest.Run(func() {
})
if sg := getg().syncGroup; sg != nil {
sec = sg.now / (1000 * 1000 * 1000)
nsec = int32(sg.now % (1000 * 1000 * 1000))
- return sec, nsec, sg.now
+ // Don't return a monotonic time inside a synctest bubble.
+ // If we return a monotonic time based on the fake clock,
+ // arithmetic on times created inside/outside bubbles is confusing.
+ // If we return a monotonic time based on the real monotonic clock,
+ // arithmetic on times created in the same bubble is confusing.
+ // Simplest is to omit the monotonic time within a bubble.
+ return sec, nsec, 0
}
return time_now()
}
return nanotime()
}
+//go:linkname time_runtimeIsBubbled time.runtimeIsBubbled
+func time_runtimeIsBubbled() bool {
+ return getg().syncGroup != nil
+}
+
// A timer is a potentially repeating trigger for calling t.f(t.arg, t.seq).
// Timers are allocated by client code, often as part of other data structures.
// Each P has a heap of pointers to timers that it manages.
// Since returns the time elapsed since t.
// It is shorthand for time.Now().Sub(t).
func Since(t Time) Duration {
- if t.wall&hasMonotonic != 0 {
+ if t.wall&hasMonotonic != 0 && !runtimeIsBubbled() {
// Common case optimization: if t has monotonic time, then Sub will use only it.
return subMono(runtimeNano()-startNano, t.ext)
}
// Until returns the duration until t.
// It is shorthand for t.Sub(time.Now()).
func Until(t Time) Duration {
- if t.wall&hasMonotonic != 0 {
+ if t.wall&hasMonotonic != 0 && !runtimeIsBubbled() {
// Common case optimization: if t has monotonic time, then Sub will use only it.
return subMono(t.ext, runtimeNano()-startNano)
}
//go:linkname runtimeNano
func runtimeNano() int64
+//go:linkname runtimeIsBubbled
+func runtimeIsBubbled() bool
+
// Monotonic times are reported as offsets from startNano.
// We initialize startNano to runtimeNano() - 1 so that on systems where
// monotonic time resolution is fairly low (e.g. Windows 2008
}
}
+func BenchmarkSince(b *testing.B) {
+ start := Now()
+ for b.Loop() {
+ u = int64(Since(start))
+ }
+}
+
+func BenchmarkUntil(b *testing.B) {
+ end := Now().Add(1 * Hour)
+ for b.Loop() {
+ u = int64(Until(end))
+ }
+}
+
func BenchmarkFormat(b *testing.B) {
t := Unix(1265346057, 0)
for i := 0; i < b.N; i++ {