github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/grpcutil/log_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 grpcutil
    12  
    13  import (
    14  	"regexp"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/testutils"
    19  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    20  	"github.com/cockroachdb/errors"
    21  	"github.com/petermattis/goid"
    22  )
    23  
    24  func TestShouldPrint(t *testing.T) {
    25  	const duration = 100 * time.Millisecond
    26  
    27  	formatRe, err := regexp.Compile("^foo")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	argRe, err := regexp.Compile("[a-z][0-9]")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	testutils.RunTrueAndFalse(t, "formatMatch", func(t *testing.T, formatMatch bool) {
    37  		testutils.RunTrueAndFalse(t, "argsMatch", func(t *testing.T, argsMatch bool) {
    38  			format := "bar=%s"
    39  			if formatMatch {
    40  				format = "foobar=%s"
    41  			}
    42  			args := []interface{}{errors.New("baz")}
    43  			if argsMatch {
    44  				args = []interface{}{errors.New("a1")}
    45  			}
    46  			curriedShouldPrint := func() bool {
    47  				return shouldPrint(formatRe, argRe, duration, format, args...)
    48  			}
    49  
    50  			// First call should always print.
    51  			if !curriedShouldPrint() {
    52  				t.Error("expected first call to print")
    53  			}
    54  
    55  			// Call from another goroutine should always print.
    56  			done := make(chan bool)
    57  			go func() {
    58  				done <- curriedShouldPrint()
    59  			}()
    60  			if !<-done {
    61  				t.Error("expected other-goroutine call to print")
    62  			}
    63  
    64  			// Should print if non-matching.
    65  			alwaysPrint := !(formatMatch && argsMatch)
    66  
    67  			if alwaysPrint {
    68  				if !curriedShouldPrint() {
    69  					t.Error("expected second call to print")
    70  				}
    71  			} else {
    72  				if curriedShouldPrint() {
    73  					t.Error("unexpected second call to print")
    74  				}
    75  			}
    76  
    77  			if !alwaysPrint {
    78  				// Force printing by pretending the previous output was well in the
    79  				// past.
    80  				spamMu.Lock()
    81  				spamMu.gids[goid.Get()] = timeutil.Now().Add(-time.Hour)
    82  				spamMu.Unlock()
    83  			}
    84  			if !curriedShouldPrint() {
    85  				t.Error("expected third call to print")
    86  			}
    87  		})
    88  	})
    89  }