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.
12 func TestTicker(t
*testing
.T
) {
14 Delta
:= 100 * Millisecond
15 ticker
:= NewTicker(Delta
)
17 for i
:= 0; i
< Count
; i
++ {
23 target
:= Delta
* Count
24 slop
:= target
* 2 / 10
25 if dt
< target
-slop ||
(!testing
.Short() && dt
> target
+slop
) {
26 t
.Fatalf("%d %s ticks took %s, expected [%s,%s]", Count
, Delta
, dt
, target
-slop
, target
+slop
)
28 // Now test that the ticker stopped
32 t
.Fatal("Ticker did not shut down")
38 // Test that a bug tearing down a ticker has been fixed. This routine should not deadlock.
39 func TestTeardown(t
*testing
.T
) {
40 Delta
:= 100 * Millisecond
42 Delta
= 20 * Millisecond
44 for i
:= 0; i
< 3; i
++ {
45 ticker
:= NewTicker(Delta
)
51 // Test the Tick convenience wrapper.
52 func TestTick(t
*testing
.T
) {
53 // Test that giving a negative duration returns nil.
54 if got
:= Tick(-1); got
!= nil {
55 t
.Errorf("Tick(-1) = %v; want nil", got
)
59 // Test that NewTicker panics when given a duration less than zero.
60 func TestNewTickerLtZeroDuration(t
*testing
.T
) {
62 if err
:= recover(); err
== nil {
63 t
.Errorf("NewTicker(-1) should have panicked")
69 func BenchmarkTicker(b
*testing
.B
) {
70 ticker
:= NewTicker(1)
73 for i
:= 0; i
< b
.N
; i
++ {