Browse Source

test: perf encoder test suite

dev
Dnomd343 9 months ago
parent
commit
09ea911a28
  1. 127
      logger/encoder_test.go

127
logger/encoder_test.go

@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/mock"
"go.uber.org/zap/zapcore"
"runtime"
"strings"
"testing"
"time"
)
@ -23,100 +24,76 @@ func zapCaller() zapcore.EntryCaller {
}
}
// encoderTest is a helper function to test buffer output of mock encoder.
func encoderTest(t *testing.T, exec func(*mocks.PrimitiveArrayEncoder), expect string) {
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendInt64", mock.Anything).Maybe() // only enroll used method
encoder.On("AppendString", mock.Anything).Maybe()
exec(encoder)
var values []string
for _, call := range encoder.Calls {
values = append(values, fmt.Sprintf("%v", call.Arguments.Get(0)))
}
assert.Equal(t, strings.Join(values, " "), expect)
}
func Test_getCaller(t *testing.T) {
caller := zapCaller()
assert.Equal(t, getCaller(caller, false), "logger/encoder_test")
assert.Equal(t, getCaller(caller, true), "logger/encoder_test.go:0")
caller.File = "Invalid Path"
assert.Equal(t, getCaller(caller, true), "unknown")
assert.Equal(t, getCaller(caller, false), "unknown")
assert.Equal(t, getCaller(zapCaller(), false), "logger/encoder_test")
assert.Equal(t, getCaller(zapCaller(), true), "logger/encoder_test.go:0")
}
func Test_timeEncoder(t *testing.T) {
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) {
timeEncoder(testTime, encoder)
encoder.AssertCalled(t, "AppendString", "2000-01-01 00:00:00.000")
}
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
}, "2000-01-01 00:00:00.000")
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) {
timeColoredEncoder(testTime, encoder)
exceptPrefix := "\x1b[36m" + logger.prefix + "\x1b[0m"
encoder.AssertCalled(t, "AppendString", exceptPrefix+" \x1b[90m2000-01-01 00:00:00.000\x1b[0m")
}
}, "\x1b[36m"+logger.prefix+"\x1b[0m \x1b[90m2000-01-01 00:00:00.000\x1b[0m")
}
func Test_callerEncoder(t *testing.T) {
logger.verbose = false
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
callerEncoder(zapCaller(), encoder)
encoder.AssertCalled(t, "AppendString", "[logger/encoder_test]")
}
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
callerColoredEncoder(zapCaller(), encoder)
encoder.AssertCalled(t, "AppendString", "\x1b[35m[logger/encoder_test]\x1b[0m")
verboseVal := logger.verbose
callerTest := func(entry func(zapcore.EntryCaller, zapcore.PrimitiveArrayEncoder), expect string) {
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) {
entry(zapCaller(), encoder)
}, expect)
}
logger.verbose = false
callerTest(callerEncoder, "[logger/encoder_test]")
callerTest(callerColoredEncoder, "\x1b[35m[logger/encoder_test]\x1b[0m")
logger.verbose = true
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
callerEncoder(zapCaller(), encoder)
expectPrefix := fmt.Sprintf("[%d]", goid.Get())
encoder.AssertCalled(t, "AppendString", expectPrefix+" [logger/encoder_test.go:0]")
}
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
callerColoredEncoder(zapCaller(), encoder)
expectPrefix := fmt.Sprintf("\x1b[34m[%d]\x1b[0m", goid.Get())
encoder.AssertCalled(t, "AppendString", expectPrefix+" \x1b[35m[logger/encoder_test.go:0]\x1b[0m")
}
gid := fmt.Sprintf("[%d]", goid.Get())
callerTest(callerEncoder, gid+" [logger/encoder_test.go:0]")
callerTest(callerColoredEncoder, "\x1b[34m"+gid+"\x1b[0m \x1b[35m[logger/encoder_test.go:0]\x1b[0m")
logger.verbose = verboseVal
}
func Test_levelEncoder(t *testing.T) {
encoder := mocks.NewPrimitiveArrayEncoder(t)
enroll := func(values []string, call *mock.Call) *mock.Call {
for _, value := range values {
if call == nil {
call = encoder.On("AppendString", value).Once()
} else {
call = encoder.On("AppendString", value).Once().NotBefore(call)
}
}
return call
levelTest := func(entry func(zapcore.Level, zapcore.PrimitiveArrayEncoder), level zapcore.Level, expect string) {
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) {
entry(level, encoder)
}, expect)
}
caller := enroll([]string{
"[DEBUG]",
"[INFO]",
"[WARN]",
"[ERROR]",
"[PANIC]",
}, nil)
levelEncoder(zapcore.DebugLevel, encoder)
levelEncoder(zapcore.InfoLevel, encoder)
levelEncoder(zapcore.WarnLevel, encoder)
levelEncoder(zapcore.ErrorLevel, encoder)
levelEncoder(zapcore.PanicLevel, encoder)
enroll([]string{
"\x1b[39m[DEBUG]\x1b[0m",
"\x1b[32m[INFO]\x1b[0m",
"\x1b[33m[WARN]\x1b[0m",
"\x1b[31m[ERROR]\x1b[0m",
"\x1b[91m[PANIC]\x1b[0m",
}, caller)
levelColoredEncoder(zapcore.DebugLevel, encoder)
levelColoredEncoder(zapcore.InfoLevel, encoder)
levelColoredEncoder(zapcore.WarnLevel, encoder)
levelColoredEncoder(zapcore.ErrorLevel, encoder)
levelColoredEncoder(zapcore.PanicLevel, encoder)
levelTest(levelEncoder, zapcore.DebugLevel, "[DEBUG]")
levelTest(levelEncoder, zapcore.InfoLevel, "[INFO]")
levelTest(levelEncoder, zapcore.WarnLevel, "[WARN]")
levelTest(levelEncoder, zapcore.ErrorLevel, "[ERROR]")
levelTest(levelEncoder, zapcore.PanicLevel, "[PANIC]")
levelTest(levelColoredEncoder, zapcore.DebugLevel, "\x1b[39m[DEBUG]\x1b[0m")
levelTest(levelColoredEncoder, zapcore.InfoLevel, "\x1b[32m[INFO]\x1b[0m")
levelTest(levelColoredEncoder, zapcore.WarnLevel, "\x1b[33m[WARN]\x1b[0m")
levelTest(levelColoredEncoder, zapcore.ErrorLevel, "\x1b[31m[ERROR]\x1b[0m")
levelTest(levelColoredEncoder, zapcore.PanicLevel, "\x1b[91m[PANIC]\x1b[0m")
}

Loading…
Cancel
Save