github.com/m3db/m3@v1.5.0/src/dbnode/storage/block/result_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 block 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/x/ident" 27 "github.com/m3db/m3/src/x/pool" 28 xtime "github.com/m3db/m3/src/x/time" 29 30 "github.com/stretchr/testify/require" 31 ) 32 33 func testResultPoolOptions(size int) pool.ObjectPoolOptions { 34 return pool.NewObjectPoolOptions().SetSize(size) 35 } 36 37 func TestFetchBlockMetadataResultsPoolResetOnPut(t *testing.T) { 38 p := NewFetchBlockMetadataResultsPool(testResultPoolOptions(1), 64) 39 res := p.Get() 40 41 // Make res non-empty 42 res.Add(NewFetchBlockMetadataResult(xtime.Now(), 0, nil, 0, nil)) 43 require.Equal(t, 1, len(res.Results())) 44 45 // Return res to pool 46 p.Put(res) 47 48 // Verify res has been reset 49 res = p.Get() 50 require.Equal(t, 0, len(res.Results())) 51 } 52 53 func TestFetchBlockMetadataResultsPoolRejectLargeSliceOnPut(t *testing.T) { 54 p := NewFetchBlockMetadataResultsPool(testResultPoolOptions(1), 64) 55 res := p.Get() 56 57 // Make res a large slice 58 iter := 1024 59 for i := 0; i < iter; i++ { 60 res.Add(NewFetchBlockMetadataResult(xtime.Now(), 0, nil, 0, nil)) 61 } 62 require.True(t, cap(res.Results()) > 64) 63 64 // Return res to pool 65 p.Put(res) 66 67 // Verify res wasn't put into pool 68 res = p.Get() 69 require.Equal(t, 64, cap(res.Results())) 70 } 71 72 func TestFetchBlocksMetadataResultsPoolResetOnPut(t *testing.T) { 73 p := NewFetchBlocksMetadataResultsPool(testResultPoolOptions(1), 64) 74 res := p.Get() 75 76 // Make res non-empty 77 res.Add(NewFetchBlocksMetadataResult(ident.StringID("foo"), 78 ident.EmptyTagIterator, NewFetchBlockMetadataResults())) 79 require.Equal(t, 1, len(res.Results())) 80 81 // Return res to pool 82 p.Put(res) 83 84 // Verify res has been reset 85 res = p.Get() 86 require.Equal(t, 0, len(res.Results())) 87 } 88 89 func TestFetchBlocksMetadataResultsPoolRejectLargeSliceOnPut(t *testing.T) { 90 p := NewFetchBlocksMetadataResultsPool(testResultPoolOptions(1), 64) 91 res := p.Get() 92 93 // Make res a large slice 94 iter := 1024 95 for i := 0; i < iter; i++ { 96 res.Add(NewFetchBlocksMetadataResult(ident.StringID("foo"), 97 ident.EmptyTagIterator, NewFetchBlockMetadataResults())) 98 } 99 require.True(t, cap(res.Results()) > 64) 100 101 // Return res to pool 102 p.Put(res) 103 104 // Verify res wasn't put into pool 105 res = p.Get() 106 require.Equal(t, 64, cap(res.Results())) 107 }