github.com/Laisky/zap@v1.27.0/level_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package zap 22 23 import ( 24 "sync" 25 "testing" 26 27 "github.com/Laisky/zap/zapcore" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestLevelEnablerFunc(t *testing.T) { 34 enab := LevelEnablerFunc(func(l zapcore.Level) bool { return l == zapcore.InfoLevel }) 35 tests := []struct { 36 level zapcore.Level 37 enabled bool 38 }{ 39 {DebugLevel, false}, 40 {InfoLevel, true}, 41 {WarnLevel, false}, 42 {ErrorLevel, false}, 43 {DPanicLevel, false}, 44 {PanicLevel, false}, 45 {FatalLevel, false}, 46 } 47 for _, tt := range tests { 48 assert.Equal(t, tt.enabled, enab.Enabled(tt.level), "Unexpected result applying LevelEnablerFunc to %s", tt.level) 49 } 50 } 51 52 func TestNewAtomicLevel(t *testing.T) { 53 lvl := NewAtomicLevel() 54 assert.Equal(t, InfoLevel, lvl.Level(), "Unexpected initial level.") 55 lvl.SetLevel(ErrorLevel) 56 assert.Equal(t, ErrorLevel, lvl.Level(), "Unexpected level after SetLevel.") 57 lvl = NewAtomicLevelAt(WarnLevel) 58 assert.Equal(t, WarnLevel, lvl.Level(), "Unexpected level after SetLevel.") 59 } 60 61 func TestParseAtomicLevel(t *testing.T) { 62 tests := []struct { 63 text string 64 level AtomicLevel 65 err string 66 }{ 67 {"info", NewAtomicLevel(), ""}, 68 {"DEBUG", NewAtomicLevelAt(DebugLevel), ""}, 69 {"FOO", NewAtomicLevel(), `unrecognized level: "FOO"`}, 70 } 71 72 for _, tt := range tests { 73 parsedAtomicLevel, err := ParseAtomicLevel(tt.text) 74 if len(tt.err) == 0 { 75 require.NoError(t, err) 76 assert.Equal(t, tt.level, parsedAtomicLevel) 77 } else { 78 assert.ErrorContains(t, err, tt.err) 79 } 80 } 81 } 82 83 func TestAtomicLevelMutation(t *testing.T) { 84 lvl := NewAtomicLevel() 85 lvl.SetLevel(WarnLevel) 86 // Trigger races for non-atomic level mutations. 87 proceed := make(chan struct{}) 88 wg := &sync.WaitGroup{} 89 runConcurrently(10, 100, wg, func() { 90 <-proceed 91 assert.Equal(t, WarnLevel, lvl.Level()) 92 }) 93 runConcurrently(10, 100, wg, func() { 94 <-proceed 95 lvl.SetLevel(WarnLevel) 96 }) 97 close(proceed) 98 wg.Wait() 99 } 100 101 func TestAtomicLevelText(t *testing.T) { 102 tests := []struct { 103 text string 104 expect zapcore.Level 105 err bool 106 }{ 107 {"debug", DebugLevel, false}, 108 {"info", InfoLevel, false}, 109 {"", InfoLevel, false}, 110 {"warn", WarnLevel, false}, 111 {"error", ErrorLevel, false}, 112 {"dpanic", DPanicLevel, false}, 113 {"panic", PanicLevel, false}, 114 {"fatal", FatalLevel, false}, 115 {"foobar", InfoLevel, true}, 116 } 117 118 for _, tt := range tests { 119 var lvl AtomicLevel 120 // Test both initial unmarshaling and overwriting existing value. 121 for i := 0; i < 2; i++ { 122 if tt.err { 123 assert.Error(t, lvl.UnmarshalText([]byte(tt.text)), "Expected unmarshaling %q to fail.", tt.text) 124 } else { 125 assert.NoError(t, lvl.UnmarshalText([]byte(tt.text)), "Expected unmarshaling %q to succeed.", tt.text) 126 } 127 assert.Equal(t, tt.expect, lvl.Level(), "Unexpected level after unmarshaling.") 128 lvl.SetLevel(InfoLevel) 129 } 130 131 // Test marshalling 132 if tt.text != "" && !tt.err { 133 lvl.SetLevel(tt.expect) 134 marshaled, err := lvl.MarshalText() 135 assert.NoError(t, err, `Unexpected error marshalling level "%v" to text.`, tt.expect) 136 assert.Equal(t, tt.text, string(marshaled), "Expected marshaled text to match") 137 assert.Equal(t, tt.text, lvl.String(), "Expected Stringer call to match") 138 } 139 } 140 }