MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestFindMinMaxTime

Function TestFindMinMaxTime

pkg/util/time_test.go:142–193  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

140}
141
142func TestFindMinMaxTime(t *testing.T) {
143 now := time.Now()
144
145 type testCase struct {
146 query string
147 lookbackDelta time.Duration
148 queryStartTime time.Time
149 queryEndTime time.Time
150 expectedMinTime time.Time
151 expectedMaxTime time.Time
152 }
153
154 tests := map[string]testCase{
155 "should consider min and max of the query param": {
156 query: "up",
157 queryStartTime: now.Add(-1 * time.Hour),
158 queryEndTime: now,
159 expectedMinTime: now.Add(-1 * time.Hour),
160 expectedMaxTime: now,
161 },
162 "should consider min and max of inner queries": {
163 query: "go_gc_duration_seconds_count[2h] offset 30m + go_gc_duration_seconds_count[3h] offset 1h",
164 queryStartTime: now.Add(-1 * time.Hour),
165 queryEndTime: now,
166 expectedMinTime: now.Add(-5 * time.Hour),
167 expectedMaxTime: now.Add(-30 * time.Minute),
168 },
169 "should consider lookback delta": {
170 query: "up",
171 lookbackDelta: 1 * time.Hour,
172 queryStartTime: now.Add(-1 * time.Hour),
173 queryEndTime: now,
174 expectedMinTime: now.Add(-2 * time.Hour),
175 expectedMaxTime: now,
176 },
177 }
178
179 for testName, testData := range tests {
180 t.Run(testName, func(t *testing.T) {
181 expr, _ := cortexparser.ParseExpr(testData.query)
182
183 url := "/query_range?query=" + testData.query +
184 "&start=" + strconv.FormatInt(testData.queryStartTime.Truncate(time.Minute).Unix(), 10) +
185 "&end=" + strconv.FormatInt(testData.queryEndTime.Truncate(time.Minute).Unix(), 10)
186 req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader([]byte{}))
187
188 minTime, maxTime := FindMinMaxTime(req, expr, testData.lookbackDelta, now)
189 assert.Equal(t, testData.expectedMinTime.Truncate(time.Minute).UnixMilli()+1, minTime) // refer to https://github.com/prometheus/prometheus/issues/13213 for the reason +1
190 assert.Equal(t, testData.expectedMaxTime.Truncate(time.Minute).UnixMilli(), maxTime)
191 })
192 }
193}
194
195func TestSlottedTicker(t *testing.T) {
196 t.Parallel()

Callers

nothing calls this directly

Calls 4

FindMinMaxTimeFunction · 0.85
RunMethod · 0.65
EqualMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected