-
Notifications
You must be signed in to change notification settings - Fork 0
/
timejump.go
89 lines (79 loc) · 1.69 KB
/
timejump.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
// Package timejump allows you to mock `time.Now`.
package timejump
import (
"sync"
"time"
)
var (
active bool
activeMu sync.Mutex
traveledTime *time.Time
traveledAt time.Time
timeScale int
location *time.Location
)
// Activate enables timejump's mocking functionality.
// Call this at the top of test functions.
func Activate() {
activeMu.Lock()
active = true
traveledTime = nil
traveledAt = time.Time{}
timeScale = 1
location = nil
}
// Deactivate disables timejump's mocking functionality.
// Call this at the bottom of test functions (or use `defer` at the top).
func Deactivate() {
active = false
traveledTime = nil
traveledAt = time.Time{}
timeScale = 1
location = nil
activeMu.Unlock()
}
// Move sets the location of the time generated by `Now`.
func Move(loc *time.Location) {
checkActive()
location = loc
}
// Jump jumps to the time t.
func Jump(t time.Time) {
checkActive()
traveledAt = time.Now()
traveledTime = &t
}
// Scale sets the scale of the world speed.
func Scale(n int) {
checkActive()
now := Now()
timeScale = n
traveledTime = &now
traveledAt = now
}
// Stop stops the world (equal to `Scale(0)`).
func Stop() {
Scale(0)
}
// Now returns the current time.
// If timejump is activated, it returns fake time calculated based on fake values set by `Jump`, `Move`, and `Scale`.
func Now() time.Time {
if !active {
return time.Now()
}
var t time.Time
if traveledTime != nil {
t = traveledTime.Add(time.Now().Sub(traveledAt) * time.Duration(timeScale))
} else {
t = time.Now()
}
if location != nil {
t = t.In(location)
}
return t
}
func checkActive() {
if !active {
panic("timegop is not activated; call timegop.Activate() at first")
}
}