Fix "PR c++/92804 ICE trying to use concept as a nested-name-specifier"
[official-gcc.git] / libgo / go / time / tick_test.go
blob71ea3672b86645a4d8d35e9be033f39930d468a8
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 "fmt"
9 "runtime"
10 "testing"
11 . "time"
14 func TestTicker(t *testing.T) {
15 // We want to test that a ticker takes as much time as expected.
16 // Since we don't want the test to run for too long, we don't
17 // want to use lengthy times. This makes the test inherently flaky.
18 // So only report an error if it fails five times in a row.
20 count := 10
21 delta := 20 * Millisecond
23 // On Darwin ARM64 the tick frequency seems limited. Issue 35692.
24 if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
25 count = 5
26 delta = 100 * Millisecond
29 var errs []string
30 logErrs := func() {
31 for _, e := range errs {
32 t.Log(e)
36 for i := 0; i < 5; i++ {
37 ticker := NewTicker(delta)
38 t0 := Now()
39 for i := 0; i < count; i++ {
40 <-ticker.C
42 ticker.Stop()
43 t1 := Now()
44 dt := t1.Sub(t0)
45 target := delta * Duration(count)
46 slop := target * 2 / 10
47 if dt < target-slop || dt > target+slop {
48 errs = append(errs, fmt.Sprintf("%d %s ticks took %s, expected [%s,%s]", count, delta, dt, target-slop, target+slop))
49 continue
51 // Now test that the ticker stopped.
52 Sleep(2 * delta)
53 select {
54 case <-ticker.C:
55 errs = append(errs, "Ticker did not shut down")
56 continue
57 default:
58 // ok
61 // Test passed, so all done.
62 if len(errs) > 0 {
63 t.Logf("saw %d errors, ignoring to avoid flakiness", len(errs))
64 logErrs()
67 return
70 t.Errorf("saw %d errors", len(errs))
71 logErrs()
74 // Issue 21874
75 func TestTickerStopWithDirectInitialization(t *testing.T) {
76 c := make(chan Time)
77 tk := &Ticker{C: c}
78 tk.Stop()
81 // Test that a bug tearing down a ticker has been fixed. This routine should not deadlock.
82 func TestTeardown(t *testing.T) {
83 Delta := 100 * Millisecond
84 if testing.Short() {
85 Delta = 20 * Millisecond
87 for i := 0; i < 3; i++ {
88 ticker := NewTicker(Delta)
89 <-ticker.C
90 ticker.Stop()
94 // Test the Tick convenience wrapper.
95 func TestTick(t *testing.T) {
96 // Test that giving a negative duration returns nil.
97 if got := Tick(-1); got != nil {
98 t.Errorf("Tick(-1) = %v; want nil", got)
102 // Test that NewTicker panics when given a duration less than zero.
103 func TestNewTickerLtZeroDuration(t *testing.T) {
104 defer func() {
105 if err := recover(); err == nil {
106 t.Errorf("NewTicker(-1) should have panicked")
109 NewTicker(-1)
112 func BenchmarkTicker(b *testing.B) {
113 benchmark(b, func(n int) {
114 ticker := NewTicker(Nanosecond)
115 for i := 0; i < n; i++ {
116 <-ticker.C
118 ticker.Stop()