1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 func TestSleep(t
*testing
.T
) {
19 const delay
= 100 * Millisecond
26 duration
:= Now().Sub(start
)
28 t
.Fatalf("Sleep(%s) slept for only %s", delay
, duration
)
32 // Test the basic function calling behavior. Correct queueing
33 // behavior is tested elsewhere, since After and AfterFunc share
35 func TestAfterFunc(t
*testing
.T
) {
53 func TestAfterStress(t
*testing
.T
) {
56 for atomic
.LoadUint32(&stop
) == 0 {
58 // Yield so that the OS can wake up the timer thread,
59 // so that it can generate channel sends for the main goroutine,
60 // which will eventually set stop = 1 for us.
64 ticker
:= NewTicker(1)
65 for i
:= 0; i
< 100; i
++ {
69 atomic
.StoreUint32(&stop
, 1)
72 func benchmark(b
*testing
.B
, bench
func(n
int)) {
73 garbage
:= make([]*Timer
, 1<<17)
74 for i
:= 0; i
< len(garbage
); i
++ {
75 garbage
[i
] = AfterFunc(Hour
, nil)
79 P
:= runtime
.GOMAXPROCS(-1)
80 N
:= int32(b
.N
/ batch
)
87 for p
:= 0; p
< P
; p
++ {
89 for atomic
.AddInt32(&N
, -1) >= 0 {
99 for i
:= 0; i
< len(garbage
); i
++ {
104 func BenchmarkAfterFunc(b
*testing
.B
) {
105 benchmark(b
, func(n
int) {
122 func BenchmarkAfter(b
*testing
.B
) {
123 benchmark(b
, func(n
int) {
124 for i
:= 0; i
< n
; i
++ {
130 func BenchmarkStop(b
*testing
.B
) {
131 benchmark(b
, func(n
int) {
132 for i
:= 0; i
< n
; i
++ {
133 NewTimer(1 * Second
).Stop()
138 func BenchmarkSimultaneousAfterFunc(b
*testing
.B
) {
139 benchmark(b
, func(n
int) {
140 var wg sync
.WaitGroup
142 for i
:= 0; i
< n
; i
++ {
143 AfterFunc(0, wg
.Done
)
149 func BenchmarkStartStop(b
*testing
.B
) {
150 benchmark(b
, func(n
int) {
151 timers
:= make([]*Timer
, n
)
152 for i
:= 0; i
< n
; i
++ {
153 timers
[i
] = AfterFunc(Hour
, nil)
156 for i
:= 0; i
< n
; i
++ {
162 func TestAfter(t
*testing
.T
) {
163 const delay
= 100 * Millisecond
165 end
:= <-After(delay
)
166 if duration
:= Now().Sub(start
); duration
< delay
{
167 t
.Fatalf("After(%s) slept for only %d ns", delay
, duration
)
169 if min
:= start
.Add(delay
); end
.Before(min
) {
170 t
.Fatalf("After(%s) expect >= %s, got %s", delay
, min
, end
)
174 func TestAfterTick(t
*testing
.T
) {
176 Delta
:= 100 * Millisecond
178 Delta
= 10 * Millisecond
181 for i
:= 0; i
< Count
; i
++ {
186 target
:= Delta
* Count
188 t
.Fatalf("%d ticks of %s too fast: took %s, expected %s", Count
, Delta
, d
, target
)
190 if !testing
.Short() && d
> target
*30/10 {
191 t
.Fatalf("%d ticks of %s too slow: took %s, expected %s", Count
, Delta
, d
, target
)
195 func TestAfterStop(t
*testing
.T
) {
196 AfterFunc(100*Millisecond
, func() {})
197 t0
:= NewTimer(50 * Millisecond
)
198 c1
:= make(chan bool, 1)
199 t1
:= AfterFunc(150*Millisecond
, func() { c1
<- true })
200 c2
:= After(200 * Millisecond
)
202 t
.Fatalf("failed to stop event 0")
205 t
.Fatalf("failed to stop event 1")
210 t
.Fatalf("event 0 was not stopped")
212 t
.Fatalf("event 1 was not stopped")
216 t
.Fatalf("Stop returned true twice")
220 func TestAfterQueuing(t
*testing
.T
) {
221 // This test flakes out on some systems,
222 // so we'll try it a few times before declaring it a failure.
224 err
:= errors
.New("!=nil")
225 for i
:= 0; i
< attempts
&& err
!= nil; i
++ {
226 if err
= testAfterQueuing(t
); err
!= nil {
227 t
.Logf("attempt %v failed: %v", i
, err
)
235 // For gccgo omit 0 for now because it can take too long to start the
236 var slots
= []int{5, 3, 6, 6, 6, 1, 1, 2, 7, 9, 4, 8 /*0*/}
238 type afterResult
struct {
243 func await(slot
int, result
chan<- afterResult
, ac
<-chan Time
) {
244 result
<- afterResult
{slot
, <-ac
}
247 func testAfterQueuing(t
*testing
.T
) error
{
248 Delta
:= 100 * Millisecond
250 Delta
= 20 * Millisecond
252 // make the result channel buffered because we don't want
253 // to depend on channel queueing semantics that might
254 // possibly change in the future.
255 result
:= make(chan afterResult
, len(slots
))
258 for _
, slot
:= range slots
{
259 go await(slot
, result
, After(Duration(slot
)*Delta
))
262 for _
, slot
:= range slots
{
265 return fmt
.Errorf("after slot %d, expected %d", r
.slot
, slot
)
268 target
:= Duration(slot
) * Delta
269 if dt
< target
-Delta
/2 || dt
> target
+Delta
*10 {
270 return fmt
.Errorf("After(%s) arrived at %s, expected [%s,%s]", target
, dt
, target
-Delta
/2, target
+Delta
*10)
276 func TestTimerStopStress(t
*testing
.T
) {
280 for i
:= 0; i
< 100; i
++ {
282 timer
:= AfterFunc(2*Second
, func() {
283 t
.Fatalf("timer %d was not stopped", i
)
292 func TestSleepZeroDeadlock(t
*testing
.T
) {
293 // Sleep(0) used to hang, the sequence of events was as follows.
294 // Sleep(0) sets G's status to Gwaiting, but then immediately returns leaving the status.
295 // Then the goroutine calls e.g. new and falls down into the scheduler due to pending GC.
296 // After the GC nobody wakes up the goroutine from Gwaiting status.
297 defer runtime
.GOMAXPROCS(runtime
.GOMAXPROCS(4))
300 for i
:= 0; i
< 100; i
++ {
305 for i
:= 0; i
< 100; i
++ {
307 tmp
:= make(chan bool, 1)
314 func testReset(d Duration
) error
{
315 t0
:= NewTimer(2 * d
)
317 if t0
.Reset(3*d
) != true {
318 return errors
.New("resetting unfired timer returned false")
323 return errors
.New("timer fired early")
330 return errors
.New("reset timer did not fire")
333 if t0
.Reset(50*Millisecond
) != false {
334 return errors
.New("resetting expired timer returned true")
339 func TestReset(t
*testing
.T
) {
340 // We try to run this test with increasingly larger multiples
341 // until one works so slow, loaded hardware isn't as flaky,
342 // but without slowing down fast machines unnecessarily.
343 const unit
= 25 * Millisecond
351 for _
, d
:= range tries
{
354 t
.Logf("passed using duration %v", d
)
361 // Test that sleeping for an interval so large it overflows does not
362 // result in a short sleep duration.
363 func TestOverflowSleep(t
*testing
.T
) {
364 const timeout
= 25 * Millisecond
365 const big
= Duration(int64(1<<63 - 1))
368 t
.Fatalf("big timeout fired")
369 case <-After(timeout
):
372 const neg
= Duration(-1 << 63)
376 case <-After(timeout
):
377 t
.Fatalf("negative timeout didn't fire")
381 // Test that a panic while deleting a timer does not leave
382 // the timers mutex held, deadlocking a ticker.Stop in a defer.
383 func TestIssue5745(t
*testing
.T
) {
384 ticker
:= NewTicker(Hour
)
386 // would deadlock here before the fix due to
387 // lock taken before the segfault.
390 if r
:= recover(); r
== nil {
391 t
.Error("Expected panic, but none happened.")
395 // cause a panic due to a segfault
398 t
.Error("Should be unreachable.")
401 func TestOverflowRuntimeTimer(t
*testing
.T
) {
403 t
.Skip("skipping in short mode, see issue 6874")
405 if err
:= CheckRuntimeTimerOverflow(); err
!= nil {
406 t
.Fatalf(err
.Error())