@@ -244,107 +244,128 @@ func TestResolveAndValidateTimestamps_Success(t *testing.T) {
244244 var chartSeconds uint64 = 1000 // -> min timestamp = lastExportedTs - chartSeconds
245245 duration := time .Second // -> max interval = 200s
246246 tests := []struct {
247- name string
248- latestExportedTs uint64
249- givenAfterTs * uint64
250- givenBeforeTs * uint64
251- wantAfterTs uint64
252- wantBeforeTs uint64
247+ name string
248+ minPossibleTs uint64
249+ maxPossibleTs uint64
250+ givenAfterTs * uint64
251+ givenBeforeTs * uint64
252+ wantAfterTs uint64
253+ wantBeforeTs uint64
253254 }{
254255 // no timestams are provided, should resolve to beforeTs = latestExportedTs and afterTs = latestExportedTs - maxAllowedInterval
255256 {
256- name : "no timestamps" ,
257- latestExportedTs : 1000000000 ,
258- givenAfterTs : nil ,
259- givenBeforeTs : nil ,
260- wantAfterTs : 999999800 ,
261- wantBeforeTs : 1000000000 ,
257+ name : "no timestamps" ,
258+ maxPossibleTs : 1000000000 ,
259+ givenAfterTs : nil ,
260+ givenBeforeTs : nil ,
261+ wantAfterTs : 999999800 ,
262+ wantBeforeTs : 1000000000 ,
262263 },
263264 // no timestamps are provided and latestExportedTs is low, should resolve to beforeTs = latestExportedTs and afterTs = 0
264265 {
265- name : "no timestamps - low latest ts" ,
266- latestExportedTs : 100 ,
267- givenAfterTs : nil ,
268- givenBeforeTs : nil ,
269- wantAfterTs : 0 ,
270- wantBeforeTs : 100 ,
266+ name : "no timestamps - low latest ts" ,
267+ maxPossibleTs : 100 ,
268+ givenAfterTs : nil ,
269+ givenBeforeTs : nil ,
270+ wantAfterTs : 0 ,
271+ wantBeforeTs : 100 ,
271272 },
272273 // afterTs is provided, beforeTs should be afterTs + maxAllowedInterval
273274 {
274- name : "high after ts" ,
275- latestExportedTs : 1000000000 ,
276- givenAfterTs : ptr (uint64 (1000000000 )),
277- givenBeforeTs : nil ,
278- wantAfterTs : 1000000000 ,
279- wantBeforeTs : 1000000200 ,
275+ name : "high after ts" ,
276+ maxPossibleTs : 1000000000 ,
277+ givenAfterTs : ptr (uint64 (1000000000 )),
278+ givenBeforeTs : nil ,
279+ wantAfterTs : 1000000000 ,
280+ wantBeforeTs : 1000000200 ,
280281 },
281282 // afterTs is provided and lowest possible
282283 {
283- name : "low after ts" ,
284- latestExportedTs : 1000000000 ,
285- givenAfterTs : ptr (uint64 (999999000 )),
286- givenBeforeTs : nil ,
287- wantAfterTs : 999999000 ,
288- wantBeforeTs : 999999200 ,
284+ name : "low after ts" ,
285+ maxPossibleTs : 1000000000 ,
286+ givenAfterTs : ptr (uint64 (999999000 )),
287+ givenBeforeTs : nil ,
288+ wantAfterTs : 999999000 ,
289+ wantBeforeTs : 999999200 ,
290+ },
291+
292+ // after ts is provided but below minPossibleTs, e.g. chain is younger than max interval
293+ {
294+ name : "after ts below min possible" ,
295+ minPossibleTs : 999999100 ,
296+ maxPossibleTs : 1000000000 ,
297+ givenAfterTs : ptr (uint64 (999998999 )),
298+ givenBeforeTs : nil ,
299+ wantAfterTs : 999999100 ,
300+ wantBeforeTs : 999999300 ,
289301 },
290302 // beforeTs is provided, afterTs should be beforeTs - maxAllowedInterval
291303 {
292- name : "high before ts" ,
293- latestExportedTs : 1000000000 ,
294- givenAfterTs : nil ,
295- givenBeforeTs : ptr (uint64 (999999800 )),
296- wantAfterTs : 999999600 ,
297- wantBeforeTs : 999999800 ,
304+ name : "high before ts" ,
305+ maxPossibleTs : 1000000000 ,
306+ givenAfterTs : nil ,
307+ givenBeforeTs : ptr (uint64 (999999800 )),
308+ wantAfterTs : 999999600 ,
309+ wantBeforeTs : 999999800 ,
310+ },
311+ // beforeTs is higher than latest exported ts, should resolve to latest exported ts
312+ {
313+ name : "high before ts - above latest" ,
314+ maxPossibleTs : 1000000000 ,
315+ givenAfterTs : nil ,
316+ givenBeforeTs : ptr (uint64 (1000000001 )),
317+ wantAfterTs : 999999800 ,
318+ wantBeforeTs : 1000000000 ,
298319 },
299320 // beforeTs is exactly minAllowedTs + maxAllowedInterval, afterTs should be minAllowedTs
300321 {
301- name : "low before ts - exact" ,
302- latestExportedTs : 1000000000 ,
303- givenAfterTs : nil ,
304- givenBeforeTs : ptr (uint64 (999999200 )),
305- wantAfterTs : 999999000 ,
306- wantBeforeTs : 999999200 ,
322+ name : "low before ts - exact" ,
323+ maxPossibleTs : 1000000000 ,
324+ givenAfterTs : nil ,
325+ givenBeforeTs : ptr (uint64 (999999200 )),
326+ wantAfterTs : 999999000 ,
327+ wantBeforeTs : 999999200 ,
307328 },
308329 // beforeTs is provided and close to minAllowedTs, afterTs should be minAllowedTs
309330 {
310- name : "low before ts" ,
311- latestExportedTs : 1000000000 ,
312- givenAfterTs : nil ,
313- givenBeforeTs : ptr (uint64 (999999050 )),
314- wantAfterTs : 999999000 ,
315- wantBeforeTs : 999999050 ,
331+ name : "low before ts" ,
332+ maxPossibleTs : 1000000000 ,
333+ givenAfterTs : nil ,
334+ givenBeforeTs : ptr (uint64 (999999050 )),
335+ wantAfterTs : 999999000 ,
336+ wantBeforeTs : 999999050 ,
316337 },
317338 // both timestamps are provided
318339 {
319- name : "both timestamps" ,
320- latestExportedTs : 1000000000 ,
321- givenAfterTs : ptr (uint64 (999999950 )),
322- givenBeforeTs : ptr (uint64 (1000000000 )),
323- wantAfterTs : 999999950 ,
324- wantBeforeTs : 1000000000 ,
340+ name : "both timestamps" ,
341+ maxPossibleTs : 1000000000 ,
342+ givenAfterTs : ptr (uint64 (999999950 )),
343+ givenBeforeTs : ptr (uint64 (1000000000 )),
344+ wantAfterTs : 999999950 ,
345+ wantBeforeTs : 1000000000 ,
325346 },
326347 // both timestamps are provided, high edge case
327348 {
328- name : "both timestamps - high edge" ,
329- latestExportedTs : 1000000000 ,
330- givenAfterTs : ptr (uint64 (1000000000 )),
331- givenBeforeTs : ptr (uint64 (1000000200 )),
332- wantAfterTs : 1000000000 ,
333- wantBeforeTs : 1000000200 ,
349+ name : "both timestamps - high edge" ,
350+ maxPossibleTs : 1000000000 ,
351+ givenAfterTs : ptr (uint64 (1000000000 )),
352+ givenBeforeTs : ptr (uint64 (1000000200 )),
353+ wantAfterTs : 1000000000 ,
354+ wantBeforeTs : 1000000200 ,
334355 },
335356 // both timestamps are provided, low edge case
336357 {
337- name : "both timestamps - low edge" ,
338- latestExportedTs : 1000000000 ,
339- givenAfterTs : ptr (uint64 (999999000 )),
340- givenBeforeTs : ptr (uint64 (999999200 )),
341- wantAfterTs : 999999000 ,
342- wantBeforeTs : 999999200 ,
358+ name : "both timestamps - low edge" ,
359+ maxPossibleTs : 1000000000 ,
360+ givenAfterTs : ptr (uint64 (999999000 )),
361+ givenBeforeTs : ptr (uint64 (999999200 )),
362+ wantAfterTs : 999999000 ,
363+ wantBeforeTs : 999999200 ,
343364 },
344365 }
345366 for _ , tt := range tests {
346367 t .Run (tt .name , func (t * testing.T ) {
347- gotAfterTs , gotBeforeTs , err := resolveAndValidateTimestamps (tt .givenAfterTs , tt .givenBeforeTs , chartSeconds , duration , tt .latestExportedTs )
368+ gotAfterTs , gotBeforeTs , err := resolveAndValidateTimestamps (tt .givenAfterTs , tt .givenBeforeTs , chartSeconds , duration , tt .minPossibleTs , tt . maxPossibleTs )
348369 assert .NoError (t , err , "Expected no error, got %v" , err )
349370 assert .Equal (t , tt .wantAfterTs , gotAfterTs , "Expected afterTs to be %d, got %d" , tt .wantAfterTs , gotAfterTs )
350371 assert .Equal (t , tt .wantBeforeTs , gotBeforeTs , "Expected beforeTs to be %d, got %d" , tt .wantBeforeTs , gotBeforeTs )
@@ -354,39 +375,37 @@ func TestResolveAndValidateTimestamps_Success(t *testing.T) {
354375
355376func TestResolveAndValidateTimestamps_Failure (t * testing.T ) {
356377 var chartSeconds uint64 = 1000
378+ maxPossibleTs := uint64 (1000000000 )
357379 duration := time .Second // -> max interval = 200s
358380 tests := []struct {
359- name string
360- latestExportedTs uint64
361- givenAfterTs * uint64
362- givenBeforeTs * uint64
363- errMsg string
381+ name string
382+ maxPossibleTs uint64
383+ givenAfterTs * uint64
384+ givenBeforeTs * uint64
385+ errMsg string
364386 }{
365387 {
366- name : "after ts below min allowed" ,
367- latestExportedTs : 1000000000 ,
368- givenAfterTs : ptr (uint64 (999998999 )),
369- givenBeforeTs : nil ,
370- errMsg : "`after_ts` must be greater or equal to 999999000" ,
388+ name : "after ts below min allowed" ,
389+ givenAfterTs : ptr (uint64 (999998999 )),
390+ givenBeforeTs : nil ,
391+ errMsg : "`after_ts` must be greater or equal to 999999000" ,
371392 },
372393 {
373- name : "before ts below min allowed" ,
374- latestExportedTs : 1000000000 ,
375- givenAfterTs : nil ,
376- givenBeforeTs : ptr (uint64 (999998999 )),
377- errMsg : "`before_ts` must be greater or equal to 999999000" ,
394+ name : "before ts below min allowed" ,
395+ givenAfterTs : nil ,
396+ givenBeforeTs : ptr (uint64 (999998999 )),
397+ errMsg : "`before_ts` must be greater or equal to 999999000" ,
378398 },
379399 {
380- name : "both timestamps - too high interval" ,
381- latestExportedTs : 1000000000 ,
382- givenAfterTs : ptr (uint64 (999999000 )),
383- givenBeforeTs : ptr (uint64 (999999201 )),
384- errMsg : "difference between `before_ts` and `after_ts` must be smaller or equal to 200" ,
400+ name : "both timestamps - too high interval" ,
401+ givenAfterTs : ptr (uint64 (999999000 )),
402+ givenBeforeTs : ptr (uint64 (999999201 )),
403+ errMsg : "difference between `before_ts` and `after_ts` must be smaller or equal to 200" ,
385404 },
386405 }
387406 for _ , tt := range tests {
388407 t .Run (tt .name , func (t * testing.T ) {
389- _ , _ , err := resolveAndValidateTimestamps (tt .givenAfterTs , tt .givenBeforeTs , chartSeconds , duration , tt . latestExportedTs )
408+ _ , _ , err := resolveAndValidateTimestamps (tt .givenAfterTs , tt .givenBeforeTs , chartSeconds , duration , 0 , maxPossibleTs )
390409 assert .Error (t , err , "Expected error, got %v" , err )
391410 assert .Contains (t , err .Error (), tt .errMsg )
392411 })
0 commit comments