github.com/m3db/m3@v1.5.0/src/dbnode/ts/segment_test.go (about)

     1  // Copyright (c) 2019 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 ts
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/x/checked"
    27  	"github.com/m3db/m3/src/x/pool"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  var (
    33  	head = []byte{
    34  		0x20, 0xc5, 0x10, 0x55, 0x0, 0x0, 0x0, 0x0, 0x1f, 0x0, 0x0, 0x0, 0x0,
    35  		0xa0, 0x90, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5, 0x28, 0x3f, 0x2f,
    36  		0xc0, 0x1, 0xf4, 0x1, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x7, 0x10, 0x1e,
    37  		0x0, 0x1, 0x0, 0xe0, 0x65, 0x58,
    38  	}
    39  	tail = []byte{
    40  		0xcd, 0x3, 0x0, 0x0, 0x0, 0x0,
    41  	}
    42  
    43  	expected = []byte{
    44  		0x20, 0xc5, 0x10, 0x55, 0x0, 0x0, 0x0, 0x0, 0x1f, 0x0, 0x0, 0x0, 0x0,
    45  		0xa0, 0x90, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5, 0x28, 0x3f, 0x2f,
    46  		0xc0, 0x1, 0xf4, 0x1, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x7, 0x10, 0x1e,
    47  		0x0, 0x1, 0x0, 0xe0, 0x65, 0x58, 0xcd, 0x3, 0x0, 0x0, 0x0, 0x0,
    48  	}
    49  )
    50  
    51  type byteFunc func(d []byte) checked.Bytes
    52  
    53  var testChecksum uint32 = 2
    54  
    55  func testSegmentCloneWithPools(
    56  	t *testing.T,
    57  	checkd byteFunc,
    58  	pool pool.CheckedBytesPool,
    59  ) {
    60  	seg := NewSegment(checkd(head), checkd(tail), testChecksum, FinalizeNone)
    61  
    62  	assert.Equal(t, len(expected), seg.Len())
    63  	cloned := seg.Clone(pool)
    64  	assert.True(t, seg.Equal(&cloned))
    65  	assert.True(t, seg.Flags == FinalizeNone)
    66  
    67  	seg.Head.Reset(tail)
    68  	assert.Equal(t, len(tail)*2, seg.Len())
    69  
    70  	assert.False(t, seg.Equal(&cloned))
    71  	assert.Equal(t, len(expected), cloned.Len())
    72  	assert.True(t, cloned.Flags|FinalizeHead > 0)
    73  	assert.True(t, cloned.Flags|FinalizeTail > 0)
    74  	assert.Equal(t, testChecksum, cloned.checksum)
    75  
    76  	cloned.Finalize()
    77  	seg.Finalize()
    78  }
    79  
    80  func TestSegmentNoPools(t *testing.T) {
    81  	checkd := func(d []byte) checked.Bytes { return checked.NewBytes(d, nil) }
    82  	testSegmentCloneWithPools(t, checkd, nil)
    83  }
    84  
    85  func TestPooledSegmentClone(t *testing.T) {
    86  	bytesPool := pool.NewCheckedBytesPool([]pool.Bucket{pool.Bucket{
    87  		Capacity: 1024,
    88  		Count:    10,
    89  	}}, nil, func(s []pool.Bucket) pool.BytesPool {
    90  		return pool.NewBytesPool(s, nil)
    91  	})
    92  	bytesPool.Init()
    93  	checkd := func(d []byte) checked.Bytes {
    94  		b := bytesPool.Get(len(d))
    95  		b.IncRef()
    96  		b.Reset(d)
    97  		b.DecRef()
    98  		return b
    99  	}
   100  
   101  	testSegmentCloneWithPools(t, checkd, bytesPool)
   102  }