github.com/m3db/m3@v1.5.0/src/dbnode/encoding/annotation_holder_test.go (about) 1 // Copyright (c) 2021 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 encoding 22 23 import ( 24 "bytes" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 29 "github.com/m3db/m3/src/dbnode/ts" 30 ) 31 32 var annotationHolderTests = []struct { 33 name string 34 annotation ts.Annotation 35 }{ 36 { 37 name: "nil", 38 }, 39 { 40 name: "empty", 41 annotation: []byte{}, 42 }, 43 { 44 name: "short", 45 annotation: []byte{1, 2, 3}, 46 }, 47 { 48 name: "OptimizedAnnotationLen - 1", 49 annotation: repeat(1, ts.OptimizedAnnotationLen-1), 50 }, 51 { 52 name: "OptimizedAnnotationLen", 53 annotation: repeat(2, ts.OptimizedAnnotationLen), 54 }, 55 { 56 name: "OptimizedAnnotationLen + 1", 57 annotation: repeat(3, ts.OptimizedAnnotationLen+1), 58 }, 59 { 60 name: "OptimizedAnnotationLen * 2", 61 annotation: repeat(4, ts.OptimizedAnnotationLen*2), 62 }, 63 } 64 65 func TestAnnotationHolder(t *testing.T) { 66 holder := annotationHolder{} 67 assert.Nil(t, holder.get()) 68 69 for _, tt := range annotationHolderTests { 70 t.Run(tt.name, func(t *testing.T) { 71 holder.set(tt.annotation) 72 if len(tt.annotation) == 0 { 73 assert.Empty(t, holder.get()) 74 } else { 75 assert.Equal(t, tt.annotation, holder.get()) 76 } 77 }) 78 } 79 } 80 81 func TestAnnotationHolderWithReset(t *testing.T) { 82 holder := annotationHolder{} 83 holder.reset() 84 assert.Nil(t, holder.get()) 85 86 for _, tt := range annotationHolderTests { 87 t.Run(tt.name, func(t *testing.T) { 88 holder.set(tt.annotation) 89 if len(tt.annotation) == 0 { 90 assert.Empty(t, holder.get()) 91 } else { 92 assert.Equal(t, tt.annotation, holder.get()) 93 } 94 holder.reset() 95 assert.Nil(t, holder.get()) 96 }) 97 } 98 } 99 100 func repeat(b byte, n int) ts.Annotation { 101 return bytes.Repeat([]byte{b}, n) 102 }