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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package time_test
import (
"errors"
"fmt"
"runtime"
"sort"
"sync/atomic"
"testing"
. "time"
)
func TestSleep(t *testing.T) {
const delay = 100 * Millisecond
go func() {
Sleep(delay / 2)
Interrupt()
}()
start := Now()
Sleep(delay)
duration := Now().Sub(start)
if duration < delay {
t.Fatalf("Sleep(%s) slept for only %s", delay, duration)
}
}
// Test the basic function calling behavior. Correct queueing
// behavior is tested elsewhere, since After and AfterFunc share
// the same code.
func TestAfterFunc(t *testing.T) {
i := 10
c := make(chan bool)
var f func()
f = func() {
i--
if i >= 0 {
AfterFunc(0, f)
Sleep(1 * Second)
} else {
c <- true
}
}
AfterFunc(0, f)
<-c
}
func TestAfterStress(t *testing.T) {
stop := uint32(0)
go func() {
for atomic.LoadUint32(&stop) == 0 {
runtime.GC()
// Yield so that the OS can wake up the timer thread,
// so that it can generate channel sends for the main goroutine,
// which will eventually set stop = 1 for us.
Sleep(Nanosecond)
}
}()
c := Tick(1)
for i := 0; i < 100; i++ {
<-c
}
atomic.StoreUint32(&stop, 1)
}
func BenchmarkAfterFunc(b *testing.B) {
i := b.N
c := make(chan bool)
var f func()
f = func() {
i--
if i >= 0 {
AfterFunc(0, f)
} else {
c <- true
}
}
AfterFunc(0, f)
<-c
}
func BenchmarkAfter(b *testing.B) {
for i := 0; i < b.N; i++ {
<-After(1)
}
}
func BenchmarkStop(b *testing.B) {
for i := 0; i < b.N; i++ {
NewTimer(1 * Second).Stop()
}
}
func TestAfter(t *testing.T) {
const delay = 100 * Millisecond
start := Now()
end := <-After(delay)
if duration := Now().Sub(start); duration < delay {
t.Fatalf("After(%s) slept for only %d ns", delay, duration)
}
if min := start.Add(delay); end.Before(min) {
t.Fatalf("After(%s) expect >= %s, got %s", delay, min, end)
}
}
func TestAfterTick(t *testing.T) {
const Count = 10
Delta := 100 * Millisecond
if testing.Short() {
Delta = 10 * Millisecond
}
t0 := Now()
for i := 0; i < Count; i++ {
<-After(Delta)
}
t1 := Now()
d := t1.Sub(t0)
target := Delta * Count
if d < target*9/10 {
t.Fatalf("%d ticks of %s too fast: took %s, expected %s", Count, Delta, d, target)
}
if !testing.Short() && d > target*30/10 {
t.Fatalf("%d ticks of %s too slow: took %s, expected %s", Count, Delta, d, target)
}
}
func TestAfterStop(t *testing.T) {
AfterFunc(100*Millisecond, func() {})
t0 := NewTimer(50 * Millisecond)
c1 := make(chan bool, 1)
t1 := AfterFunc(150*Millisecond, func() { c1 <- true })
c2 := After(200 * Millisecond)
if !t0.Stop() {
t.Fatalf("failed to stop event 0")
}
if !t1.Stop() {
t.Fatalf("failed to stop event 1")
}
<-c2
select {
case <-t0.C:
t.Fatalf("event 0 was not stopped")
case <-c1:
t.Fatalf("event 1 was not stopped")
default:
}
if t1.Stop() {
t.Fatalf("Stop returned true twice")
}
}
func TestAfterQueuing(t *testing.T) {
// This test flakes out on some systems,
// so we'll try it a few times before declaring it a failure.
const attempts = 3
err := errors.New("!=nil")
for i := 0; i < attempts && err != nil; i++ {
if err = testAfterQueuing(t); err != nil {
t.Logf("attempt %v failed: %v", i, err)
}
}
if err != nil {
t.Fatal(err)
}
}
// For gccgo omit 0 for now because it can take too long to start the
var slots = []int{5, 3, 6, 6, 6, 1, 1, 2, 7, 9, 4, 8 /*0*/}
type afterResult struct {
slot int
t Time
}
func await(slot int, result chan<- afterResult, ac <-chan Time) {
result <- afterResult{slot, <-ac}
}
func testAfterQueuing(t *testing.T) error {
Delta := 100 * Millisecond
if testing.Short() {
Delta = 20 * Millisecond
}
// make the result channel buffered because we don't want
// to depend on channel queueing semantics that might
// possibly change in the future.
result := make(chan afterResult, len(slots))
t0 := Now()
for _, slot := range slots {
go await(slot, result, After(Duration(slot)*Delta))
}
sort.Ints(slots)
for _, slot := range slots {
r := <-result
if r.slot != slot {
return fmt.Errorf("after slot %d, expected %d", r.slot, slot)
}
dt := r.t.Sub(t0)
target := Duration(slot) * Delta
if dt < target-Delta/2 || dt > target+Delta*10 {
return fmt.Errorf("After(%s) arrived at %s, expected [%s,%s]", target, dt, target-Delta/2, target+Delta*10)
}
}
return nil
}
func TestTimerStopStress(t *testing.T) {
if testing.Short() {
return
}
for i := 0; i < 100; i++ {
go func(i int) {
timer := AfterFunc(2*Second, func() {
t.Fatalf("timer %d was not stopped", i)
})
Sleep(1 * Second)
timer.Stop()
}(i)
}
Sleep(3 * Second)
}
func TestSleepZeroDeadlock(t *testing.T) {
// Sleep(0) used to hang, the sequence of events was as follows.
// Sleep(0) sets G's status to Gwaiting, but then immediately returns leaving the status.
// Then the goroutine calls e.g. new and falls down into the scheduler due to pending GC.
// After the GC nobody wakes up the goroutine from Gwaiting status.
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
c := make(chan bool)
go func() {
for i := 0; i < 100; i++ {
runtime.GC()
}
c <- true
}()
for i := 0; i < 100; i++ {
Sleep(0)
tmp := make(chan bool, 1)
tmp <- true
<-tmp
}
<-c
}
func testReset(d Duration) error {
t0 := NewTimer(2 * d)
Sleep(d)
if t0.Reset(3*d) != true {
return errors.New("resetting unfired timer returned false")
}
Sleep(2 * d)
select {
case <-t0.C:
return errors.New("timer fired early")
default:
}
Sleep(2 * d)
select {
case <-t0.C:
default:
return errors.New("reset timer did not fire")
}
if t0.Reset(50*Millisecond) != false {
return errors.New("resetting expired timer returned true")
}
return nil
}
func TestReset(t *testing.T) {
// We try to run this test with increasingly larger multiples
// until one works so slow, loaded hardware isn't as flaky,
// but without slowing down fast machines unnecessarily.
const unit = 25 * Millisecond
tries := []Duration{
1 * unit,
3 * unit,
7 * unit,
15 * unit,
}
var err error
for _, d := range tries {
err = testReset(d)
if err == nil {
t.Logf("passed using duration %v", d)
return
}
}
t.Error(err)
}
|