-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
48 lines (39 loc) · 930 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package job
import (
"sync"
// This library abstracts the time functionality of the OS so that it can be controlled during unit tests.
// It was selected over thejerf/abtime because abtime is geared towards precision timing rather than scheduling.
// Other libraries were reviewed and rejected due to outstanding bugs.
"github.com/mixer/clock"
)
type Clock struct {
clock.Clock
lock sync.RWMutex
}
func (clk *Clock) SetClock(in clock.Clock) {
clk.lock.Lock()
defer clk.lock.Unlock()
clk.Clock = in
}
func (clk *Clock) Time() clock.Clock {
clk.lock.RLock()
defer clk.lock.RUnlock()
if clk.Clock == nil {
clk.lock.RUnlock()
clk.lock.Lock()
clk.Clock = clock.C
clk.lock.Unlock()
clk.lock.RLock()
}
return clk.Clock
}
func (clk *Clock) TimeSet() (result bool) {
clk.lock.RLock()
result = clk.Clock != nil
clk.lock.RUnlock()
return
}
type Clocker interface {
Time() clock.Clock
TimeSet() bool
}