github.com/m3db/m3@v1.5.0/src/query/storage/m3/encoded_series_iterator_test.go (about)

     1  // Copyright (c) 2018 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  	"time"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  
    31  	"github.com/m3db/m3/src/dbnode/encoding"
    32  	"github.com/m3db/m3/src/query/block"
    33  	"github.com/m3db/m3/src/query/test/compare"
    34  	"github.com/m3db/m3/src/query/ts"
    35  )
    36  
    37  func datapointsToFloatSlices(t *testing.T, dps []ts.Datapoints) [][]float64 {
    38  	vals := make([][]float64, len(dps))
    39  	for i, dp := range dps {
    40  		vals[i] = dp.Values()
    41  	}
    42  
    43  	return vals
    44  }
    45  
    46  func TestSeriesIterator(t *testing.T) {
    47  	expected := [][]float64{
    48  		{1, 2, 3, 4, 5, 6, 7, 8, 9},
    49  		{10, 20, 30, 40},
    50  		{100, 200, 300, 400, 500},
    51  	}
    52  
    53  	j := 0
    54  	opts := NewOptions(encoding.NewOptions()).
    55  		SetLookbackDuration(1 * time.Minute).
    56  		SetSplitSeriesByBlock(false)
    57  	require.NoError(t, opts.Validate())
    58  	blocks, bounds := generateBlocks(t, time.Minute, opts)
    59  	for i, block := range blocks {
    60  		iters, err := block.SeriesIter()
    61  		require.NoError(t, err)
    62  
    63  		require.True(t, bounds.Equals(block.Meta().Bounds))
    64  		verifyMetas(t, i, block.Meta(), iters.SeriesMeta())
    65  		for iters.Next() {
    66  			series := iters.Current()
    67  			actual := make([]float64, 0, len(series.Datapoints()))
    68  			for _, v := range series.Datapoints() {
    69  				actual = append(actual, v.Value)
    70  			}
    71  
    72  			compare.EqualsWithNans(t, expected[j], actual)
    73  			j++
    74  		}
    75  
    76  		require.Equal(t, len(expected), j)
    77  		require.NoError(t, iters.Err())
    78  	}
    79  }
    80  
    81  func verifySingleMeta(
    82  	t *testing.T,
    83  	i int,
    84  	meta block.Metadata,
    85  	metas []block.SeriesMeta,
    86  ) {
    87  	require.Equal(t, 0, meta.Tags.Len())
    88  	require.Equal(t, 1, len(metas))
    89  
    90  	m := metas[0]
    91  	assert.Equal(t, fmt.Sprintf("abc%d", i), string(m.Name))
    92  	require.Equal(t, 2, m.Tags.Len())
    93  
    94  	val, found := m.Tags.Get([]byte("a"))
    95  	assert.True(t, found)
    96  	assert.Equal(t, []byte("b"), val)
    97  
    98  	val, found = m.Tags.Get([]byte("c"))
    99  	assert.True(t, found)
   100  	assert.Equal(t, []byte(fmt.Sprint(i)), val)
   101  }
   102  
   103  func TestSeriesIteratorBatch(t *testing.T) {
   104  	expected := [][]float64{
   105  		{1, 2, 3, 4, 5, 6, 7, 8, 9},
   106  		{10, 20, 30, 40},
   107  		{100, 200, 300, 400, 500},
   108  	}
   109  
   110  	count := 0
   111  	opts := NewOptions(encoding.NewOptions()).
   112  		SetLookbackDuration(1 * time.Minute).
   113  		SetSplitSeriesByBlock(false)
   114  	require.NoError(t, opts.Validate())
   115  	blocks, bounds := generateBlocks(t, time.Minute, opts)
   116  	for _, bl := range blocks {
   117  		require.True(t, bounds.Equals(bl.Meta().Bounds))
   118  		iters, err := bl.MultiSeriesIter(3)
   119  		require.NoError(t, err)
   120  		require.Equal(t, 3, len(iters))
   121  
   122  		for i, itBatch := range iters {
   123  			iter := itBatch.Iter
   124  			require.Equal(t, 1, itBatch.Size)
   125  			verifySingleMeta(t, i, bl.Meta(), iter.SeriesMeta())
   126  			for iter.Next() {
   127  				series := iter.Current()
   128  				actual := make([]float64, 0, len(series.Datapoints()))
   129  				for _, v := range series.Datapoints() {
   130  					actual = append(actual, v.Value)
   131  				}
   132  
   133  				compare.EqualsWithNans(t, expected[i], actual)
   134  				count++
   135  			}
   136  
   137  			require.NoError(t, iter.Err())
   138  		}
   139  
   140  		assert.Equal(t, 3, count)
   141  	}
   142  }