github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/checked/bytes_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 "runtime/debug" 25 "strings" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestBytes(t *testing.T) { 32 raw := make([]byte, 3, 5) 33 copy(raw, []byte{'a', 'b', 'c'}) 34 35 var onFinalize BytesFinalizerFn 36 finalizer := BytesFinalizerFn(func(finalizing Bytes) { 37 onFinalize(finalizing) 38 }) 39 40 b := NewBytes(raw, NewBytesOptions().SetFinalizer(finalizer)) 41 b.IncRef() 42 43 assert.Equal(t, []byte("abc"), b.Bytes()) 44 assert.Equal(t, 3, b.Len()) 45 assert.Equal(t, 5, b.Cap()) 46 47 b.Append('d') 48 b.AppendAll([]byte{'e', 'f'}) 49 50 assert.Equal(t, []byte("abcdef"), b.Bytes()) 51 assert.Equal(t, 6, b.Len()) 52 53 b.Resize(4) 54 assert.Equal(t, []byte("abcd"), b.Bytes()) 55 assert.Equal(t, 4, b.Len()) 56 57 b.Reset([]byte{'x', 'y', 'z'}) 58 assert.Equal(t, []byte("xyz"), b.Bytes()) 59 assert.Equal(t, 3, b.Len()) 60 61 b.DecRef() 62 63 finalizerCalls := 0 64 onFinalize = func(finalizing Bytes) { 65 // Ensure that RefCount.Finalize was called before the bytesRef finalizer. 66 stack := debug.Stack() 67 refCountFinalizeLineNumber := -1 68 bytesRefOnFinalizeLineNumber := -1 69 for idx, line := range strings.Split(string(stack), "\n") { 70 if strings.Contains(line, "(*bytesRef).OnFinalize") { 71 bytesRefOnFinalizeLineNumber = idx 72 } 73 if strings.Contains(line, "(*RefCount).Finalize") { 74 refCountFinalizeLineNumber = idx 75 } 76 } 77 assert.True(t, (refCountFinalizeLineNumber > -1) && (bytesRefOnFinalizeLineNumber > -1)) 78 assert.True(t, refCountFinalizeLineNumber > bytesRefOnFinalizeLineNumber) 79 80 // Ensure closing the ref we created 81 assert.Equal(t, b, finalizing) 82 finalizing.IncRef() 83 assert.Equal(t, []byte("xyz"), finalizing.Bytes()) 84 finalizing.DecRef() 85 finalizerCalls++ 86 } 87 88 b.Finalize() 89 assert.Equal(t, 1, finalizerCalls) 90 }