-
Notifications
You must be signed in to change notification settings - Fork 50
/
fake_ticker.go
93 lines (74 loc) · 1.81 KB
/
fake_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package clock
import (
"errors"
"sync"
"time"
)
type fakeTicker struct {
clock *fakeClock
c chan time.Time
lck sync.Mutex
remaining time.Duration
duration time.Duration
}
func (f *fakeClock) NewTicker(d time.Duration) Ticker {
// replicate the panic from time.NewTicker
if d <= 0 {
panic(errors.New("non-positive interval for NewTicker"))
}
f.lck.Lock()
defer f.lck.Unlock()
ticker := &fakeTicker{
clock: f,
// important: the channel needs to be buffered with at least one element capacity, otherwise you can't advance
// the clock and read the channel in the same thread without hanging.
c: make(chan time.Time, 1),
remaining: d,
duration: d,
}
f.tickers = append(f.tickers, ticker)
f.blockOnTickers = f.notifyBlockers(f.blockOnTickers, f.waitingTickers())
return ticker
}
func (f *fakeTicker) Stop() {
f.lck.Lock()
defer f.lck.Unlock()
f.remaining = 0
// mark ticker as disabled
f.duration = 0
}
func (f *fakeTicker) Chan() <-chan time.Time {
return f.c
}
func (f *fakeTicker) Reset(d time.Duration) {
if d <= 0 {
panic(errors.New("non-positive interval for Reset"))
}
f.lck.Lock()
f.remaining = d
f.duration = d
f.lck.Unlock()
f.clock.lck.Lock()
defer f.clock.lck.Unlock()
f.clock.blockOnTickers = f.clock.notifyBlockers(f.clock.blockOnTickers, f.clock.waitingTickers())
}
func (f *fakeTicker) advance(t time.Time, d time.Duration) {
f.lck.Lock()
defer f.lck.Unlock()
if f.duration == 0 {
// ticker was stopped
return
}
if f.remaining > d {
f.remaining -= d
return
}
f.remaining = f.duration
// similar to a real timer, empty the output channel before writing the new value to the channel (to avoid hanging
// the current go routine should the ticker expire a second time before it was read)
select {
case <-f.c:
default:
}
f.c <- t
}