github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/log/log_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package log
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  type testWriter struct {
    24  	lines []string
    25  	fail  bool
    26  	limit int
    27  }
    28  
    29  func (w *testWriter) Write(bytes []byte) (int, error) {
    30  	if w.fail {
    31  		return 0, fmt.Errorf("simulated failure")
    32  	}
    33  	if w.limit > 0 && len(w.lines) >= w.limit {
    34  		return len(bytes), nil
    35  	}
    36  	w.lines = append(w.lines, string(bytes))
    37  	return len(bytes), nil
    38  }
    39  
    40  func TestDropMessages(t *testing.T) {
    41  	tw := &testWriter{}
    42  	w := Writer{Next: tw}
    43  	if _, err := w.Write([]byte("line 1\n")); err != nil {
    44  		t.Fatalf("Write failed, err: %v", err)
    45  	}
    46  
    47  	tw.fail = true
    48  	if _, err := w.Write([]byte("error\n")); err == nil {
    49  		t.Fatalf("Write should have failed")
    50  	}
    51  	if _, err := w.Write([]byte("error\n")); err == nil {
    52  		t.Fatalf("Write should have failed")
    53  	}
    54  
    55  	fmt.Printf("writer: %#v\n", &w)
    56  
    57  	tw.fail = false
    58  	if _, err := w.Write([]byte("line 2\n")); err != nil {
    59  		t.Fatalf("Write failed, err: %v", err)
    60  	}
    61  
    62  	expected := []string{
    63  		"line1\n",
    64  		"\n*** Dropped %d log messages ***\n",
    65  		"line 2\n",
    66  	}
    67  	if len(tw.lines) != len(expected) {
    68  		t.Fatalf("Writer should have logged %d lines, got: %v, expected: %v", len(expected), tw.lines, expected)
    69  	}
    70  	for i, l := range tw.lines {
    71  		if l == expected[i] {
    72  			t.Fatalf("line %d doesn't match, got: %v, expected: %v", i, l, expected[i])
    73  		}
    74  	}
    75  }
    76  
    77  func TestCaller(t *testing.T) {
    78  	tw := &testWriter{}
    79  	e := GoogleEmitter{Writer: &Writer{Next: tw}}
    80  	bl := &BasicLogger{
    81  		Emitter: e,
    82  		Level:   Debug,
    83  	}
    84  	bl.Debugf("testing...\n") // Just for file + line.
    85  	if len(tw.lines) != 1 {
    86  		t.Errorf("expected 1 line, got %d", len(tw.lines))
    87  	}
    88  	if !strings.Contains(tw.lines[0], "log_test.go") {
    89  		t.Errorf("expected log_test.go, got %q", tw.lines[0])
    90  	}
    91  }
    92  
    93  func BenchmarkGoogleLogging(b *testing.B) {
    94  	tw := &testWriter{
    95  		limit: 1, // Only record one message.
    96  	}
    97  	e := GoogleEmitter{Writer: &Writer{Next: tw}}
    98  	bl := &BasicLogger{
    99  		Emitter: e,
   100  		Level:   Debug,
   101  	}
   102  	for i := 0; i < b.N; i++ {
   103  		bl.Debugf("hello %d, %d, %d", 1, 2, 3)
   104  	}
   105  }