-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker.go
76 lines (64 loc) · 1.46 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package clock
import (
"sync"
"time"
)
// Ticker is an abstraction for the type time.Ticker that can be mocked for tests.
type Ticker interface {
// Chan returns the readonly channel of the ticker
Chan() <-chan time.Time
// Stop stops the ticker. No more events will be sent through the channel
Stop()
}
// realTicker is just the type time.Ticker and implements the Ticker interface.
type realTicker struct {
*time.Ticker
}
// Chan returns the readonly channel of the ticker
func (r *realTicker) Chan() <-chan time.Time {
return r.C
}
// fakeTicker is a fake implementation of Ticker based on the time mocking in Mock.
type fakeTicker struct {
mu sync.RWMutex
ch chan time.Time
clock *Mock
d time.Duration
next time.Time
stopped bool
}
// Chan returns the readonly channel of the ticker.
func (f *fakeTicker) Chan() <-chan time.Time {
return f.ch
}
// Stop stops the ticker. No more events will be sent through the channel
func (f *fakeTicker) Stop() {
f.mu.Lock()
f.stopped = true
f.mu.Unlock()
f.clock.removeTimer(f)
}
// Execute executes the Ticker
func (f *fakeTicker) Execute(t time.Time) {
f.mu.RLock()
next := f.next
stopped := f.stopped
f.mu.RUnlock()
if stopped {
return
}
f.mu.Lock()
f.next = next.Add(f.d)
f.mu.Unlock()
select {
case f.ch <- next:
default:
}
sched()
}
// NextExecution returns the next execution time
func (f *fakeTicker) NextExecution() time.Time {
f.mu.RLock()
defer f.mu.RUnlock()
return f.next
}