-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.go
30 lines (26 loc) · 951 Bytes
/
clock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Package clock implements a clock interface.
//
// Your application code should use the clock interface to interact with time wherever possible,
// and then it can be easily unit-tested by swapping out the "real" clock with a "simulated"
// one during unit tests via dependency injection techniques.
package clock
import (
"time"
)
// An abstract clock interface to facilitate dependency injection of time.
//
// Implementations must provide thread-safe access to interface methods, to allow
// clocks to be used in background goroutines.
type Clock interface {
// Returns the current time.
Now() time.Time
// Returns a timer that will fire after the given duration from Now().
NewTimer(time.Duration) Timer
}
// This has essentially the same interface as a time.Timer, except C() is a function
// in order to make it a pure interface that we can mock.
type Timer interface {
Reset(time.Duration) bool
Stop() bool
C() <-chan time.Time
}