Files

39 lines
731 B
Go

package clock
import (
"sync"
"time"
)
// FakeClock is a controllable Clock for tests. Construct with NewFake; advance
// time with Advance; jump to a specific time with Set. Safe for concurrent use.
type FakeClock struct {
mu sync.Mutex
now time.Time
}
// NewFake returns a FakeClock initialized to t.
func NewFake(t time.Time) *FakeClock {
return &FakeClock{now: t}
}
func (c *FakeClock) Now() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.now
}
// Advance moves the clock forward by d.
func (c *FakeClock) Advance(d time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.now = c.now.Add(d)
}
// Set jumps the clock to t.
func (c *FakeClock) Set(t time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.now = t
}