This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
forked from ssoroka/ttime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttime_test.go
144 lines (131 loc) · 2.82 KB
/
ttime_test.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package ttime
import (
// "github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestFreezingTime(t *testing.T) {
// freeze time at a specific date/time (eg, test leap-year support!):
now, err := time.Parse(time.RFC3339, "2012-02-29T00:00:00Z")
if err != nil {
panic("date time parse failed")
}
Freeze(Time(now))
if !IsFrozen() {
t.Error("Time should be frozen here, and was not.")
}
if Now().UTC() != Time(now) {
t.Error("Time should still be set to frozen time")
}
t.Logf("It is now %v (frozen)", Now().UTC())
Unfreeze()
if Now().UTC() == Time(now) || IsFrozen() {
t.Error("Time should no longer be frozen")
}
t.Logf("It is now %v (not frozen)", Now().UTC())
}
func TestAfterFreezeMode(t *testing.T) {
// test frozen functionality.
start := Now()
Freeze(Now())
<-After(10 * Millisecond)
Unfreeze()
elapsed := Now().Sub(start)
t.Logf("Took %v", elapsed)
if elapsed >= 1*Millisecond {
t.Error("Took too long")
}
}
func TestAfterNoFreeze(t *testing.T) {
// test original functionality is restored
start := Now()
<-After(1 * Millisecond)
elapsed := Now().Sub(start)
t.Logf("Took %v", elapsed)
if elapsed > 2*Millisecond {
t.Error("Took too long")
}
if elapsed < 1*Millisecond {
t.Error("Went too fast")
}
}
func TestTickFreezeMode(t *testing.T) {
// TODO: this leaks tickers
// test frozen functionality.
start := Now()
Freeze(start)
c := Tick(10 * Millisecond)
<-c
<-c
<-c
if !start.Add(40 * Millisecond).Equal(Now()) {
t.Errorf("Expected Tick to advance clock, did not. %v != %v", start.Add(40*Millisecond), Now())
}
Unfreeze()
elapsed := Now().Sub(start)
t.Logf("Took %v", elapsed)
if elapsed >= 1*Millisecond {
t.Error("Took too long")
}
}
func TestTickNoFreeze(t *testing.T) {
// TODO: this leaks tickers
// test original functionality is restored
start := Now()
c := Tick(1 * Millisecond)
<-c
<-c
<-c
elapsed := Now().Sub(start)
t.Logf("Took %v", elapsed)
if elapsed > 4*Millisecond {
t.Error("Took too long")
}
if elapsed < 3*Millisecond {
t.Error("Went too fast")
}
}
func TestSleepNoFreeze(t *testing.T) {
start := Now()
Sleep(1 * Millisecond)
elapsed := Now().Sub(start)
if elapsed > 2*Millisecond {
t.Error("Took too long")
}
if elapsed < 1*Millisecond {
t.Error("Went too fast")
}
}
func TestSleepFreezeMode(t *testing.T) {
start := Now()
Freeze(start)
Sleep(1 * Second)
Unfreeze()
elapsed := Now().Sub(start)
if elapsed > 1*Millisecond {
t.Error("Took too long")
}
}
/*
func TestSleepFreezeModeSleepMath(t *testing.T) {
start := Now()
Freeze(start)
sleeper1 := make(chan Time)
sleeper2 := make(chan Time)
go func() {
Sleep(200)
Sleep(200)
Sleep(200)
sleeper1<- Now()
}()
go func() {
Sleep(300)
Sleep(300)
sleeper2<- Now()
}()
sl1, sl2 := <-sleeper1,<-sleeper2
if sl1!=sl2 {
t.Errorf("Sleep is broken: %v, %v", sl1, sl2)
}
}
*/