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.
17 func TestSleep(t *testing.T) {
18 const delay = 100 * Millisecond
25 duration := Now().Sub(start)
27 t.Fatalf("Sleep(%s) slept for only %s", delay, duration)
31 // Test the basic function calling behavior. Correct queueing
32 // behavior is tested elsewhere, since After and AfterFunc share
34 func TestAfterFunc(t *testing.T) {
52 func TestAfterStress(t *testing.T) {
55 for atomic.LoadUint32(&stop) == 0 {
57 // Need to yield, because otherwise
58 // the main goroutine will never set the stop flag.
63 for i := 0; i < 100; i++ {
66 atomic.StoreUint32(&stop, 1)
69 func BenchmarkAfterFunc(b *testing.B) {
86 func BenchmarkAfter(b *testing.B) {
87 for i := 0; i < b.N; i++ {
92 func BenchmarkStop(b *testing.B) {
93 for i := 0; i < b.N; i++ {
94 NewTimer(1 * Second).Stop()
98 func TestAfter(t *testing.T) {
99 const delay = 100 * Millisecond
101 end := <-After(delay)
102 if duration := Now().Sub(start); duration < delay {
103 t.Fatalf("After(%s) slept for only %d ns", delay, duration)
105 if min := start.Add(delay); end.Before(min) {
106 t.Fatalf("After(%s) expect >= %s, got %s", delay, min, end)
110 func TestAfterTick(t *testing.T) {
112 Delta := 100 * Millisecond
114 Delta = 10 * Millisecond
117 for i := 0; i < Count; i++ {
122 target := Delta * Count
124 t.Fatalf("%d ticks of %s too fast: took %s, expected %s", Count, Delta, d, target)
126 if !testing.Short() && d > target*30/10 {
127 t.Fatalf("%d ticks of %s too slow: took %s, expected %s", Count, Delta, d, target)
131 func TestAfterStop(t *testing.T) {
132 AfterFunc(100*Millisecond, func() {})
133 t0 := NewTimer(50 * Millisecond)
134 c1 := make(chan bool, 1)
135 t1 := AfterFunc(150*Millisecond, func() { c1 <- true })
136 c2 := After(200 * Millisecond)
138 t.Fatalf("failed to stop event 0")
141 t.Fatalf("failed to stop event 1")
146 t.Fatalf("event 0 was not stopped")
148 t.Fatalf("event 1 was not stopped")
152 t.Fatalf("Stop returned true twice")
156 func TestAfterQueuing(t *testing.T) {
157 // This test flakes out on some systems,
158 // so we'll try it a few times before declaring it a failure.
160 err := errors.New("!=nil")
161 for i := 0; i < attempts && err != nil; i++ {
162 if err = testAfterQueuing(t); err != nil {
163 t.Logf("attempt %v failed: %v", i, err)
171 // For gccgo omit 0 for now because it can take too long to start the
172 var slots = []int{5, 3, 6, 6, 6, 1, 1, 2, 7, 9, 4, 8 /*0*/}
174 type afterResult struct {
179 func await(slot int, result chan<- afterResult, ac <-chan Time) {
180 result <- afterResult{slot, <-ac}
183 func testAfterQueuing(t *testing.T) error {
184 Delta := 100 * Millisecond
186 Delta = 20 * Millisecond
188 // make the result channel buffered because we don't want
189 // to depend on channel queueing semantics that might
190 // possibly change in the future.
191 result := make(chan afterResult, len(slots))
194 for _, slot := range slots {
195 go await(slot, result, After(Duration(slot)*Delta))
198 for _, slot := range slots {
201 return fmt.Errorf("after slot %d, expected %d", r.slot, slot)
204 target := Duration(slot) * Delta
205 if dt < target-Delta/2 || dt > target+Delta*10 {
206 return fmt.Errorf("After(%s) arrived at %s, expected [%s,%s]", target, dt, target-Delta/2, target+Delta*10)
212 func TestTimerStopStress(t *testing.T) {
216 for i := 0; i < 100; i++ {
218 timer := AfterFunc(2*Second, func() {
219 t.Fatalf("timer %d was not stopped", i)