Source file src/time/tick_test.go

Documentation: time

     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.
     4  
     5  package time_test
     6  
     7  import (
     8  	"fmt"
     9  	"runtime"
    10  	"testing"
    11  	. "time"
    12  )
    13  
    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.
    19  
    20  	count := 10
    21  	delta := 20 * Millisecond
    22  
    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
    27  	}
    28  
    29  	var errs []string
    30  	logErrs := func() {
    31  		for _, e := range errs {
    32  			t.Log(e)
    33  		}
    34  	}
    35  
    36  	for i := 0; i < 5; i++ {
    37  		ticker := NewTicker(delta)
    38  		t0 := Now()
    39  		for i := 0; i < count; i++ {
    40  			<-ticker.C
    41  		}
    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
    50  		}
    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
    59  		}
    60  
    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()
    65  		}
    66  
    67  		return
    68  	}
    69  
    70  	t.Errorf("saw %d errors", len(errs))
    71  	logErrs()
    72  }
    73  
    74  // Issue 21874
    75  func TestTickerStopWithDirectInitialization(t *testing.T) {
    76  	c := make(chan Time)
    77  	tk := &Ticker{C: c}
    78  	tk.Stop()
    79  }
    80  
    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
    86  	}
    87  	for i := 0; i < 3; i++ {
    88  		ticker := NewTicker(Delta)
    89  		<-ticker.C
    90  		ticker.Stop()
    91  	}
    92  }
    93  
    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)
    99  	}
   100  }
   101  
   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")
   107  		}
   108  	}()
   109  	NewTicker(-1)
   110  }
   111  
   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
   117  		}
   118  		ticker.Stop()
   119  	})
   120  }
   121  

View as plain text