github.com/m3db/m3@v1.5.0/src/x/ident/identifier_pool_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 ident 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/x/checked" 27 "github.com/m3db/m3/src/x/context" 28 "github.com/m3db/m3/src/x/pool" 29 30 "github.com/stretchr/testify/suite" 31 ) 32 33 func TestSimpleIDPool(t *testing.T) { 34 s := &idPoolTestSuite{ 35 pool: newTestSimplePool(), 36 } 37 suite.Run(t, s) 38 } 39 40 type idPoolTestSuite struct { 41 suite.Suite 42 pool Pool 43 } 44 45 func (s idPoolTestSuite) TestPoolGetClone() { 46 ctx := context.NewBackground() 47 48 a := s.pool.GetStringID(ctx, "abc") 49 b := s.pool.Clone(a) 50 51 s.Require().True(a.Equal(b)) 52 53 ctx.BlockingClose() 54 55 s.Require().Nil(a.Bytes()) 56 s.Require().NotEmpty(b.Bytes()) 57 } 58 59 func (s idPoolTestSuite) TestPoolStringRefs() { 60 a := s.pool.StringID("abc") 61 s.Require().Equal(1, a.(*id).data.NumRef()) 62 } 63 64 func (s idPoolTestSuite) TestPoolBinaryRefs() { 65 v := checked.NewBytes([]byte("abc"), nil) 66 s.Require().Equal(0, v.NumRef()) 67 68 a := s.pool.BinaryID(v) 69 s.Require().Equal(1, a.(*id).data.NumRef()) 70 71 b := s.pool.Clone(a) 72 s.Require().Equal(1, b.(*id).data.NumRef()) 73 s.Require().Equal(1, a.(*id).data.NumRef()) 74 } 75 76 func (s idPoolTestSuite) TestPoolGetBinaryID() { 77 v := checked.NewBytes([]byte("abc"), nil) 78 nr := v.NumRef() 79 80 ctx := context.NewBackground() 81 bid := s.pool.GetBinaryID(ctx, v) 82 83 s.Require().Equal(1+nr, v.NumRef()) 84 85 ctx.BlockingClose() 86 s.Require().Nil(bid.(*id).data) 87 } 88 89 func (s idPoolTestSuite) TestPoolBinaryID() { 90 v := checked.NewBytes([]byte("abc"), nil) 91 v.IncRef() 92 nr := v.NumRef() 93 94 bid := s.pool.BinaryID(v) 95 s.Require().Equal(1+nr, v.NumRef()) 96 v.DecRef() 97 bid.Finalize() 98 s.Require().Nil(bid.(*id).data) 99 s.Require().Equal(nr-1, v.NumRef()) 100 } 101 102 func (s idPoolTestSuite) TestPoolGetBinaryTag() { 103 tagName := checked.NewBytes([]byte("abc"), nil) 104 tagValue := checked.NewBytes([]byte("def"), nil) 105 nr := tagName.NumRef() 106 107 ctx := context.NewBackground() 108 tag := s.pool.GetBinaryTag(ctx, tagName, tagValue) 109 s.Require().Equal(1+nr, tagName.NumRef()) 110 s.Require().Equal(1+nr, tagValue.NumRef()) 111 ctx.BlockingClose() 112 s.Require().Nil(tag.Name.(*id).data) 113 s.Require().Nil(tag.Value.(*id).data) 114 s.Require().Equal(nr, tagName.NumRef()) 115 s.Require().Equal(nr, tagValue.NumRef()) 116 } 117 118 func (s idPoolTestSuite) TestPoolBinaryTag() { 119 tagName := checked.NewBytes([]byte("abc"), nil) 120 tagName.IncRef() 121 tagValue := checked.NewBytes([]byte("def"), nil) 122 tagValue.IncRef() 123 nr := tagName.NumRef() 124 vr := tagValue.NumRef() 125 126 tag := s.pool.BinaryTag(tagName, tagValue) 127 s.Require().Equal(1+nr, tagName.NumRef()) 128 s.Require().Equal(1+vr, tagValue.NumRef()) 129 130 tagName.DecRef() 131 tagValue.DecRef() 132 133 tag.Finalize() 134 s.Require().Nil(tag.Name) 135 s.Require().Equal(nr-1, tagName.NumRef()) 136 137 s.Require().Nil(tag.Value) 138 s.Require().Equal(vr-1, tagValue.NumRef()) 139 } 140 141 func (s idPoolTestSuite) TestPoolGetStringID() { 142 ctx := context.NewBackground() 143 sid := s.pool.GetStringID(ctx, "abc") 144 s.Require().Equal("abc", sid.String()) 145 146 ctx.BlockingClose() 147 s.Require().Nil(sid.(*id).data) 148 } 149 150 func (s idPoolTestSuite) TestPoolStringID() { 151 sid := s.pool.StringID("abc") 152 s.Require().Equal("abc", sid.String()) 153 154 sid.Finalize() 155 s.Require().Nil(sid.(*id).data) 156 } 157 158 func (s idPoolTestSuite) TestPoolTags() { 159 tags := s.pool.Tags() 160 tags.Append(s.pool.StringTag("foo", "000")) 161 tags.Append(s.pool.StringTag("bar", "111")) 162 s.Require().True(tags.Equal(NewTags( 163 StringTag("foo", "000"), 164 StringTag("bar", "111"), 165 ))) 166 tags.Finalize() 167 s.Require().Nil(tags.Values()) 168 } 169 170 func (s idPoolTestSuite) TestPoolGetTagsIterator() { 171 tags := s.pool.Tags() 172 tags.Append(s.pool.StringTag("foo", "000")) 173 tags.Append(s.pool.StringTag("bar", "111")) 174 175 ctx := context.NewBackground() 176 iter := s.pool.GetTagsIterator(ctx) 177 iter.Reset(tags) 178 179 s.Require().True(NewTagIterMatcher(iter).Matches( 180 NewTagsIterator(NewTags( 181 StringTag("foo", "000"), 182 StringTag("bar", "111"), 183 )), 184 )) 185 186 ctx.BlockingClose() 187 188 s.Require().Equal(tagsSlice{}, iter.(*tagSliceIter).backingSlice) 189 s.Require().Equal(-1, iter.(*tagSliceIter).currentIdx) 190 } 191 192 func (s idPoolTestSuite) TestPoolTagsIterator() { 193 tags := s.pool.Tags() 194 tags.Append(s.pool.StringTag("foo", "000")) 195 tags.Append(s.pool.StringTag("bar", "111")) 196 197 iter := s.pool.TagsIterator() 198 iter.Reset(tags) 199 200 s.Require().True(NewTagIterMatcher(iter).Matches( 201 NewTagsIterator(NewTags( 202 StringTag("foo", "000"), 203 StringTag("bar", "111"), 204 )), 205 )) 206 207 iter.Close() 208 209 s.Require().Equal(tagsSlice{}, iter.(*tagSliceIter).backingSlice) 210 s.Require().Equal(-1, iter.(*tagSliceIter).currentIdx) 211 } 212 213 func newTestSimplePool() Pool { 214 bytesPool := pool.NewCheckedBytesPool(nil, nil, 215 func(s []pool.Bucket) pool.BytesPool { 216 return pool.NewBytesPool(s, nil) 217 }) 218 bytesPool.Init() 219 return NewPool(bytesPool, PoolOptions{}) 220 }