// The ReadAll will hang for a failing test, so use a Timer to
// fail explicitly.
- goTimeout(t, 2e9, func() {
+ goTimeout(t, 2*time.Second, func() {
got, _ := ioutil.ReadAll(conn)
expectedSuffix := "\r\n\r\ntoo short"
if !strings.HasSuffix(string(got), expectedSuffix) {
success := make(chan bool)
go func() {
select {
- case <-time.After(5e9):
+ case <-time.After(5 * time.Second):
t.Fatal("body not closed after 5s")
case <-success:
}
t.Fatalf("Dial: %v", err)
}
defer conn.Close()
- goTimeout(t, 10e9, func() {
+ goTimeout(t, 10*time.Second, func() {
var buf [1]byte
n, err := conn.Read(buf[:])
if err == nil || n != 0 {
t.Fatalf("Dial: %v", err)
}
defer idleConn.Close()
- goTimeout(t, 10e9, func() {
+ goTimeout(t, 10*time.Second, func() {
if !strings.HasPrefix(ts.URL, "https://") {
t.Errorf("expected test TLS server to start with https://, got %q", ts.URL)
return
select {
case <-done:
return
- case <-time.After(5e9):
+ case <-time.After(5 * time.Second):
t.Fatal("expected server handler to log an error")
}
}
}()
select {
case <-donec:
- case <-time.After(10e9):
+ case <-time.After(10 * time.Second):
t.Fatalf("timeout")
}
}
}
// goTimeout runs f, failing t if f takes more than ns to complete.
-func goTimeout(t *testing.T, ns int64, f func()) {
+func goTimeout(t *testing.T, d time.Duration, f func()) {
ch := make(chan bool, 2)
- timer := time.AfterFunc(ns, func() {
- t.Errorf("Timeout expired after %d ns", ns)
+ timer := time.AfterFunc(d, func() {
+ t.Errorf("Timeout expired after %v", d)
ch <- true
})
defer timer.Stop()
return NewTimer(d).C
}
-// AfterFunc waits at least ns nanoseconds before calling f
+// AfterFunc waits for the duration to elapse and then calls f
// in its own goroutine. It returns a Timer that can
// be used to cancel the call using its Stop method.
-func AfterFunc(ns int64, f func()) *Timer {
+func AfterFunc(d Duration, f func()) *Timer {
t := &Timer{
r: runtimeTimer{
- when: nano() + ns,
+ when: nano() + int64(d),
f: goFunc,
arg: f,
},
i--
if i >= 0 {
AfterFunc(0, f)
- Sleep(1e9)
+ Sleep(1 * Second)
} else {
c <- true
}
func BenchmarkStop(b *testing.B) {
for i := 0; i < b.N; i++ {
- NewTimer(1e9).Stop()
+ NewTimer(1 * Second).Stop()
}
}
}
func TestAfterStop(t *testing.T) {
- const msec = 1e6
- AfterFunc(100*msec, func() {})
- t0 := NewTimer(50 * msec)
+ AfterFunc(100*Millisecond, func() {})
+ t0 := NewTimer(50 * Millisecond)
c1 := make(chan bool, 1)
- t1 := AfterFunc(150*msec, func() { c1 <- true })
- c2 := After(200 * msec)
+ t1 := AfterFunc(150*Millisecond, func() { c1 <- true })
+ c2 := After(200 * Millisecond)
if !t0.Stop() {
t.Fatalf("failed to stop event 0")
}
}
for i := 0; i < 100; i++ {
go func(i int) {
- timer := AfterFunc(2e9, func() {
+ timer := AfterFunc(2*Second, func() {
t.Fatalf("timer %d was not stopped", i)
})
- Sleep(1e9)
+ Sleep(1 * Second)
timer.Stop()
}(i)
}
- Sleep(3e9)
+ Sleep(3 * Second)
}