github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/goutils/logger/impl_test.go (about) 1 /* 2 * Copyright (c) 2020-present unTill Pro, Ltd. 3 * @author Maxim Geraskin 4 * 5 * This source code is licensed under the MIT license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 package logger 10 11 import ( 12 "strings" 13 "testing" 14 "time" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func Test_MsgFormatter(t *testing.T) { 20 var out string 21 22 out = globalLogPrinter.getFormattedMsg("", "sync_op.doSync", 120, "line1") 23 assert.True(t, strings.Contains(out, ": [sync_op.doSync:120]: line1")) 24 25 out = globalLogPrinter.getFormattedMsg("", "", 121, "line1", "line2") 26 assert.True(t, strings.Contains(out, ": [:121]: line1 line2")) 27 28 out = globalLogPrinter.getFormattedMsg("m1:m2/m3", "sync_op.doSync", 126, "line1", "line2", "line3") 29 assert.True(t, strings.Contains(out, "m1:m2/m3: [sync_op.doSync:126]: line1 line2 line3")) 30 31 out = globalLogPrinter.getFormattedMsg("m1:m2/m3", "sync_op.doSync", 127, "line/1", "line/2", "line/3") 32 assert.True(t, strings.Contains(out, "m1:m2/m3: [sync_op.doSync:127]: line/1 line/2 line/3")) 33 } 34 35 func Test_CheckRightPrefix(t *testing.T) { 36 // 1. Info LogLevel = LogLevelInfo 37 SetLogLevel(LogLevelInfo) 38 assert.Equal(t, infoPrefix, getLevelPrefix(globalLogPrinter.logLevel)) 39 40 // 2. Trace LogLevel = LogLevelTrace 41 SetLogLevel(LogLevelTrace) 42 assert.Equal(t, tracePrefix, getLevelPrefix(globalLogPrinter.logLevel)) 43 44 // 3. Warning LogLevel = LogLevelWarning 45 SetLogLevel(LogLevelWarning) 46 assert.Equal(t, warningPrefix, getLevelPrefix(globalLogPrinter.logLevel)) 47 48 // 4. Error LogLevel = LogLevelError 49 SetLogLevel(LogLevelError) 50 assert.Equal(t, errorPrefix, getLevelPrefix(globalLogPrinter.logLevel)) 51 52 // 5. Unexisting level 53 SetLogLevel(7) 54 assert.Equal(t, "", getLevelPrefix(globalLogPrinter.logLevel)) 55 56 SetLogLevel(LogLevelInfo) 57 } 58 59 func Test_GetFuncName(t *testing.T) { 60 funcName, line := globalLogPrinter.getFuncName(2) 61 assert.Equal(t, "testing.tRunner", funcName) 62 assert.Greater(t, line, 0) 63 } 64 65 func Benchmark_FuncForPC(b *testing.B) { 66 var funcName string 67 68 start := time.Now() 69 b.ResetTimer() 70 71 for i := 0; i < b.N; i++ { 72 funcName, _ = globalLogPrinter.getFuncName(2) 73 } 74 assert.Equal(b, "testing.(*B).runN", funcName) 75 76 elapsed := time.Since(start).Seconds() 77 b.ReportMetric(float64(b.N)/elapsed, "rps") 78 }