...
Source file
src/time/tick_test.go
Documentation: time
1
2
3
4
5 package time_test
6
7 import (
8 "fmt"
9 "runtime"
10 "testing"
11 . "time"
12 )
13
14 func TestTicker(t *testing.T) {
15
16
17
18
19
20 count := 10
21 delta := 20 * Millisecond
22
23
24 if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && runtime.GOARCH == "arm64" {
25
26
27
28
29
30 count = 6
31 delta = 100 * Millisecond
32 }
33
34 var errs []string
35 logErrs := func() {
36 for _, e := range errs {
37 t.Log(e)
38 }
39 }
40
41 for i := 0; i < 5; i++ {
42 ticker := NewTicker(delta)
43 t0 := Now()
44 for i := 0; i < count/2; i++ {
45 <-ticker.C
46 }
47 ticker.Reset(delta * 2)
48 for i := count / 2; i < count; i++ {
49 <-ticker.C
50 }
51 ticker.Stop()
52 t1 := Now()
53 dt := t1.Sub(t0)
54 target := 3 * delta * Duration(count/2)
55 slop := target * 3 / 10
56 if dt < target-slop || dt > target+slop {
57 errs = append(errs, fmt.Sprintf("%d %s ticks then %d %s ticks took %s, expected [%s,%s]", count/2, delta, count/2, delta*2, dt, target-slop, target+slop))
58 if dt > target+slop {
59
60
61 Sleep(Second / 2)
62 }
63 continue
64 }
65
66 Sleep(2 * delta)
67 select {
68 case <-ticker.C:
69 errs = append(errs, "Ticker did not shut down")
70 continue
71 default:
72
73 }
74
75
76 if len(errs) > 0 {
77 t.Logf("saw %d errors, ignoring to avoid flakiness", len(errs))
78 logErrs()
79 }
80
81 return
82 }
83
84 t.Errorf("saw %d errors", len(errs))
85 logErrs()
86 }
87
88
89 func TestTickerStopWithDirectInitialization(t *testing.T) {
90 c := make(chan Time)
91 tk := &Ticker{C: c}
92 tk.Stop()
93 }
94
95
96 func TestTeardown(t *testing.T) {
97 Delta := 100 * Millisecond
98 if testing.Short() {
99 Delta = 20 * Millisecond
100 }
101 for i := 0; i < 3; i++ {
102 ticker := NewTicker(Delta)
103 <-ticker.C
104 ticker.Stop()
105 }
106 }
107
108
109 func TestTick(t *testing.T) {
110
111 if got := Tick(-1); got != nil {
112 t.Errorf("Tick(-1) = %v; want nil", got)
113 }
114 }
115
116
117 func TestNewTickerLtZeroDuration(t *testing.T) {
118 defer func() {
119 if err := recover(); err == nil {
120 t.Errorf("NewTicker(-1) should have panicked")
121 }
122 }()
123 NewTicker(-1)
124 }
125
126 func BenchmarkTicker(b *testing.B) {
127 benchmark(b, func(n int) {
128 ticker := NewTicker(Nanosecond)
129 for i := 0; i < n; i++ {
130 <-ticker.C
131 }
132 ticker.Stop()
133 })
134 }
135
136 func BenchmarkTickerReset(b *testing.B) {
137 benchmark(b, func(n int) {
138 ticker := NewTicker(Nanosecond)
139 for i := 0; i < n; i++ {
140 ticker.Reset(Nanosecond * 2)
141 }
142 ticker.Stop()
143 })
144 }
145
146 func BenchmarkTickerResetNaive(b *testing.B) {
147 benchmark(b, func(n int) {
148 ticker := NewTicker(Nanosecond)
149 for i := 0; i < n; i++ {
150 ticker.Stop()
151 ticker = NewTicker(Nanosecond * 2)
152 }
153 ticker.Stop()
154 })
155 }
156
View as plain text