github.com/thanos-io/thanos@v0.32.5/pkg/cache/redis_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cache
     5  
     6  import (
     7  	"context"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/alicebob/miniredis/v2"
    13  	"github.com/efficientgo/core/testutil"
    14  	"github.com/go-kit/log"
    15  	"github.com/prometheus/client_golang/prometheus"
    16  	prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
    17  	"github.com/thanos-io/thanos/pkg/cacheutil"
    18  )
    19  
    20  func TestRedisCache(t *testing.T) {
    21  	// Init some data to conveniently define test cases later one.
    22  	key1 := "key1"
    23  	key2 := "key2"
    24  	key3 := "key3"
    25  	value1 := []byte{1}
    26  	value2 := []byte{2}
    27  	value3 := []byte{3}
    28  
    29  	type args struct {
    30  		data      map[string][]byte
    31  		fetchKeys []string
    32  	}
    33  	type want struct {
    34  		hits map[string][]byte
    35  	}
    36  	tests := []struct {
    37  		name string
    38  		args args
    39  		want want
    40  	}{
    41  		{
    42  			name: "all hit",
    43  			args: args{
    44  				data: map[string][]byte{
    45  					key1: value1,
    46  					key2: value2,
    47  					key3: value3,
    48  				},
    49  				fetchKeys: []string{key1, key2, key3},
    50  			},
    51  			want: want{
    52  				hits: map[string][]byte{
    53  					key1: value1,
    54  					key2: value2,
    55  					key3: value3,
    56  				},
    57  			},
    58  		},
    59  		{
    60  			name: "partial hit",
    61  			args: args{
    62  				data: map[string][]byte{
    63  					key1: value1,
    64  					key2: value2,
    65  				},
    66  				fetchKeys: []string{key1, key2, key3},
    67  			},
    68  			want: want{
    69  				hits: map[string][]byte{
    70  					key1: value1,
    71  					key2: value2,
    72  				},
    73  			},
    74  		},
    75  		{
    76  			name: "not hit",
    77  			args: args{
    78  				data:      map[string][]byte{},
    79  				fetchKeys: []string{key1, key2, key3},
    80  			},
    81  			want: want{
    82  				hits: map[string][]byte{},
    83  			},
    84  		},
    85  	}
    86  	s, err := miniredis.Run()
    87  	if err != nil {
    88  		testutil.Ok(t, err)
    89  	}
    90  	defer s.Close()
    91  	logger := log.NewLogfmtLogger(os.Stderr)
    92  	reg := prometheus.NewRegistry()
    93  	cfg := cacheutil.DefaultRedisClientConfig
    94  	cfg.Addr = s.Addr()
    95  	c, err := cacheutil.NewRedisClientWithConfig(logger, t.Name(), cfg, reg)
    96  	if err != nil {
    97  		testutil.Ok(t, err)
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			defer s.FlushAll()
   102  			c := NewRedisCache(tt.name, logger, c, reg)
   103  			// Store the cache expected before running the test.
   104  			ctx := context.Background()
   105  			c.Store(tt.args.data, time.Hour)
   106  
   107  			// Fetch postings from cached and assert on it.
   108  			hits := c.Fetch(ctx, tt.args.fetchKeys)
   109  			testutil.Equals(t, tt.want.hits, hits)
   110  
   111  			// Assert on metrics.
   112  			testutil.Equals(t, float64(len(tt.args.fetchKeys)), prom_testutil.ToFloat64(c.requests))
   113  			testutil.Equals(t, float64(len(tt.want.hits)), prom_testutil.ToFloat64(c.hits))
   114  		})
   115  	}
   116  }