github.com/m3db/m3@v1.5.0/src/query/storage/m3/encoded_block_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 m3
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  
    27  	"github.com/golang/mock/gomock"
    28  
    29  	"github.com/m3db/m3/src/dbnode/encoding"
    30  	"github.com/m3db/m3/src/dbnode/ts"
    31  	"github.com/m3db/m3/src/query/block"
    32  	xtest "github.com/m3db/m3/src/x/test"
    33  	xtime "github.com/m3db/m3/src/x/time"
    34  
    35  	"github.com/stretchr/testify/assert"
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  func buildBlock(
    40  	count int,
    41  	now xtime.UnixNano,
    42  	ctrl *gomock.Controller,
    43  ) *encodedBlock {
    44  	iters := make([]encoding.SeriesIterator, count)
    45  	seriesMetas := make([]block.SeriesMeta, count)
    46  	for i := range iters {
    47  		it := encoding.NewMockSeriesIterator(ctrl)
    48  		it.EXPECT().Next().Return(true)
    49  		dp := ts.Datapoint{
    50  			TimestampNanos: now,
    51  			Value:          float64(i),
    52  		}
    53  
    54  		it.EXPECT().Current().Return(dp, xtime.Second, nil)
    55  		it.EXPECT().Next().Return(false)
    56  		it.EXPECT().Err().Return(nil)
    57  		it.EXPECT().Close()
    58  		iters[i] = it
    59  		seriesMetas[i] = block.SeriesMeta{Name: []byte(fmt.Sprint(i))}
    60  	}
    61  
    62  	return &encodedBlock{
    63  		seriesBlockIterators: iters,
    64  		seriesMetas:          seriesMetas,
    65  		options:              NewOptions(encoding.NewOptions()),
    66  	}
    67  }
    68  
    69  func TestMultiSeriesIter(t *testing.T) {
    70  	ctrl := xtest.NewController(t)
    71  	defer ctrl.Finish()
    72  
    73  	now := xtime.Now()
    74  	tests := []struct {
    75  		concurrency int
    76  		sizes       []int
    77  	}{
    78  		{1, []int{12}},
    79  		{2, []int{6, 6}},
    80  		{3, []int{4, 4, 4}},
    81  		{4, []int{3, 3, 3, 3}},
    82  		{5, []int{3, 3, 2, 2, 2}},
    83  		{6, []int{2, 2, 2, 2, 2, 2}},
    84  		{7, []int{2, 2, 2, 2, 2, 1, 1}},
    85  		{8, []int{2, 2, 2, 2, 1, 1, 1, 1}},
    86  		{9, []int{2, 2, 2, 1, 1, 1, 1, 1, 1}},
    87  		{10, []int{2, 2, 1, 1, 1, 1, 1, 1, 1, 1}},
    88  		{11, []int{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},
    89  		{12, []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},
    90  		{13, []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}},
    91  	}
    92  
    93  	for _, tt := range tests {
    94  		b := buildBlock(12, now, ctrl)
    95  		batch, err := b.MultiSeriesIter(tt.concurrency)
    96  		require.NoError(t, err)
    97  
    98  		count := 0
    99  		iterCount := 0.0
   100  		for i, b := range batch {
   101  			require.Equal(t, tt.sizes[i], b.Size)
   102  			iter := b.Iter
   103  			require.Equal(t, tt.sizes[i], iter.SeriesCount())
   104  
   105  			// Ensure that all metas are split as expected.
   106  			metas := iter.SeriesMeta()
   107  			for _, m := range metas {
   108  				assert.Equal(t, string(m.Name), fmt.Sprint(count))
   109  				count++
   110  			}
   111  
   112  			// Ensure that all iterators are split as expected.
   113  			for iter.Next() {
   114  				vals := iter.Current().Datapoints().Values()
   115  				require.Equal(t, 1, len(vals))
   116  				assert.Equal(t, iterCount, vals[0])
   117  				iterCount++
   118  			}
   119  
   120  			assert.NoError(t, iter.Err())
   121  		}
   122  
   123  		assert.NoError(t, b.Close())
   124  	}
   125  }
   126  
   127  func TestMultiSeriesIterError(t *testing.T) {
   128  	b := &encodedBlock{options: NewOptions(encoding.NewOptions())}
   129  	_, err := b.MultiSeriesIter(0)
   130  	require.Error(t, err)
   131  
   132  	_, err = b.MultiSeriesIter(-1)
   133  	require.Error(t, err)
   134  }