github.com/m3db/m3@v1.5.0/src/dbnode/storage/util_test.go (about) 1 // Copyright (c) 2017 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 storage 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/require" 28 29 xtime "github.com/m3db/m3/src/x/time" 30 ) 31 32 func TestNumIntervals(t *testing.T) { 33 var ( 34 bs = 10 35 timeFor = func(i int) xtime.UnixNano { 36 return xtime.FromSeconds(int64(bs * i)) 37 } 38 ) 39 40 w := time.Second * time.Duration(bs) 41 t0 := timeFor(0) 42 t1 := timeFor(1) 43 44 require.Equal(t, 0, numIntervals(t1, t0, w)) 45 require.Equal(t, 1, numIntervals(t0, t0, w)) 46 require.Equal(t, 2, numIntervals(t0, t1, w)) 47 } 48 49 func TestNumIntervalsStartEqualsEnd(t *testing.T) { 50 s := xtime.Now() 51 w := time.Minute 52 require.Equal(t, 1, numIntervals(s, s, w)) 53 } 54 55 func TestNumIntervalsStartAfterEnd(t *testing.T) { 56 w := time.Minute 57 end := xtime.Now() 58 start := end.Add(w) 59 require.Equal(t, 0, numIntervals(start, end, w)) 60 } 61 62 func TestNumIntervalsZeroWindow(t *testing.T) { 63 w := time.Duration(0) 64 s := xtime.Now() 65 require.Equal(t, 0, numIntervals(s, s, w)) 66 } 67 68 func TestNumIntervalsNegativeWindow(t *testing.T) { 69 w := -time.Minute 70 s := xtime.Now() 71 require.Equal(t, 0, numIntervals(s, s, w)) 72 } 73 74 func TestTimesInRange(t *testing.T) { 75 var ( 76 w = 10 * time.Second 77 timeFor = func(i int64) xtime.UnixNano { 78 return xtime.UnixNano(int64(w) * i) 79 } 80 timesForN = func(i, j int64) []xtime.UnixNano { 81 times := make([]xtime.UnixNano, 0, j-i+1) 82 for k := j; k >= i; k-- { 83 times = append(times, timeFor(k)) 84 } 85 return times 86 } 87 ) 88 // [0, 2] with a gap of 1 ==> [0, 1, 2] 89 require.Equal(t, []xtime.UnixNano{timeFor(2), timeFor(1), timeFor(0)}, 90 timesInRange(timeFor(0), timeFor(2), w)) 91 92 // [2, 1] with a gap of 1 ==> empty result 93 require.Empty(t, timesInRange(timeFor(2), timeFor(1), w)) 94 95 // [2, 2] with a gap of 1 ==> [2] 96 require.Equal(t, []xtime.UnixNano{timeFor(2)}, 97 timesInRange(timeFor(2), timeFor(2), w)) 98 99 // [0, 9] with a gap of 3 ==> [9, 6, 3, 0] 100 require.Equal(t, []xtime.UnixNano{timeFor(9), timeFor(6), timeFor(3), timeFor(0)}, 101 timesInRange(timeFor(0), timeFor(9), 3*w)) 102 103 // [1, 9] with a gap of 3 ==> [9, 6, 3] 104 require.Equal(t, []xtime.UnixNano{timeFor(9), timeFor(6), timeFor(3)}, 105 timesInRange(timeFor(1), timeFor(9), 3*w)) 106 107 // [1, 100] with a gap of 1 ==> [100, 99, ..., 1] 108 require.Equal(t, timesForN(1, 100), timesInRange(timeFor(1), timeFor(100), w)) 109 }