github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/limits/source_logger_test.go (about) 1 // Copyright (c) 2020 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 limits 22 23 import ( 24 "math" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29 "go.uber.org/zap" 30 "go.uber.org/zap/zapcore" 31 32 "github.com/m3db/m3/src/x/instrument" 33 ) 34 35 func newTestZapHookOptions(logs *[]string) zap.Option { 36 // NB: allows inspection of incoming logs by writing them to slice. 37 return zap.Hooks(func(entry zapcore.Entry) error { 38 *logs = append(*logs, entry.Message) 39 return nil 40 }) 41 } 42 43 func TestNewSourceLoggerAboveDebugLevelDoesNotLog(t *testing.T) { 44 testSourceLoggerWithLevel(t, false) 45 } 46 47 func TestNewSourceLoggerAtDebugLevelLogsWhenExceeded(t *testing.T) { 48 testSourceLoggerWithLevel(t, true) 49 } 50 51 func testSourceLoggerWithLevel(t *testing.T, debugLog bool) { 52 var ( 53 err error 54 logger *zap.Logger 55 56 writtenLogs = []string{} 57 zapHookOpts = newTestZapHookOptions(&writtenLogs) 58 ) 59 60 if debugLog { 61 logger, err = zap.NewDevelopment(zapHookOpts) 62 } else { 63 logger, err = zap.NewProduction(zapHookOpts) 64 } 65 66 require.NoError(t, err) 67 var ( 68 iOpts = instrument.NewOptions().SetLogger(logger) 69 builder = &sourceLoggerBuilder{} 70 sourceLogger = builder.NewSourceLogger("name", iOpts) 71 ) 72 73 sourceLogger.LogSourceValue(1, []byte("foo")) 74 assert.Equal(t, 0, len(writtenLogs)) 75 76 sourceLogger.LogSourceValue(defaultLimit-1, []byte("defaultLimit-1")) 77 assert.Equal(t, 0, len(writtenLogs)) 78 79 sourceLogger.LogSourceValue(defaultLimit, []byte("defaultLimit")) 80 if debugLog { 81 assert.Equal(t, 1, len(writtenLogs)) 82 } else { 83 assert.Equal(t, 0, len(writtenLogs)) 84 } 85 86 sourceLogger.LogSourceValue(math.MaxInt64, []byte("maxInt")) 87 if debugLog { 88 assert.Equal(t, 2, len(writtenLogs)) 89 } else { 90 assert.Equal(t, 0, len(writtenLogs)) 91 } 92 }