vitess.io/vitess@v0.16.2/go/vt/logutil/logger_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package logutil 18 19 import ( 20 "testing" 21 "time" 22 23 "vitess.io/vitess/go/race" 24 logutilpb "vitess.io/vitess/go/vt/proto/logutil" 25 ) 26 27 func TestLogEvent(t *testing.T) { 28 testValues := []struct { 29 event *logutilpb.Event 30 expected string 31 }{ 32 { 33 event: &logutilpb.Event{ 34 Time: TimeToProto(time.Date(2014, time.November, 10, 23, 30, 12, 123456000, time.UTC)), 35 Level: logutilpb.Level_INFO, 36 File: "file.go", 37 Line: 123, 38 Value: "message", 39 }, 40 expected: "I1110 23:30:12.123456 file.go:123] message", 41 }, 42 { 43 event: &logutilpb.Event{ 44 Time: TimeToProto(time.Date(2014, time.January, 20, 23, 30, 12, 0, time.UTC)), 45 Level: logutilpb.Level_WARNING, 46 File: "file2.go", 47 Line: 567, 48 Value: "message %v %v", 49 }, 50 expected: "W0120 23:30:12.000000 file2.go:567] message %v %v", 51 }, 52 { 53 event: &logutilpb.Event{ 54 Time: TimeToProto(time.Date(2014, time.January, 20, 23, 30, 12, 0, time.UTC)), 55 Level: logutilpb.Level_ERROR, 56 File: "file2.go", 57 Line: 567, 58 Value: "message %v %v", 59 }, 60 expected: "E0120 23:30:12.000000 file2.go:567] message %v %v", 61 }, 62 { 63 event: &logutilpb.Event{ 64 Time: TimeToProto(time.Date(2014, time.January, 20, 23, 30, 12, 0, time.UTC)), 65 Level: logutilpb.Level_CONSOLE, 66 File: "file2.go", 67 Line: 567, 68 Value: "message %v %v", 69 }, 70 expected: "message %v %v", 71 }, 72 } 73 ml := NewMemoryLogger() 74 for i, testValue := range testValues { 75 LogEvent(ml, testValue.event) 76 if got, want := ml.Events[i].Value, testValue.expected; got != want { 77 t.Errorf("ml.Events[%v].Value = %q, want %q", i, got, want) 78 } 79 // Skip the check below if go test -race is run because then the stack 80 // is shifted by one and the test would fail. 81 if !race.Enabled { 82 if got, want := ml.Events[i].File, "logger_test.go"; got != want && ml.Events[i].Level != logutilpb.Level_CONSOLE { 83 t.Errorf("ml.Events[%v].File = %q (line = %v), want %q", i, got, ml.Events[i].Line, want) 84 } 85 } 86 } 87 } 88 89 func TestMemoryLogger(t *testing.T) { 90 ml := NewMemoryLogger() 91 ml.Infof("test %v", 123) 92 if got, want := len(ml.Events), 1; got != want { 93 t.Fatalf("len(ml.Events) = %v, want %v", got, want) 94 } 95 if got, want := ml.Events[0].File, "logger_test.go"; got != want { 96 t.Errorf("ml.Events[0].File = %q, want %q", got, want) 97 } 98 ml.Warningf("test %v", 456) 99 if got, want := len(ml.Events), 2; got != want { 100 t.Fatalf("len(ml.Events) = %v, want %v", got, want) 101 } 102 if got, want := ml.Events[1].File, "logger_test.go"; got != want { 103 t.Errorf("ml.Events[1].File = %q, want %q", got, want) 104 } 105 ml.Errorf("test %v", 789) 106 if got, want := len(ml.Events), 3; got != want { 107 t.Fatalf("len(ml.Events) = %v, want %v", got, want) 108 } 109 if got, want := ml.Events[2].File, "logger_test.go"; got != want { 110 t.Errorf("ml.Events[2].File = %q, want %q", got, want) 111 } 112 } 113 114 func TestChannelLogger(t *testing.T) { 115 cl := NewChannelLogger(10) 116 cl.Infof("test %v", 123) 117 cl.Warningf("test %v", 123) 118 cl.Errorf("test %v", 123) 119 cl.Printf("test %v", 123) 120 close(cl.C) 121 122 count := 0 123 for e := range cl.C { 124 if got, want := e.Value, "test 123"; got != want { 125 t.Errorf("e.Value = %q, want %q", got, want) 126 } 127 if e.File != "logger_test.go" { 128 t.Errorf("Invalid file name: %v", e.File) 129 } 130 count++ 131 } 132 if got, want := count, 4; got != want { 133 t.Errorf("count = %v, want %v", got, want) 134 } 135 } 136 137 func TestTeeLogger(t *testing.T) { 138 ml := NewMemoryLogger() 139 cl := NewChannelLogger(10) 140 tl := NewTeeLogger(ml, cl) 141 142 tl.Infof("test infof %v %v", 1, 2) 143 tl.Warningf("test warningf %v %v", 2, 3) 144 tl.Errorf("test errorf %v %v", 3, 4) 145 tl.Printf("test printf %v %v", 4, 5) 146 close(cl.C) 147 148 clEvents := []*logutilpb.Event{} 149 for e := range cl.C { 150 clEvents = append(clEvents, e) 151 } 152 153 wantEvents := []*logutilpb.Event{ 154 {Level: logutilpb.Level_INFO, Value: "test infof 1 2"}, 155 {Level: logutilpb.Level_WARNING, Value: "test warningf 2 3"}, 156 {Level: logutilpb.Level_ERROR, Value: "test errorf 3 4"}, 157 {Level: logutilpb.Level_CONSOLE, Value: "test printf 4 5"}, 158 } 159 wantFile := "logger_test.go" 160 161 for i, events := range [][]*logutilpb.Event{ml.Events, clEvents} { 162 if got, want := len(events), len(wantEvents); got != want { 163 t.Fatalf("[%v] len(events) = %v, want %v", i, got, want) 164 } 165 for j, got := range events { 166 want := wantEvents[j] 167 if got.Level != want.Level { 168 t.Errorf("[%v] events[%v].Level = %s, want %s", i, j, got.Level, want.Level) 169 } 170 if got.Value != want.Value { 171 t.Errorf("[%v] events[%v].Value = %q, want %q", i, j, got.Value, want.Value) 172 } 173 // Skip the check below if go test -race is run because then the stack 174 // is shifted by one and the test would fail. 175 if !race.Enabled { 176 if got.File != wantFile && got.Level != logutilpb.Level_CONSOLE { 177 t.Errorf("[%v] events[%v].File = %q, want %q", i, j, got.File, wantFile) 178 } 179 } 180 } 181 } 182 }