github.com/thanos-io/thanos@v0.32.5/cmd/thanos/query_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package main
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/prometheus/prometheus/promql"
    11  
    12  	"github.com/efficientgo/core/testutil"
    13  )
    14  
    15  func TestLookbackDeltaFactory(t *testing.T) {
    16  	type testCase struct {
    17  		stepMillis int64
    18  		expect     time.Duration
    19  	}
    20  	var (
    21  		minute = time.Minute.Milliseconds()
    22  		hour   = time.Hour.Milliseconds()
    23  		tData  = []struct {
    24  			lookbackDelta        time.Duration
    25  			dynamicLookbackDelta bool
    26  			tcs                  []testCase
    27  		}{
    28  			{
    29  				// Non-dynamic lookbackDelta should always return the same engine.
    30  				lookbackDelta:        0,
    31  				dynamicLookbackDelta: false,
    32  				tcs: []testCase{
    33  					{0, time.Duration(0)},
    34  					{5 * minute, time.Duration(0)},
    35  					{1 * hour, time.Duration(0)},
    36  				},
    37  			},
    38  			{
    39  
    40  				lookbackDelta:        3 * time.Minute,
    41  				dynamicLookbackDelta: true,
    42  				tcs: []testCase{
    43  					{2 * minute, time.Duration(3) * time.Minute},
    44  					{3 * minute, time.Duration(3) * time.Minute},
    45  					{4 * minute, time.Duration(5) * time.Minute},
    46  					{5 * minute, time.Duration(5) * time.Minute},
    47  					{6 * minute, time.Duration(1) * time.Hour},
    48  					{1 * hour, time.Duration(1) * time.Hour},
    49  					{2 * hour, time.Duration(1) * time.Hour},
    50  				},
    51  			},
    52  			{
    53  				lookbackDelta:        5 * time.Minute,
    54  				dynamicLookbackDelta: true,
    55  				tcs: []testCase{
    56  					{0, time.Duration(5) * time.Minute},
    57  					{5 * minute, time.Duration(5) * time.Minute},
    58  					{6 * minute, time.Duration(1) * time.Hour},
    59  					{59 * minute, time.Duration(1) * time.Hour},
    60  					{1 * hour, time.Duration(1) * time.Hour},
    61  					{2 * hour, time.Duration(1) * time.Hour},
    62  				},
    63  			},
    64  			{
    65  				lookbackDelta:        30 * time.Minute,
    66  				dynamicLookbackDelta: true,
    67  				tcs: []testCase{
    68  					{0, time.Duration(30) * time.Minute},
    69  					{5 * minute, time.Duration(30) * time.Minute},
    70  					{30 * minute, time.Duration(30) * time.Minute},
    71  					{31 * minute, time.Duration(1) * time.Hour},
    72  					{59 * minute, time.Duration(1) * time.Hour},
    73  					{1 * hour, time.Duration(1) * time.Hour},
    74  					{2 * hour, time.Duration(1) * time.Hour},
    75  				},
    76  			},
    77  			{
    78  				lookbackDelta:        1 * time.Hour,
    79  				dynamicLookbackDelta: true,
    80  				tcs: []testCase{
    81  					{0, time.Duration(1) * time.Hour},
    82  					{5 * minute, time.Duration(1) * time.Hour},
    83  					{1 * hour, time.Duration(1) * time.Hour},
    84  					{2 * hour, time.Duration(1) * time.Hour},
    85  				},
    86  			},
    87  		}
    88  	)
    89  	for _, td := range tData {
    90  		lookbackCreate := LookbackDeltaFactory(promql.EngineOpts{LookbackDelta: td.lookbackDelta}, td.dynamicLookbackDelta)
    91  		for _, tc := range td.tcs {
    92  			got := lookbackCreate(tc.stepMillis)
    93  			testutil.Equals(t, tc.expect, got)
    94  		}
    95  	}
    96  }