github.com/m3db/m3@v1.5.0/src/x/pool/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 pool
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestBytesPool(t *testing.T) {
    30  	p := getBytesPool(2, []int{5, 10})
    31  	p.Init()
    32  
    33  	assert.Equal(t, []byte(nil), p.Get(0))
    34  
    35  	b1 := p.Get(1)
    36  	assert.Equal(t, 0, len(b1))
    37  	assert.Equal(t, 5, cap(b1))
    38  	b1 = append(b1, 'a')
    39  
    40  	b2 := p.Get(3)
    41  	assert.Equal(t, 0, len(b2))
    42  	assert.Equal(t, 5, cap(b2))
    43  	b2 = append(b1, 'b')
    44  	assert.NotEqual(t, b1, b2)
    45  	p.Put(b1)
    46  
    47  	b3 := p.Get(2)
    48  	assert.Equal(t, 0, len(b3))
    49  	assert.Equal(t, 5, cap(b3))
    50  	assert.Equal(t, b1, b3[:1])
    51  }
    52  
    53  func TestBytesPoolGetLargerThanLargestBucket(t *testing.T) {
    54  	p := getBytesPool(2, []int{8})
    55  	p.Init()
    56  
    57  	x := p.Get(16)
    58  	assert.NotNil(t, x)
    59  	assert.Equal(t, 16, cap(x))
    60  	assert.Equal(t, 0, len(x))
    61  
    62  	// Assert not from pool
    63  	bucketed := p.pool.(*bucketizedObjectPool)
    64  	assert.Equal(t, 1, len(bucketed.buckets))
    65  	assert.Equal(t, 2, len(bucketed.buckets[0].pool.(*objectPool).values))
    66  }
    67  
    68  func TestAppendByte(t *testing.T) {
    69  	p := getBytesPool(1, []int{3, 10})
    70  	p.Init()
    71  	vals := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
    72  
    73  	b1 := p.Get(2)
    74  	b2 := b1
    75  	for _, val := range vals {
    76  		b2 = AppendByte(b2, val, p)
    77  	}
    78  
    79  	assert.Equal(t, b2, vals)
    80  	assert.Equal(t, 9, len(b2))
    81  	assert.Equal(t, 10, cap(b2))
    82  
    83  	b3 := p.Get(2)
    84  	assert.Equal(t, cap(b1), cap(b3))
    85  	assert.Equal(t, b1[:3], b3[:3])
    86  }
    87  
    88  func getBytesPool(bucketSizes int, bucketCaps []int) *bytesPool {
    89  	buckets := make([]Bucket, len(bucketCaps))
    90  	for i, cap := range bucketCaps {
    91  		buckets[i] = Bucket{
    92  			Count:    Size(bucketSizes),
    93  			Capacity: cap,
    94  		}
    95  	}
    96  
    97  	return NewBytesPool(buckets, nil).(*bytesPool)
    98  }