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.
15 func TestSleep(t *testing.T) {
16 const delay = int64(100e6)
21 start := Nanoseconds()
23 duration := Nanoseconds() - start
25 t.Fatalf("Sleep(%d) slept for only %d ns", delay, duration)
29 // Test the basic function calling behavior. Correct queueing
30 // behavior is tested elsewhere, since After and AfterFunc share
32 func TestAfterFunc(t *testing.T) {
50 func BenchmarkAfterFunc(b *testing.B) {
67 func BenchmarkAfter(b *testing.B) {
68 for i := 0; i < b.N; i++ {
73 func BenchmarkStop(b *testing.B) {
74 for i := 0; i < b.N; i++ {
79 func TestAfter(t *testing.T) {
80 const delay = int64(100e6)
81 start := Nanoseconds()
83 if duration := Nanoseconds() - start; duration < delay {
84 t.Fatalf("After(%d) slept for only %d ns", delay, duration)
86 if min := start + delay; end < min {
87 t.Fatalf("After(%d) expect >= %d, got %d", delay, min, end)
91 func TestAfterTick(t *testing.T) {
97 for i := 0; i < Count; i++ {
102 target := int64(Delta * Count)
103 slop := target * 2 / 10
104 if ns < target-slop || ns > target+slop {
105 t.Fatalf("%d ticks of %g ns took %g ns, expected %g", Count, float64(Delta), float64(ns), float64(target))
109 func TestAfterStop(t *testing.T) {
111 AfterFunc(100*msec, func() {})
112 t0 := NewTimer(50 * msec)
113 c1 := make(chan bool, 1)
114 t1 := AfterFunc(150*msec, func() { c1 <- true })
115 c2 := After(200 * msec)
117 t.Fatalf("failed to stop event 0")
120 t.Fatalf("failed to stop event 1")
125 t.Fatalf("event 0 was not stopped")
127 t.Fatalf("event 1 was not stopped")
131 t.Fatalf("Stop returned true twice")
135 func TestAfterQueuing(t *testing.T) {
136 // This test flakes out on some systems,
137 // so we'll try it a few times before declaring it a failure.
139 err := errors.New("!=nil")
140 for i := 0; i < attempts && err != nil; i++ {
141 if err = testAfterQueuing(t); err != nil {
142 t.Logf("attempt %v failed: %v", i, err)
150 // For gccgo omit 0 for now because it can take too long to start the
151 var slots = []int{5, 3, 6, 6, 6, 1, 1, 2, 7, 9, 4, 8, /*0*/}
153 type afterResult struct {
158 func await(slot int, result chan<- afterResult, ac <-chan int64) {
159 result <- afterResult{slot, <-ac}
162 func testAfterQueuing(t *testing.T) error {
166 // make the result channel buffered because we don't want
167 // to depend on channel queueing semantics that might
168 // possibly change in the future.
169 result := make(chan afterResult, len(slots))
172 for _, slot := range slots {
173 go await(slot, result, After(int64(slot)*Delta))
176 for _, slot := range slots {
179 return fmt.Errorf("after queue got slot %d, expected %d", r.slot, slot)
182 target := int64(slot * Delta)
183 slop := int64(Delta) / 4
184 if ns < target-slop || ns > target+slop {
185 return fmt.Errorf("after queue slot %d arrived at %g, expected [%g,%g]", slot, float64(ns), float64(target-slop), float64(target+slop))