github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/cache/cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/redis/go-redis/v9"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestRedisCache(t *testing.T) {
    14  	redisURL := "redis://localhost:6379/0"
    15  	opts, err := redis.ParseURL(redisURL)
    16  	require.NoError(t, err)
    17  
    18  	redisClient := NewRedis(redis.NewClient(opts))
    19  	inMemoryClient := NewInMemory()
    20  
    21  	tests := []struct {
    22  		name        string
    23  		client      Cache
    24  		RequireInte bool
    25  	}{
    26  		{
    27  			name:        "RedisClient",
    28  			client:      redisClient,
    29  			RequireInte: true,
    30  		},
    31  		{
    32  			name:        "InMemoryClient",
    33  			client:      inMemoryClient,
    34  			RequireInte: false,
    35  		},
    36  	}
    37  
    38  	for _, test := range tests {
    39  		t.Run(test.name, func(t *testing.T) {
    40  			c := test.client
    41  
    42  			t.Run("ImplementsCache", func(t *testing.T) {
    43  				require.Implements(t, (*Cache)(nil), c)
    44  			})
    45  
    46  			if testing.Short() && test.RequireInte {
    47  				t.Skip("a redis is required for this test: test skipped due to the use of --short flag")
    48  			}
    49  
    50  			t.Run("SET/GET/EXPIRE", func(t *testing.T) {
    51  				key := "foo"
    52  				val := []byte("bar")
    53  
    54  				// Set a key/value pair with a 10 ms expiration
    55  				c.Set(key, val, 10*time.Millisecond)
    56  
    57  				// Fetch the pair
    58  				actual, ok := c.Get(key)
    59  				assert.True(t, ok)
    60  				assert.Equal(t, val, actual)
    61  
    62  				// Wait the pair expiration
    63  				time.Sleep(11 * time.Millisecond)
    64  
    65  				// Check that the pair is no more available
    66  				_, ok = c.Get(key)
    67  				assert.False(t, ok)
    68  			})
    69  
    70  			t.Run("Keys", func(t *testing.T) {
    71  				c.Set("foo:one", []byte("1"), 10*time.Millisecond)
    72  				c.Set("foo:two", []byte("2"), 10*time.Millisecond)
    73  				c.Set("bar:baz", []byte("3"), 10*time.Millisecond)
    74  				keys := c.Keys("foo:")
    75  				sort.Strings(keys)
    76  				assert.Equal(t, []string{"foo:one", "foo:two"}, keys)
    77  			})
    78  
    79  			t.Run("MultiGet", func(t *testing.T) {
    80  				// Set two values
    81  				c.Set("one", []byte("1"), 10*time.Millisecond)
    82  				c.Set("two", []byte("2"), 10*time.Millisecond)
    83  
    84  				// Get the two
    85  				bufs := c.MultiGet([]string{"one", "two", "three"})
    86  
    87  				assert.Len(t, bufs, 3)
    88  				assert.Equal(t, []byte("1"), bufs[0])
    89  				assert.Equal(t, []byte("2"), bufs[1])
    90  				assert.Nil(t, bufs[2])
    91  			})
    92  
    93  			t.Run("Keys", func(t *testing.T) {
    94  				// Set three values
    95  				c.Set("foo:one", []byte("1"), 10*time.Millisecond)
    96  				c.Set("foo:two", []byte("2"), 10*time.Millisecond)
    97  				c.Set("bar:baz", []byte("3"), 10*time.Millisecond)
    98  
    99  				keys := c.Keys("foo:")
   100  				sort.Strings(keys)
   101  				assert.Equal(t, []string{"foo:one", "foo:two"}, keys)
   102  			})
   103  		})
   104  	}
   105  }