github.com/thanos-io/thanos@v0.32.5/internal/cortex/chunk/cache/cache_test.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cache_test
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"math/rand"
    10  	"strconv"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/go-kit/log"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/thanos-io/thanos/internal/cortex/chunk/cache"
    18  )
    19  
    20  func fillCache(t *testing.T, cache cache.Cache) ([]string, [][]byte) {
    21  	keys := []string{}
    22  	bufs := [][]byte{}
    23  	for i := 0; i < 111; i++ {
    24  		keys = append(keys, fmt.Sprintf("test%d", i))
    25  		bufs = append(bufs, []byte(fmt.Sprintf("buf%d", i)))
    26  	}
    27  
    28  	cache.Store(context.Background(), keys, bufs)
    29  	return keys, bufs
    30  }
    31  
    32  func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, data [][]byte) {
    33  	for i := 0; i < 100; i++ {
    34  		index := rand.Intn(len(keys))
    35  		key := keys[index]
    36  
    37  		found, bufs, missingKeys := cache.Fetch(context.Background(), []string{key})
    38  		require.Len(t, found, 1)
    39  		require.Len(t, bufs, 1)
    40  		require.Len(t, missingKeys, 0)
    41  		require.Equal(t, data[index], bufs[0])
    42  	}
    43  }
    44  
    45  func testCacheMultiple(t *testing.T, cache cache.Cache, keys []string, data [][]byte) {
    46  	// test getting them all
    47  	found, bufs, missingKeys := cache.Fetch(context.Background(), keys)
    48  	require.Len(t, found, len(keys))
    49  	require.Len(t, bufs, len(keys))
    50  	require.Len(t, missingKeys, 0)
    51  
    52  	result := [][]byte{}
    53  	for i := range found {
    54  		result = append(result, bufs[i])
    55  	}
    56  	require.Equal(t, data, result)
    57  }
    58  
    59  func testCacheMiss(t *testing.T, cache cache.Cache) {
    60  	for i := 0; i < 100; i++ {
    61  		key := strconv.Itoa(rand.Int()) // arbitrary key which should fail: no chunk key is a single integer
    62  		found, bufs, missing := cache.Fetch(context.Background(), []string{key})
    63  		require.Empty(t, found)
    64  		require.Empty(t, bufs)
    65  		require.Len(t, missing, 1)
    66  	}
    67  }
    68  
    69  func testCache(t *testing.T, cache cache.Cache) {
    70  	keys, bufs := fillCache(t, cache)
    71  	t.Run("Single", func(t *testing.T) {
    72  		testCacheSingle(t, cache, keys, bufs)
    73  	})
    74  	t.Run("Multiple", func(t *testing.T) {
    75  		testCacheMultiple(t, cache, keys, bufs)
    76  	})
    77  	t.Run("Miss", func(t *testing.T) {
    78  		testCacheMiss(t, cache)
    79  	})
    80  }
    81  
    82  func TestMemcache(t *testing.T) {
    83  	t.Run("Unbatched", func(t *testing.T) {
    84  		cache := cache.NewMemcached(cache.MemcachedConfig{}, newMockMemcache(),
    85  			"test", nil, log.NewNopLogger())
    86  		testCache(t, cache)
    87  	})
    88  
    89  	t.Run("Batched", func(t *testing.T) {
    90  		cache := cache.NewMemcached(cache.MemcachedConfig{
    91  			BatchSize:   10,
    92  			Parallelism: 3,
    93  		}, newMockMemcache(), "test", nil, log.NewNopLogger())
    94  		testCache(t, cache)
    95  	})
    96  }
    97  
    98  func TestFifoCache(t *testing.T) {
    99  	cache := cache.NewFifoCache("test", cache.FifoCacheConfig{MaxSizeItems: 1e3, Validity: 1 * time.Hour},
   100  		nil, log.NewNopLogger())
   101  	testCache(t, cache)
   102  }
   103  
   104  func TestSnappyCache(t *testing.T) {
   105  	cache := cache.NewSnappy(cache.NewMockCache(), log.NewNopLogger())
   106  	testCache(t, cache)
   107  }