-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker.go
51 lines (38 loc) · 826 Bytes
/
ticker.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
package horus
import "time"
// TimeTicker defines a ticker interface
type TimeTicker interface {
Stop()
Chan() <-chan time.Time
}
// Ticker wraps time.Ticker to be TimeTicker complaint
type Ticker struct {
*time.Ticker
}
var _ TimeTicker = (*Ticker)(nil)
func NewTicker(d time.Duration) *Ticker {
return &Ticker{time.NewTicker(d)}
}
func (t *Ticker) Chan() <-chan time.Time {
return t.C
}
// TestTicker provides control over a ticker. To be used only on tests.
type TestTicker struct {
c chan time.Time
}
func NewTestTicker() *TestTicker {
return &TestTicker{
c: make(chan time.Time),
}
}
func (t *TestTicker) Chan() <-chan time.Time {
return t.c
}
func (t *TestTicker) Stop() {
}
func (t *TestTicker) Tick() time.Time {
now := time.Now()
t.c <- now
time.Sleep(100 * time.Millisecond)
return now
}