github.com/m3db/m3@v1.5.0/src/x/instrument/invariant_test.go (about) 1 // Copyright (c) 2018 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 instrument_test 22 23 import ( 24 "fmt" 25 "testing" 26 27 "github.com/m3db/m3/src/x/instrument" 28 29 "github.com/stretchr/testify/require" 30 "github.com/uber-go/tally" 31 "go.uber.org/zap" 32 ) 33 34 func ExampleInvariantViolatedMetricInvocation() { 35 defer instrument.SetShouldPanicEnvironmentVariable(false)() 36 testScope := tally.NewTestScope("", nil) 37 opts := instrument.NewOptions().SetMetricsScope(testScope) 38 39 instrument.EmitInvariantViolation(opts) 40 counters := testScope.Snapshot().Counters() 41 counter := counters[instrument.InvariantViolatedMetricName+"+"] 42 fmt.Println(counter.Name(), counter.Value()) 43 // Output: invariant_violated 1 44 } 45 46 func TestEmitInvariantViolationDoesNotPanicIfEnvNotSet(t *testing.T) { 47 defer instrument.SetShouldPanicEnvironmentVariable(false)() 48 opts := instrument.NewOptions() 49 require.NotPanics(t, func() { 50 instrument.EmitInvariantViolation(opts) 51 }) 52 } 53 54 func TestEmitAndLogInvariantViolationDoesNotPanicIfEnvNotSet(t *testing.T) { 55 defer instrument.SetShouldPanicEnvironmentVariable(false)() 56 opts := instrument.NewOptions() 57 require.NotPanics(t, func() { 58 instrument.EmitAndLogInvariantViolation(opts, func(l *zap.Logger) { 59 l.Error("some_error") 60 }) 61 }) 62 } 63 64 func TestErrorfDoesNotPanicIfEnvNotSet(t *testing.T) { 65 defer instrument.SetShouldPanicEnvironmentVariable(false)() 66 var ( 67 format = "some error format: %s" 68 err_msg = "error message" 69 expectedErr = fmt.Errorf("invariant_violated: some error format: error message") 70 ) 71 require.NotPanics(t, func() { 72 require.Equal( 73 t, expectedErr, instrument.InvariantErrorf(format, err_msg)) 74 }) 75 } 76 77 func TestEmitInvariantViolationPanicsIfEnvSet(t *testing.T) { 78 defer instrument.SetShouldPanicEnvironmentVariable(true)() 79 80 opts := instrument.NewOptions() 81 require.Panics(t, func() { 82 instrument.EmitInvariantViolation(opts) 83 }) 84 } 85 86 func TestEmitAndLogInvariantViolationPanicsIfEnvSet(t *testing.T) { 87 defer instrument.SetShouldPanicEnvironmentVariable(true)() 88 89 opts := instrument.NewOptions() 90 require.Panics(t, func() { 91 instrument.EmitAndLogInvariantViolation(opts, func(l *zap.Logger) { 92 l.Error("some_error") 93 }) 94 }) 95 } 96 97 func TestErrorfPanicsIfEnvSet(t *testing.T) { 98 defer instrument.SetShouldPanicEnvironmentVariable(true)() 99 require.Panics(t, func() { 100 instrument.InvariantErrorf("some_error") 101 }) 102 }