-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealClock_test.go
More file actions
189 lines (176 loc) · 4.93 KB
/
realClock_test.go
File metadata and controls
189 lines (176 loc) · 4.93 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package clock
import (
"context"
"sync"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func Test_realClock_After(t *testing.T) {
Convey("新建一个 realClock", t, func() {
c := NewRealClock()
Convey("after 应该在规定的时间内,触发", func() {
now := c.Now()
dur := time.Millisecond * 500
delta := dur / 10
after := <-c.After(dur)
So(after, ShouldHappenWithin, dur+delta, now)
})
})
}
func Test_realClock_AfterFunc(t *testing.T) {
Convey("利用 realClock.AfterFunc 生成 *Timer", t, func() {
c := NewRealClock()
secs := time.Second
var mutex sync.Mutex
timer := c.AfterFunc(secs, func() {
mutex.Lock()
})
Convey("立刻停止 timer 的话", func() {
isActive := timer.Stop()
Convey("timer 仍然是活的", func() {
So(isActive, ShouldBeTrue)
})
Convey("func 未被执行,所以,可以上锁", func() {
So(func() {
mutex.Lock()
defer mutex.Unlock()
}, ShouldNotPanic)
})
})
Convey("立刻重置为两倍原来的时候的话", func() {
isActive := timer.Reset(secs * 2)
Convey("timer 仍然是活的", func() {
So(isActive, ShouldBeTrue)
})
Convey("func 未被执行,所以,可以上锁", func() {
So(func() {
mutex.Lock()
defer mutex.Unlock()
}, ShouldNotPanic)
})
Convey("超过 timer 的时限后", func() {
// * 110 / 100 是为了确保 timer 触发
time.Sleep(secs * 2 * 110 / 100)
Convey("func 已执行,此时可以解锁", func() {
So(func() {
mutex.Unlock()
}, ShouldNotPanic)
})
})
})
Convey("超过 timer 的时限后", func() {
// * 110 / 100 是为了确保 timer 触发
time.Sleep(secs * 110 / 100)
Convey("func 已执行,此时可以解锁", func() {
So(func() {
mutex.Unlock()
}, ShouldNotPanic)
})
})
})
}
func Test_realClock_NewTicker(t *testing.T) {
Convey("利用 realClock 创建 ticker", t, func() {
c := NewRealClock()
t := c.NewTicker(time.Second)
Convey("应该是对 time.Ticker 的封装", func() {
So(t.Stop, ShouldEqual, t.ticker.Stop)
})
})
}
func Test_realClock_NewTimer(t *testing.T) {
Convey("利用 realClock 创建 timer", t, func() {
c := NewRealClock()
t := c.NewTimer(time.Second)
Convey("应该是对 time.Timer 的封装", func() {
So(t.C, ShouldEqual, t.timer.C)
So(t.Reset, ShouldEqual, t.timer.Reset)
So(t.Stop, ShouldEqual, t.timer.Stop)
})
})
}
func Test_realClock_Now(t *testing.T) {
Convey("新建一个 realClock", t, func() {
c := NewRealClock()
Convey("realClock.Now() 和 time.Now() 应该返回差不多的时间", func() {
delta := 10 * time.Microsecond
So(c.Now(), ShouldHappenWithin, delta, time.Now())
})
})
}
func Test_realClock_Since(t *testing.T) {
Convey("新建一个 realClock", t, func() {
c := NewRealClock()
expected := time.Second
oneSecondBefore := time.Now().Add(-expected)
actual := c.Since(oneSecondBefore)
Convey("realClock.Since 应该返回 1 秒钟", func() {
So(actual, ShouldAlmostEqual, expected, time.Millisecond)
})
})
}
func Test_realClock_Sleep(t *testing.T) {
Convey("新建一个 realClock", t, func() {
c := NewRealClock()
dur := time.Millisecond * 100
delta := dur / 10
start := time.Now()
c.Sleep(dur)
end := time.Now()
Convey("起止时间,应该差不多就相差了一个 dur", func() {
So(end, ShouldHappenWithin, dur+delta, start)
})
})
}
func Test_realClock_Tick(t *testing.T) {
Convey("新建一个 realClock", t, func() {
c := NewRealClock()
dur := time.Second
delta := dur / 10
start := time.Now()
end := <-c.Tick(dur)
Convey("起止时间,应该差不多就相差了一个 dur", func() {
So(end, ShouldHappenWithin, dur+delta, start)
})
})
}
func Test_realClock_Until(t *testing.T) {
Convey("新建一个 realClock", t, func() {
c := NewRealClock()
expected := time.Second
oneSecondAfter := time.Now().Add(expected)
actual := c.Until(oneSecondAfter)
Convey("realClock.Since 应该返回 1 秒钟", func() {
So(actual, ShouldAlmostEqual, expected, time.Millisecond)
})
})
}
func Test_realClock_ContextWithDeadline(t *testing.T) {
Convey("利用 realClock 创建 context", t, func() {
c := NewRealClock()
dur := time.Millisecond * 200
delta := dur / 10
deadline := time.Now().Add(dur)
ctx, _ := c.ContextWithDeadline(context.Background(), deadline)
<-ctx.Done()
endTime := time.Now()
Convey("应该大约在预定的时间内完成", func() {
So(endTime, ShouldHappenWithin, delta, deadline)
})
})
}
func Test_realClock_ContextWithTimeout(t *testing.T) {
Convey("利用 realClock 创建 context", t, func() {
c := NewRealClock()
timeout := time.Millisecond * 200
delta := timeout / 10
deadline := time.Now().Add(timeout)
ctx, _ := c.ContextWithTimeout(context.Background(), timeout)
<-ctx.Done()
endTime := time.Now()
Convey("应该大约在预定的时间内完成", func() {
So(endTime, ShouldHappenWithin, delta, deadline)
})
})
}