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

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cache
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/alicebob/miniredis/v2"
    12  	"github.com/redis/rueidis"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestRedisClient(t *testing.T) {
    17  	single, err := mockRedisClientSingle()
    18  	require.Nil(t, err)
    19  	defer single.Close()
    20  
    21  	cluster, err := mockRedisClientCluster()
    22  	require.Nil(t, err)
    23  	defer cluster.Close()
    24  
    25  	require.NoError(t, single.Ping(context.Background()))
    26  	require.NoError(t, cluster.Ping(context.Background()))
    27  
    28  	ctx := context.Background()
    29  
    30  	tests := []struct {
    31  		name   string
    32  		client *RedisClient
    33  	}{
    34  		{
    35  			name:   "single redis client",
    36  			client: single,
    37  		},
    38  		{
    39  			name:   "cluster redis client",
    40  			client: cluster,
    41  		},
    42  	}
    43  
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			keys := []string{"key1", "key2", "key3"}
    47  			bufs := [][]byte{[]byte("data1"), []byte("data2"), []byte("data3")}
    48  			miss := []string{"miss1", "miss2"}
    49  
    50  			// set values
    51  			err := tt.client.MSet(ctx, keys, bufs)
    52  			require.Nil(t, err)
    53  
    54  			// get keys
    55  			values, err := tt.client.MGet(ctx, keys)
    56  			require.Nil(t, err)
    57  			require.Len(t, values, len(keys))
    58  			for i, value := range values {
    59  				require.Equal(t, values[i], value)
    60  			}
    61  
    62  			// get missing keys
    63  			values, err = tt.client.MGet(ctx, miss)
    64  			require.NoError(t, err)
    65  			require.Len(t, values, len(miss))
    66  			for _, value := range values {
    67  				require.Nil(t, value)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func mockRedisClientSingle() (*RedisClient, error) {
    74  	redisServer, err := miniredis.Run()
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	cl, err := rueidis.NewClient(rueidis.ClientOption{
    80  		InitAddress:  []string{redisServer.Addr()},
    81  		DisableCache: true,
    82  	})
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	return &RedisClient{
    87  		expiration: time.Minute,
    88  		timeout:    100 * time.Millisecond,
    89  		rdb:        cl,
    90  	}, nil
    91  }
    92  
    93  func mockRedisClientCluster() (*RedisClient, error) {
    94  	redisServer1, err := miniredis.Run()
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	redisServer2, err := miniredis.Run()
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	cl, err := rueidis.NewClient(rueidis.ClientOption{
   103  		InitAddress:  []string{redisServer1.Addr(), redisServer2.Addr()},
   104  		DisableCache: true,
   105  	})
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	return &RedisClient{
   110  		expiration: time.Minute,
   111  		timeout:    100 * time.Millisecond,
   112  		rdb:        cl,
   113  	}, nil
   114  }