github.com/m3db/m3@v1.5.0/src/x/checked/debug_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 checked 22 23 import ( 24 "errors" 25 "strings" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestSetPanicFn(t *testing.T) { 33 var err error 34 SetPanicFn(func(e error) { 35 err = e 36 }) 37 defer ResetPanicFn() 38 39 expected := errors.New("an error") 40 Panic(expected) 41 42 assert.Equal(t, expected, err) 43 } 44 45 func TestTracebackReadAfterFree(t *testing.T) { 46 EnableTracebacks() 47 defer DisableTracebacks() 48 SetTracebackCycles(2) 49 defer SetTracebackCycles(defaultTracebackCycles) 50 51 for i := 0; i < 1000; i++ { 52 elem := &struct { 53 RefCount 54 x int 55 }{ 56 x: 42, 57 } 58 59 finalized := 0 60 elem.SetOnFinalize(OnFinalizeFn(func() { 61 finalized++ 62 })) 63 64 elem.IncRef() 65 assert.Equal(t, 1, elem.NumRef()) 66 assert.Equal(t, 0, finalized) 67 68 elem.IncReads() 69 assert.Equal(t, 1, elem.NumReaders()) 70 71 elem.DecReads() 72 assert.Equal(t, 0, elem.NumReaders()) 73 74 elem.DecRef() 75 assert.Equal(t, 0, finalized) 76 77 elem.Finalize() 78 assert.Equal(t, 1, finalized) 79 80 var err error 81 SetPanicFn(func(e error) { 82 err = e 83 }) 84 85 elem.IncReads() 86 87 require.Error(t, err) 88 89 ResetPanicFn() 90 91 str := err.Error() 92 assert.True(t, strings.Contains(str, "read after free: reads=1, ref=0")) 93 assert.True(t, strings.Contains(str, "IncReads, ref=0, unixnanos=")) 94 assert.True(t, strings.Contains(str, "checked.(*RefCount).IncReads")) 95 assert.True(t, strings.Contains(str, "DecRef, ref=0, unixnanos=")) 96 assert.True(t, strings.Contains(str, "checked.(*RefCount).DecRef")) 97 assert.True(t, strings.Contains(str, "IncRef, ref=1, unixnanos=")) 98 assert.True(t, strings.Contains(str, "checked.(*RefCount).IncRef")) 99 } 100 } 101 102 func TestTracebackDoubleWrite(t *testing.T) { 103 EnableTracebacks() 104 defer DisableTracebacks() 105 SetTracebackCycles(2) 106 defer SetTracebackCycles(defaultTracebackCycles) 107 108 for i := 0; i < 1000; i++ { 109 elem := &struct { 110 RefCount 111 x int 112 }{ 113 x: 42, 114 } 115 116 finalized := 0 117 elem.SetOnFinalize(OnFinalizeFn(func() { 118 finalized++ 119 })) 120 121 elem.IncRef() 122 elem.DecRef() 123 assert.Equal(t, 0, elem.NumRef()) 124 125 elem.IncRef() 126 assert.Equal(t, 1, elem.NumRef()) 127 assert.Equal(t, 0, finalized) 128 129 elem.IncWrites() 130 elem.DecWrites() 131 assert.Equal(t, 0, elem.NumWriters()) 132 133 elem.DecRef() 134 135 elem.IncRef() 136 elem.MoveRef() 137 assert.Equal(t, 1, elem.NumRef()) 138 139 var err error 140 SetPanicFn(func(e error) { 141 err = e 142 }) 143 144 elem.IncWrites() 145 assert.Equal(t, 1, elem.NumWriters()) 146 147 elem.IncWrites() 148 149 require.Error(t, err) 150 151 ResetPanicFn() 152 153 str := err.Error() 154 assert.True(t, strings.Contains(str, "double write: writes=2, ref=1")) 155 assert.True(t, strings.Contains(str, "IncWrites, ref=1, unixnanos=")) 156 assert.True(t, strings.Contains(str, "checked.(*RefCount).IncWrites")) 157 assert.True(t, strings.Contains(str, "IncWrites, ref=1, unixnanos=")) 158 assert.True(t, strings.Contains(str, "checked.(*RefCount).IncWrites")) 159 assert.True(t, strings.Contains(str, "IncRef, ref=1, unixnanos=")) 160 assert.True(t, strings.Contains(str, "checked.(*RefCount).IncRef")) 161 } 162 }