github.com/m3db/m3@v1.5.0/src/query/storage/m3/consolidators/step_consolidator_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 consolidators 22 23 import ( 24 "fmt" 25 "math" 26 "testing" 27 "time" 28 29 "github.com/stretchr/testify/assert" 30 31 "github.com/m3db/m3/src/dbnode/ts" 32 "github.com/m3db/m3/src/query/test/compare" 33 xtime "github.com/m3db/m3/src/x/time" 34 ) 35 36 func TestConsolidator(t *testing.T) { 37 lookback := time.Minute 38 start := xtime.Now().Truncate(time.Hour) 39 fn := TakeLast 40 consolidator := NewStepLookbackConsolidator( 41 lookback, 42 lookback, 43 start, 44 fn, 45 ) 46 47 // NB: lookback limit: start-1 48 consolidator.BufferStep() 49 50 consolidator.AddPoint(ts.Datapoint{TimestampNanos: start, Value: 1}) 51 consolidator.AddPoint(ts.Datapoint{ 52 TimestampNanos: start.Add(time.Minute), 53 Value: 10, 54 }) 55 consolidator.BufferStep() 56 consolidator.BufferStep() 57 consolidator.BufferStep() 58 consolidator.AddPoint(ts.Datapoint{ 59 TimestampNanos: start.Add(2*time.Minute + time.Second*30), 60 Value: 2, 61 }) 62 consolidator.AddPoint(ts.Datapoint{ 63 TimestampNanos: start.Add(3*time.Minute + time.Second), 64 Value: 3, 65 }) 66 consolidator.BufferStep() 67 68 actual := consolidator.ConsolidateAndMoveToNext() 69 compare.EqualsWithNans(t, nan, actual) 70 71 // NB: lookback limit: start 72 actual = consolidator.ConsolidateAndMoveToNext() 73 assert.Equal(t, 10.0, actual) 74 75 // NB: lookback limit: start+1, point 1 is outside of the lookback period 76 actual = consolidator.ConsolidateAndMoveToNext() 77 assert.Equal(t, 10.0, actual) 78 79 // NB: lookback limit: start+2 both points outside of the lookback period 80 actual = consolidator.ConsolidateAndMoveToNext() 81 assert.True(t, math.IsNaN(actual), fmt.Sprintf("%f should be nan", actual)) 82 83 // NB: lookback limit: start+3, both points in lookback period 84 actual = consolidator.ConsolidateAndMoveToNext() 85 assert.Equal(t, 3.0, actual) 86 }