| package fxtime_test |
| |
| import ( |
| "testing" |
| "time" |
| |
| "fuchsia.googlesource.com/infra/infra/fxtime" |
| ) |
| |
| func TestFixedClock(t *testing.T) { |
| t.Run("Now should return a fixed time", func(t *testing.T) { |
| now := time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC) |
| clock := fxtime.NewFixedClock(now) |
| expectTime(t, now, clock.Now()) |
| expectTime(t, now, clock.Now()) |
| }) |
| } |
| |
| func TestStopwatch(t *testing.T) { |
| now := time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC) |
| |
| t.Run("Restart should return the current time", func(t *testing.T) { |
| clock := &TestClock{ |
| nowTimes: []time.Time{now, now.Add(time.Hour)}, |
| } |
| stopwatch := fxtime.NewStopwatch(clock) |
| expectTime(t, clock.nowTimes[1], stopwatch.Restart()) |
| }) |
| |
| t.Run("Elapsed should return the duration since Restart() was last called", |
| func(t *testing.T) { |
| clock := &TestClock{ |
| nowTimes: []time.Time{ |
| now, |
| now.Add(time.Hour), |
| now.Add(24 * time.Hour), |
| now.Add(72 * time.Hour), |
| }, |
| } |
| stopwatch := fxtime.NewStopwatch(clock) |
| expectTime(t, clock.nowTimes[1], stopwatch.Restart()) |
| duration := clock.nowTimes[2].Sub(clock.nowTimes[1]) |
| expectDuration(t, duration, stopwatch.Elapsed()) |
| duration = clock.nowTimes[3].Sub(clock.nowTimes[1]) |
| expectDuration(t, duration, stopwatch.Elapsed()) |
| }) |
| } |
| |
| func expectTime(t *testing.T, expected, actual time.Time) { |
| if expected != actual { |
| t.Fatalf("expected time %v but got %v", expected, actual) |
| } |
| } |
| |
| func expectDuration(t *testing.T, expected, actual time.Duration) { |
| if expected != actual { |
| t.Fatalf("expected duration %v but got %v", expected, actual) |
| } |
| } |
| |
| // TestClock is used to test Stopwatch. |
| type TestClock struct { |
| // List of Times to return when Now is called. |
| nowTimes []time.Time |
| // Current index in nowTimes |
| i uint |
| } |
| |
| // Returns the current time. This will panic if called more than len(nowTimes) times. |
| func (c *TestClock) Now() time.Time { |
| t := c.nowTimes[c.i] |
| c.i += 1 |
| return t |
| } |