forked from bluele/gcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
53 lines (41 loc) · 800 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
49
50
51
52
53
package gcache
import (
"sync"
"time"
)
type Clock interface {
Now() time.Time
}
type RealClock struct{}
func NewRealClock() Clock {
return RealClock{}
}
func (rc RealClock) Now() time.Time {
t := time.Now()
return t
}
type FakeClock interface {
Clock
Advance(d time.Duration)
}
func NewFakeClock() FakeClock {
return &fakeclock{
// Taken from github.com/jonboulle/clockwork: use a fixture that does not fulfill Time.IsZero()
now: time.Date(1984, time.April, 4, 0, 0, 0, 0, time.UTC),
}
}
type fakeclock struct {
now time.Time
mutex sync.RWMutex
}
func (fc *fakeclock) Now() time.Time {
fc.mutex.RLock()
defer fc.mutex.RUnlock()
t := fc.now
return t
}
func (fc *fakeclock) Advance(d time.Duration) {
fc.mutex.Lock()
defer fc.mutex.Unlock()
fc.now = fc.now.Add(d)
}