github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/series/index/schema_caching_test.go (about)

     1  package index
     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 := mustMakeSchema("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  	defer mtime.NowReset()
    28  
    29  	for i, tc := range []struct {
    30  		from, through time.Time
    31  
    32  		cacheableIdx int
    33  	}{
    34  		{
    35  			// Completely cacheable.
    36  			baseTime.Add(-36 * time.Hour),
    37  			baseTime.Add(-25 * time.Hour),
    38  			0,
    39  		},
    40  		{
    41  			// Completely active.
    42  			baseTime.Add(-23 * time.Hour),
    43  			baseTime.Add(-2 * time.Hour),
    44  			-1,
    45  		},
    46  		{
    47  			// Mix of both but the cacheable entry is also active.
    48  			baseTime.Add(-36 * time.Hour),
    49  			baseTime.Add(-2 * time.Hour),
    50  			-1,
    51  		},
    52  		{
    53  			// Mix of both.
    54  			baseTime.Add(-50 * time.Hour),
    55  			baseTime.Add(-2 * time.Hour),
    56  			-1,
    57  		},
    58  	} {
    59  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    60  			have, err := schema.GetReadQueriesForMetric(
    61  				model.TimeFromUnix(tc.from.Unix()), model.TimeFromUnix(tc.through.Unix()),
    62  				userID, "foo",
    63  			)
    64  			require.NoError(t, err)
    65  
    66  			for i := range have {
    67  				assert.Equal(t, have[i].Immutable, i <= tc.cacheableIdx, i)
    68  			}
    69  		})
    70  	}
    71  }