github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/every_n_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package util
    12  
    13  import (
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    18  )
    19  
    20  func TestEveryN(t *testing.T) {
    21  	start := timeutil.Now()
    22  	en := EveryN{N: time.Minute}
    23  	testCases := []struct {
    24  		t        time.Duration // time since start
    25  		expected bool
    26  	}{
    27  		{0, true}, // the first attempt to log should always succeed
    28  		{0, false},
    29  		{time.Second, false},
    30  		{time.Minute - 1, false},
    31  		{time.Minute, true},
    32  		{time.Minute, false},
    33  		{time.Minute + 30*time.Second, false},
    34  		{10 * time.Minute, true},
    35  		{10 * time.Minute, false},
    36  		{10*time.Minute + 59*time.Second, false},
    37  		{11 * time.Minute, true},
    38  	}
    39  	for _, tc := range testCases {
    40  		if a, e := en.ShouldProcess(start.Add(tc.t)), tc.expected; a != e {
    41  			t.Errorf("ShouldProcess(%v) got %v, want %v", tc.t, a, e)
    42  		}
    43  	}
    44  }