github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/storage/m3/consolidators/step_accumulator_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 consolidators
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/dbnode/ts"
    28  	xts "github.com/m3db/m3/src/query/ts"
    29  	xtime "github.com/m3db/m3/src/x/time"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  )
    33  
    34  func TestAccumulator(t *testing.T) {
    35  	lookback := time.Minute
    36  	start := xtime.Now().Truncate(time.Hour)
    37  	acc := NewStepLookbackAccumulator(
    38  		lookback,
    39  		lookback,
    40  		start,
    41  	)
    42  
    43  	// NB: lookback limit: start-1
    44  	actual := acc.AccumulateAndMoveToNext()
    45  	assert.Nil(t, actual)
    46  
    47  	acc.AddPoint(ts.Datapoint{TimestampNanos: start, Value: 1})
    48  	acc.AddPoint(ts.Datapoint{TimestampNanos: start.Add(time.Minute), Value: 10})
    49  	acc.BufferStep()
    50  	acc.BufferStep()
    51  	acc.BufferStep()
    52  	acc.AddPoint(ts.Datapoint{
    53  		TimestampNanos: start.Add(2*time.Minute + time.Second*30),
    54  		Value:          2,
    55  	})
    56  	acc.AddPoint(ts.Datapoint{
    57  		TimestampNanos: start.Add(3*time.Minute + time.Second),
    58  		Value:          3,
    59  	})
    60  	acc.BufferStep()
    61  
    62  	// NB: lookback limit: start
    63  	actual = acc.AccumulateAndMoveToNext()
    64  	assert.Equal(t, []xts.Datapoint{
    65  		{Timestamp: start, Value: 1},
    66  		{Timestamp: start.Add(time.Minute), Value: 10},
    67  	}, actual)
    68  
    69  	// NB: lookback limit: start+1, should be reset
    70  	actual = acc.AccumulateAndMoveToNext()
    71  	assert.Nil(t, actual)
    72  
    73  	// NB: lookback limit: start+2, should be reset
    74  	actual = acc.AccumulateAndMoveToNext()
    75  	assert.Nil(t, actual)
    76  
    77  	// NB: lookback limit: start+3, both points in lookback period
    78  	actual = acc.AccumulateAndMoveToNext()
    79  	assert.Equal(t, []xts.Datapoint{
    80  		{Timestamp: start.Add(2*time.Minute + time.Second*30), Value: 2},
    81  		{Timestamp: start.Add(3*time.Minute + time.Second), Value: 3},
    82  	}, actual)
    83  }