github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/chunk/schema_caching_test.go (about)

     1  package chunk
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/prometheus/common/model"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/weaveworks/common/mtime"
    12  )
    13  
    14  func TestCachingSchema(t *testing.T) {
    15  	const userID = "userid"
    16  
    17  	dailyBuckets := makeSeriesStoreSchema("v9")
    18  	schema := &schemaCaching{
    19  		SeriesStoreSchema: dailyBuckets,
    20  		cacheOlderThan:    24 * time.Hour,
    21  	}
    22  
    23  	baseTime := time.Unix(0, 0)
    24  	baseTime = baseTime.Add(30*24*time.Hour - 1)
    25  
    26  	mtime.NowForce(baseTime)
    27  
    28  	for i, tc := range []struct {
    29  		from, through time.Time
    30  
    31  		cacheableIdx int
    32  	}{
    33  		{
    34  			// Completely cacheable.
    35  			baseTime.Add(-36 * time.Hour),
    36  			baseTime.Add(-25 * time.Hour),
    37  			0,
    38  		},
    39  		{
    40  			// Completely active.
    41  			baseTime.Add(-23 * time.Hour),
    42  			baseTime.Add(-2 * time.Hour),
    43  			-1,
    44  		},
    45  		{
    46  			// Mix of both but the cacheable entry is also active.
    47  			baseTime.Add(-36 * time.Hour),
    48  			baseTime.Add(-2 * time.Hour),
    49  			-1,
    50  		},
    51  		{
    52  			// Mix of both.
    53  			baseTime.Add(-50 * time.Hour),
    54  			baseTime.Add(-2 * time.Hour),
    55  			-1,
    56  		},
    57  	} {
    58  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    59  			have, err := schema.GetReadQueriesForMetric(
    60  				model.TimeFromUnix(tc.from.Unix()), model.TimeFromUnix(tc.through.Unix()),
    61  				userID, "foo",
    62  			)
    63  			require.NoError(t, err)
    64  
    65  			for i := range have {
    66  				assert.Equal(t, have[i].Immutable, i <= tc.cacheableIdx, i)
    67  			}
    68  		})
    69  	}
    70  }