LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package time_test | 5 package time_test |
6 | 6 |
7 import ( | 7 import ( |
8 "os" | 8 "os" |
9 "syscall" | 9 "syscall" |
10 "testing" | 10 "testing" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 slop := target * 2 / 10 | 103 slop := target * 2 / 10 |
104 if ns < target-slop || ns > target+slop { | 104 if ns < target-slop || ns > target+slop { |
105 t.Fatalf("%d ticks of %g ns took %g ns, expected %g", Count, flo
at64(Delta), float64(ns), float64(target)) | 105 t.Fatalf("%d ticks of %g ns took %g ns, expected %g", Count, flo
at64(Delta), float64(ns), float64(target)) |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 func TestAfterStop(t *testing.T) { | 109 func TestAfterStop(t *testing.T) { |
110 const msec = 1e6 | 110 const msec = 1e6 |
111 AfterFunc(100*msec, func() {}) | 111 AfterFunc(100*msec, func() {}) |
112 t0 := NewTimer(50 * msec) | 112 t0 := NewTimer(50 * msec) |
113 » t1 := NewTimer(150 * msec) | 113 » c1 := make(chan bool, 1) |
| 114 » t1 := AfterFunc(150*msec, func() { c1 <- true }) |
114 c2 := After(200 * msec) | 115 c2 := After(200 * msec) |
115 if !t0.Stop() { | 116 if !t0.Stop() { |
116 t.Fatalf("failed to stop event 0") | 117 t.Fatalf("failed to stop event 0") |
117 } | 118 } |
118 if !t1.Stop() { | 119 if !t1.Stop() { |
119 t.Fatalf("failed to stop event 1") | 120 t.Fatalf("failed to stop event 1") |
120 } | 121 } |
121 <-c2 | 122 <-c2 |
122 _, ok0 := <-t0.C | 123 _, ok0 := <-t0.C |
123 » _, ok1 := <-t1.C | 124 » _, ok1 := <-c1 |
124 if ok0 || ok1 { | 125 if ok0 || ok1 { |
125 t.Fatalf("events were not stopped") | 126 t.Fatalf("events were not stopped") |
126 } | 127 } |
127 if t1.Stop() { | 128 if t1.Stop() { |
128 t.Fatalf("Stop returned true twice") | 129 t.Fatalf("Stop returned true twice") |
129 } | 130 } |
130 } | 131 } |
131 | 132 |
132 var slots = []int{5, 3, 6, 6, 6, 1, 1, 2, 7, 9, 4, 8, 0} | 133 var slots = []int{5, 3, 6, 6, 6, 1, 1, 2, 7, 9, 4, 8, 0} |
133 | 134 |
(...skipping 26 matching lines...) Expand all Loading... |
160 t.Fatalf("after queue got slot %d, expected %d", r.slot,
slot) | 161 t.Fatalf("after queue got slot %d, expected %d", r.slot,
slot) |
161 } | 162 } |
162 ns := r.t - t0 | 163 ns := r.t - t0 |
163 target := int64(slot * Delta) | 164 target := int64(slot * Delta) |
164 slop := int64(Delta) / 4 | 165 slop := int64(Delta) / 4 |
165 if ns < target-slop || ns > target+slop { | 166 if ns < target-slop || ns > target+slop { |
166 t.Fatalf("after queue slot %d arrived at %g, expected [%
g,%g]", slot, float64(ns), float64(target-slop), float64(target+slop)) | 167 t.Fatalf("after queue slot %d arrived at %g, expected [%
g,%g]", slot, float64(ns), float64(target-slop), float64(target+slop)) |
167 } | 168 } |
168 } | 169 } |
169 } | 170 } |
LEFT | RIGHT |