forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.go
65 lines (50 loc) · 1.08 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
package utils
import (
"sync"
"time"
)
type Clock interface {
Now() time.Time
After(d time.Duration) <-chan time.Time
Sleep(d time.Duration)
}
type RealClock struct{}
func (self RealClock) Sleep(d time.Duration) {
time.Sleep(d)
}
func (self RealClock) After(d time.Duration) <-chan time.Time {
return time.After(d)
}
func (self RealClock) Now() time.Time {
return time.Now()
}
type MockClock struct {
MockNow time.Time
duration time.Duration
}
func (self MockClock) Now() time.Time {
return self.MockNow
}
func (self MockClock) After(d time.Duration) <-chan time.Time {
return time.After(self.duration)
}
func (self MockClock) Sleep(d time.Duration) {
time.Sleep(self.duration)
}
// A clock that increments each time someone calls Now()
type IncClock struct {
mu sync.Mutex
NowTime int64
}
func (self *IncClock) Now() time.Time {
self.mu.Lock()
defer self.mu.Unlock()
self.NowTime++
return time.Unix(self.NowTime, 0)
}
func (self *IncClock) After(d time.Duration) <-chan time.Time {
return time.After(0)
}
func (self *IncClock) Sleep(d time.Duration) {
time.Sleep(0)
}