github.com/m3db/m3@v1.5.0/src/metrics/policy/retention_test.go (about) 1 // Copyright (c) 2016 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 policy 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestValidRetentionValue(t *testing.T) { 31 inputs := []RetentionValue{ 32 OneHour, 33 SixHours, 34 TwelveHours, 35 OneDay, 36 TwoDays, 37 SevenDays, 38 FourteenDays, 39 ThirtyDays, 40 FourtyFiveDays, 41 } 42 expected := []time.Duration{ 43 time.Hour, 44 6 * time.Hour, 45 12 * time.Hour, 46 24 * time.Hour, 47 2 * 24 * time.Hour, 48 7 * 24 * time.Hour, 49 14 * 24 * time.Hour, 50 30 * 24 * time.Hour, 51 45 * 24 * time.Hour, 52 } 53 for i, value := range inputs { 54 retention, err := value.Retention() 55 require.NoError(t, err) 56 require.Equal(t, expected[i], retention.Duration()) 57 require.Equal(t, Retention(expected[i]), retention) 58 require.True(t, value.IsValid()) 59 } 60 } 61 62 func TestInvalidRetentionValue(t *testing.T) { 63 inputs := []RetentionValue{ 64 UnknownRetentionValue, 65 RetentionValue(100), 66 } 67 for _, value := range inputs { 68 _, err := value.Retention() 69 require.Equal(t, errUnknownRetentionValue, err) 70 require.False(t, value.IsValid()) 71 } 72 } 73 74 func TestValidRetention(t *testing.T) { 75 inputs := []Retention{ 76 Retention(time.Hour), 77 Retention(6 * time.Hour), 78 Retention(12 * time.Hour), 79 Retention(24 * time.Hour), 80 Retention(2 * 24 * time.Hour), 81 Retention(7 * 24 * time.Hour), 82 Retention(14 * 24 * time.Hour), 83 Retention(30 * 24 * time.Hour), 84 Retention(45 * 24 * time.Hour), 85 } 86 expected := []RetentionValue{ 87 OneHour, 88 SixHours, 89 TwelveHours, 90 OneDay, 91 TwoDays, 92 SevenDays, 93 FourteenDays, 94 ThirtyDays, 95 FourtyFiveDays, 96 } 97 for i, retention := range inputs { 98 value, err := ValueFromRetention(retention) 99 require.NoError(t, err) 100 require.Equal(t, expected[i], value) 101 } 102 } 103 104 func TestInvalidRetention(t *testing.T) { 105 inputs := []Retention{ 106 Retention(time.Nanosecond), 107 Retention(37 * time.Hour), 108 } 109 for _, retention := range inputs { 110 _, err := ValueFromRetention(retention) 111 require.Equal(t, errUnknownRetention, err) 112 } 113 } 114 115 func TestParseRetention(t *testing.T) { 116 inputs := []struct { 117 str string 118 expected Retention 119 }{ 120 { 121 str: "1s", 122 expected: Retention(time.Second), 123 }, 124 { 125 str: "10s", 126 expected: Retention(10 * time.Second), 127 }, 128 { 129 str: "1m", 130 expected: Retention(time.Minute), 131 }, 132 { 133 str: "10m", 134 expected: Retention(10 * time.Minute), 135 }, 136 { 137 str: "1h", 138 expected: Retention(time.Hour), 139 }, 140 { 141 str: "1d", 142 expected: Retention(24 * time.Hour), 143 }, 144 { 145 str: "7d", 146 expected: Retention(24 * 7 * time.Hour), 147 }, 148 { 149 str: "30d", 150 expected: Retention(24 * 30 * time.Hour), 151 }, 152 { 153 str: "24h0m0s", 154 expected: Retention(24 * time.Hour), 155 }, 156 } 157 158 for _, input := range inputs { 159 res, err := ParseRetention(input.str) 160 require.NoError(t, err) 161 require.Equal(t, input.expected, res) 162 } 163 } 164 165 func TestParseRetentionError(t *testing.T) { 166 inputs := []string{ 167 "4", 168 "", 169 "4minutes", 170 "4d3", 171 ",3490", 172 } 173 174 for _, input := range inputs { 175 _, err := ParseRetention(input) 176 require.Error(t, err) 177 } 178 } 179 180 func TestRetentionString(t *testing.T) { 181 inputs := []struct { 182 retention Retention 183 expected string 184 }{ 185 { 186 retention: Retention(time.Second), 187 expected: "1s", 188 }, 189 { 190 retention: Retention(10 * time.Second), 191 expected: "10s", 192 }, 193 { 194 retention: Retention(time.Minute), 195 expected: "1m", 196 }, 197 { 198 retention: Retention(10 * time.Minute), 199 expected: "10m", 200 }, 201 { 202 retention: Retention(time.Hour), 203 expected: "1h", 204 }, 205 { 206 retention: Retention(24 * time.Hour), 207 expected: "1d", 208 }, 209 { 210 retention: Retention(24 * 7 * time.Hour), 211 expected: "7d", 212 }, 213 { 214 retention: Retention(24 * 30 * time.Hour), 215 expected: "30d", 216 }, 217 } 218 219 for _, input := range inputs { 220 require.Equal(t, input.expected, input.retention.String()) 221 } 222 } 223 224 func TestRetentionRoundTrip(t *testing.T) { 225 inputs := []Retention{ 226 Retention(time.Second), 227 Retention(10 * time.Second), 228 Retention(time.Minute), 229 Retention(10 * time.Minute), 230 Retention(time.Hour), 231 Retention(24 * time.Hour), 232 Retention(24 * 7 * time.Hour), 233 Retention(24 * 30 * time.Hour), 234 Retention(24 * 180 * time.Hour), 235 } 236 237 for _, input := range inputs { 238 str := input.String() 239 res, err := ParseRetention(str) 240 require.NoError(t, err) 241 require.Equal(t, input, res) 242 } 243 }