Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / time / tick_test.go
blobd089a9b98ca1739e1524fd863ee5f1a9ac53a831
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.
5 package time_test
7 import (
8 "testing"
9 . "time"
12 func TestTicker(t *testing.T) {
13 const (
14 Delta = 100 * 1e6
15 Count = 10
17 ticker := NewTicker(Delta)
18 t0 := Nanoseconds()
19 for i := 0; i < Count; i++ {
20 <-ticker.C
22 ticker.Stop()
23 t1 := Nanoseconds()
24 ns := t1 - t0
25 target := int64(Delta * Count)
26 slop := target * 2 / 10
27 if ns < target-slop || ns > target+slop {
28 t.Fatalf("%d ticks of %g ns took %g ns, expected %g", Count, float64(Delta), float64(ns), float64(target))
30 // Now test that the ticker stopped
31 Sleep(2 * Delta)
32 _, received := <-ticker.C
33 if received {
34 t.Fatalf("Ticker did not shut down")
38 // Test that a bug tearing down a ticker has been fixed. This routine should not deadlock.
39 func TestTeardown(t *testing.T) {
40 for i := 0; i < 3; i++ {
41 ticker := NewTicker(1e8)
42 <-ticker.C
43 ticker.Stop()