1
0
times/lib.go

66 lines
1.9 KiB
Go
Raw Normal View History

2026-03-04 21:22:12 +01:00
// Package times provides a clock interface with implementations for production and testing code.
2026-03-04 21:11:49 +01:00
package times
import "time"
2026-03-04 21:22:12 +01:00
// Clock defines an interface with primitives for time related functionality.
2026-03-04 21:11:49 +01:00
type Clock interface {
2026-03-04 21:22:12 +01:00
// Now returns the current time.
2026-03-04 21:11:49 +01:00
Now() time.Time
2026-03-04 21:22:12 +01:00
// After returns a channel that can be used to receive the current time once, after the specified
// duration has passed.
2026-03-04 21:11:49 +01:00
After(time.Duration) <-chan time.Time
}
2026-03-04 21:22:12 +01:00
// TestClock is a test implementation of the Clock interface. On top of the Clock methods, it adds the
// possibility to manually step time or to virtually jump to a specified time.
2026-03-04 21:22:35 +01:00
type TestClock struct {
2026-03-04 21:11:49 +01:00
clock test
}
2026-03-04 21:22:12 +01:00
// Sys returns the Clock implementation based on the standard library time functions. Meant to be used with
// production code.
2026-03-04 21:11:49 +01:00
func Sys() Clock {
return sys{}
}
2026-03-04 21:22:12 +01:00
// Test creates a Clock implementation with manual time stepping function. Meant to be used with testing code.
2026-03-04 21:11:49 +01:00
func Test() TestClock {
var zero time.Time
return TestFrom(zero)
}
2026-03-04 21:22:12 +01:00
// TestFrom creates a Clock implementation with manual time stepping function, starting from the specified time.
// Meant to be used with testing code.
2026-03-04 21:11:49 +01:00
func TestFrom(t time.Time) TestClock {
return TestClock{clock: makeTestClock(t)}
}
2026-03-04 21:22:12 +01:00
// Now returns the current test time.
2026-03-04 21:11:49 +01:00
func (c TestClock) Now() time.Time {
return c.clock.now()
}
2026-03-04 21:22:12 +01:00
// After returns a channel that communicates the current time if the specified duration has passed.
2026-03-04 21:11:49 +01:00
func (c TestClock) After(d time.Duration) <-chan time.Time {
return c.clock.after(d)
}
2026-03-04 21:22:12 +01:00
// Pass turns the clock forward by d duration. Negative d is considered as 0.
2026-03-04 21:11:49 +01:00
func (c TestClock) Pass(d time.Duration) {
c.clock.pass(d)
}
2026-03-04 21:22:12 +01:00
// Jump sets the clock to time specified by t. Values of t in the past, relative to TestClock's current time,
// are ignored.
2026-03-04 21:11:49 +01:00
func (c TestClock) Jump(t time.Time) {
c.clock.jump(t)
}
2026-03-05 18:39:54 +01:00
// Waiting tells how many channels are waiting set by After().
func (c TestClock) Waiting() int {
return c.clock.waiting()
}