-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.go
70 lines (58 loc) · 1.33 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package clock
import (
"time"
)
// Clock is interface for time operation, so time sensitive application
// can be easily unit tested by mocking time.
type Clock interface {
Now() time.Time
NewTicker(d time.Duration) *Ticker
NewTimer(d time.Duration) *Timer
}
// Timerable is interface for [time.Timer].
type Timerable interface {
Reset(d time.Duration) bool
Stop() bool
}
// Tickerable is interface for [time.Ticker].
type Tickerable interface {
Reset(d time.Duration)
Stop()
}
// Timer is [time.Timer] drop-in replacement.
type Timer struct {
// The real Timer implementation
Timerable
// The channel on which the timer result are delivered.
C <-chan time.Time
}
// Ticker is [time.Ticker] drop-in replacement.
type Ticker struct {
// The real Ticker implementation
Tickerable
// The channel on which the ticks are delivered.
C <-chan time.Time
}
// ===================================================================
type clock struct{}
// New returns a new real-time Clock.
func New() Clock {
return &clock{}
}
func (c *clock) Now() time.Time {
return time.Now()
}
func (c *clock) NewTimer(d time.Duration) *Timer {
t := time.NewTimer(d)
return &Timer{
Timerable: t,
C: t.C,
}
}
func (c *clock) NewTicker(d time.Duration) *Ticker {
t := time.NewTicker(d)
return &Ticker{
Tickerable: t,
C: t.C,
}
}