github.com/m3db/m3@v1.5.0/src/dbnode/client/aggregate_op_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 client
    22  
    23  import (
    24  	"context"
    25  	"testing"
    26  
    27  	"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
    28  	"github.com/m3db/m3/src/x/pool"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestAggregateOpPool(t *testing.T) {
    34  	p := newAggregateOpPool(pool.NewObjectPoolOptions().SetSize(1))
    35  	p.Init()
    36  
    37  	op := p.Get()
    38  	require.Equal(t, p, op.pool)
    39  	p.Put(op)
    40  }
    41  
    42  func TestAggregateOp(t *testing.T) {
    43  	p := newAggregateOpPool(pool.NewObjectPoolOptions().SetSize(1))
    44  	p.Init()
    45  	op := p.Get()
    46  
    47  	var (
    48  		inter interface{}
    49  		err   error
    50  		count int
    51  	)
    52  
    53  	fn := func(i interface{}, e error) {
    54  		require.Equal(t, 0, count)
    55  		require.Equal(t, inter, i)
    56  		require.Equal(t, err, e)
    57  		count++
    58  	}
    59  	op.update(context.Background(), rpc.AggregateQueryRawRequest{}, fn)
    60  	op.CompletionFn()(inter, err)
    61  	require.Equal(t, 1, count)
    62  }
    63  
    64  func TestAggregateOpPoolInteraction(t *testing.T) {
    65  	p := newAggregateOpPool(pool.NewObjectPoolOptions().SetSize(1))
    66  	p.Init()
    67  	op := p.Get()
    68  
    69  	testPool := &testAggregateOpPool{t, op, false}
    70  	op.pool = testPool
    71  
    72  	op.incRef()
    73  	op.decRef()
    74  	require.True(t, testPool.called)
    75  	require.Nil(t, op.completionFn)
    76  	require.Equal(t, aggregateOpRequestZeroed, op.request)
    77  }
    78  
    79  type testAggregateOpPool struct {
    80  	t          *testing.T
    81  	expectedOp *aggregateOp
    82  	called     bool
    83  }
    84  
    85  var _ aggregateOpPool = &testAggregateOpPool{}
    86  
    87  func (p *testAggregateOpPool) Put(o *aggregateOp) {
    88  	require.False(p.t, p.called)
    89  	p.called = true
    90  	require.Equal(p.t, p.expectedOp, o)
    91  }
    92  
    93  func (p *testAggregateOpPool) Init()             { panic("not implemented") }
    94  func (p *testAggregateOpPool) Get() *aggregateOp { panic("not implemented") }