From ee5616f45b55077bd0a26dee21bb6522d18184f9 Mon Sep 17 00:00:00 2001 From: Bart Venter Date: Fri, 14 Jun 2024 12:40:56 +0000 Subject: [PATCH] test(all): Refactor tests to run conformance tests --- memcache/memcache_test.go | 241 ++++++--------------------- redis/redis_test.go | 255 +++++----------------------- rediscluster/rediscluster_test.go | 267 ++++++------------------------ 3 files changed, 147 insertions(+), 616 deletions(-) diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 31ced4b..ea49abe 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -8,7 +8,7 @@ import ( "time" cache "github.com/bartventer/gocache" - "github.com/bartventer/gocache/internal/testutil" + "github.com/bartventer/gocache/internal/cachetest" "github.com/bradfitz/gomemcache/memcache" "github.com/docker/docker/api/types/container" "github.com/stretchr/testify/assert" @@ -23,11 +23,30 @@ const ( defaultAddr = "localhost:" + defaultPort ) -// malformedKey is a key that is too long which will trigger the [memcache.ErrMalformedKey] error. -var malformedKey = strings.Repeat("a", 251) +func TestMemcacheCache_OpenCacheURL(t *testing.T) { + t.Parallel() + m := &memcacheCache{} + + u, err := url.Parse("memcache://" + defaultAddr) + require.NoError(t, err) + + _, err = m.OpenCacheURL(context.Background(), u, &cache.Options{}) + require.NoError(t, err) + assert.NotNil(t, m.client) +} + +func TestMemcacheCache_New(t *testing.T) { + t.Parallel() + ctx := context.Background() + config := cache.Config{} -// setupMemcached creates a new Memcached container. -func setupMemcached(t *testing.T) *memcacheCache { + m := New(ctx, &config, defaultAddr) + require.NotNil(t, m) + assert.NotNil(t, m.client) +} + +// setupCache creates a new Memcached container. +func setupCache(t *testing.T) *memcacheCache { t.Helper() // Create a new Memcached container ctx := context.Background() @@ -75,216 +94,60 @@ func setupMemcached(t *testing.T) *memcacheCache { return &memcacheCache{client: client, config: &cache.Config{CountLimit: 100}} } -func TestMemcacheCache_OpenCacheURL(t *testing.T) { - t.Parallel() - m := &memcacheCache{} - - u, err := url.Parse("memcache://" + defaultAddr) - require.NoError(t, err) - - _, err = m.OpenCacheURL(context.Background(), u, &cache.Options{}) - require.NoError(t, err) - assert.NotNil(t, m.client) -} - -func TestMemcacheCache_New(t *testing.T) { - t.Parallel() - ctx := context.Background() - config := cache.Config{} - - m := New(ctx, &config, defaultAddr) - require.NotNil(t, m) - assert.NotNil(t, m.client) -} - -func TestMemcacheCache_Exists(t *testing.T) { +func TestMemcacheCache_MalformedKey(t *testing.T) { t.Parallel() - c := setupMemcached(t) - key := testutil.UniqueKey(t) + c := setupCache(t) + // malformedKey is a key that is too long which will trigger the [memcache.ErrMalformedKey] error. + malformedKey := strings.Repeat("a", 251) value := "testValue" - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - t.Cleanup(func() { - c.client.Delete(key) - }) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.True(t, exists) - - // Non-existent key - exists, err = c.Exists(context.Background(), "nonExistentKey") - require.NoError(t, err) - assert.False(t, exists) - - // Malformed key - _, err = c.Exists(context.Background(), malformedKey) + // Test Exists function with malformed key + _, err := c.Exists(context.Background(), malformedKey) require.Error(t, err) assert.Contains(t, err.Error(), memcache.ErrMalformedKey.Error()) -} - -func TestMemcacheCache_Del(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - key := testutil.UniqueKey(t) - value := "testValue" - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - - err := c.Del(context.Background(), key) - require.NoError(t, err) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.False(t, exists) - - // Non-existent key - err = c.Del(context.Background(), "nonExistentKey") - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) - - // Malformed key + // Test Del function with malformed key err = c.Del(context.Background(), malformedKey) require.Error(t, err) assert.Contains(t, err.Error(), memcache.ErrMalformedKey.Error()) -} - -func TestMemcacheCache_Clear(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - - err := c.Clear(context.Background()) - require.NoError(t, err) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.False(t, exists) -} -func TestMemcacheCache_Get(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - t.Cleanup(func() { - c.client.Delete(key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, []byte(value), got) - - // Non-existent key - _, err = c.Get(context.Background(), "nonExistentKey") - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) - - // Malformed key + // Test Get function with malformed key _, err = c.Get(context.Background(), malformedKey) require.Error(t, err) assert.Contains(t, err.Error(), memcache.ErrMalformedKey.Error()) -} - -func TestMemcacheCache_Set(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - key := testutil.UniqueKey(t) - value := "testValue" - - err := c.Set(context.Background(), key, value) - require.NoError(t, err) - t.Cleanup(func() { - c.client.Delete(key) - }) - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, []byte(value), got) - - // Malformed key + // Test Set function with malformed key err = c.Set(context.Background(), malformedKey, value) require.Error(t, err) assert.Contains(t, err.Error(), memcache.ErrMalformedKey.Error()) } -func TestMemcacheCache_SetWithExpiry(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - key := testutil.UniqueKey(t) - value := "testValue" - expiry := 1 * time.Second - - err := c.SetWithExpiry(context.Background(), key, value, expiry) - require.NoError(t, err) - t.Cleanup(func() { - c.client.Delete(key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, []byte(value), got) - - // Wait for the key to expire - time.Sleep(expiry + 1*time.Second) - - _, err = c.Get(context.Background(), key) - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) - - // Malformed key - err = c.SetWithExpiry(context.Background(), malformedKey, value, expiry) - require.Error(t, err) - assert.Contains(t, err.Error(), memcache.ErrMalformedKey.Error()) +type harness struct { + cache *memcacheCache } -// Pattern matching operations not supported by Memcache - -func TestMemcacheCache_Count(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - _, err := c.Count(context.Background(), "*") - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrPatternMatchingNotSupported.Error()) +func (h *harness) MakeCache(ctx context.Context) (cache.Cache, error) { + return h.cache, nil } -func TestMemcacheCache_DelKeys(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - err := c.DelKeys(context.Background(), "*") - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrPatternMatchingNotSupported.Error()) +func (h *harness) Close() { + // Cleanup is handled in setup function } -func TestMemcacheCache_Ping(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - - err := c.Ping(context.Background()) - require.NoError(t, err) +func (h *harness) Options() cachetest.Options { + return cachetest.Options{ + PatternMatchingDisabled: true, // Memcached does not support pattern matching + CloseIsNoop: true, // Cache can still be used after closing + } } -func TestMemcacheCache_Close(t *testing.T) { - t.Parallel() - c := setupMemcached(t) - - err := c.Close() - require.NoError(t, err) +func newHarness(ctx context.Context, t *testing.T) (cachetest.Harness, error) { + cache := setupCache(t) + return &harness{ + cache: cache, + }, nil +} - // After closing, pinging should still succeed because Close is a no-op - err = c.Ping(context.Background()) - require.NoError(t, err) +func TestConformance(t *testing.T) { + cachetest.RunConformanceTests(t, newHarness) } diff --git a/redis/redis_test.go b/redis/redis_test.go index ae91632..e11ff1e 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -7,8 +7,7 @@ import ( "time" cache "github.com/bartventer/gocache" - "github.com/bartventer/gocache/internal/testutil" - "github.com/bartventer/gocache/keymod" + "github.com/bartventer/gocache/internal/cachetest" "github.com/docker/docker/api/types/container" "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" @@ -23,8 +22,33 @@ const ( defaultAddr = "localhost:" + defaultPort ) -// setupRedis creates a new Redis cache with a test container. -func setupRedis(t *testing.T) *redisCache { +func TestRedisCache_OpenCacheURL(t *testing.T) { + t.Parallel() + r := &redisCache{} + + u, err := url.Parse("redis://" + defaultAddr + "?maxretries=5&minretrybackoff=1000ms") + require.NoError(t, err) + + _, err = r.OpenCacheURL(context.Background(), u, &cache.Options{}) + require.NoError(t, err) + assert.NotNil(t, r.client) +} + +func TestRedisCache_New(t *testing.T) { + t.Parallel() + ctx := context.Background() + config := cache.Config{} + options := redis.Options{ + Addr: defaultAddr, + } + + r := New(ctx, &config, options) + require.NotNil(t, r) + assert.NotNil(t, r.client) +} + +// setupCache creates a new Redis cache with a test container. +func setupCache(t *testing.T) *redisCache { t.Helper() // Create a new Redis container ctx := context.Background() @@ -74,221 +98,32 @@ func setupRedis(t *testing.T) *redisCache { return &redisCache{client: client, config: &cache.Config{CountLimit: 100}} } -func TestRedisCache_OpenCacheURL(t *testing.T) { - t.Parallel() - r := &redisCache{} - - u, err := url.Parse("redis://" + defaultAddr + "?maxretries=5&minretrybackoff=1000ms") - require.NoError(t, err) - - _, err = r.OpenCacheURL(context.Background(), u, &cache.Options{}) - require.NoError(t, err) - assert.NotNil(t, r.client) +type harness struct { + cache *redisCache } -func TestRedisCache_New(t *testing.T) { - t.Parallel() - ctx := context.Background() - config := cache.Config{} - options := redis.Options{ - Addr: defaultAddr, - } - - r := New(ctx, &config, options) - require.NotNil(t, r) - assert.NotNil(t, r.client) +func (h *harness) MakeCache(ctx context.Context) (cache.Cache, error) { + return h.cache, nil } -func TestRedisCache_Count(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - key := testutil.UniqueKey(t) - value := "testValue1" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - count, err := c.Count(context.Background(), "*") - require.NoError(t, err) - assert.Equal(t, int64(1), count) +func (h *harness) Close() { + // Cleanup is handled in setup function } -func TestRedisCache_Exists(t *testing.T) { - t.Parallel() - c := setupRedis(t) - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) +func (h *harness) Options() cachetest.Options { + return cachetest.Options{ + PatternMatchingDisabled: false, + CloseIsNoop: false, } - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.True(t, exists) } -func TestRedisCache_Del(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - - err := c.Del(context.Background(), key) - require.NoError(t, err) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.False(t, exists) - - // Non-existent key - err = c.Del(context.Background(), "nonExistentKey") - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) -} - -func TestRedisCache_DelKeys(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - keys := []string{"testKey1", "testKey2", "testKey3"} - hashTag := testutil.UniqueKey(t) - for _, key := range keys { - if err := c.Set(context.Background(), key, "testValue", keymod.HashTagModifier(hashTag)); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - } - - err := c.DelKeys(context.Background(), "testKey*", keymod.HashTagModifier(hashTag)) - require.NoError(t, err) - - for _, key := range keys { - exists, existsErr := c.Exists(context.Background(), key, keymod.HashTagModifier(hashTag)) - require.NoError(t, existsErr) - assert.False(t, exists) - } - - // Non-existent key - err = c.DelKeys(context.Background(), "nonExistentKey*", keymod.HashTagModifier(hashTag)) - require.NoError(t, err) -} - -func TestRedisCache_Clear(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - - err := c.Clear(context.Background()) - require.NoError(t, err) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.False(t, exists) -} - -func TestRedisCache_Get(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.client.Set(context.Background(), key, value, 0).Err(); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, value, string(got)) - - // Non-existent key - _, err = c.Get(context.Background(), "nonExistentKey") - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) +func newHarness(ctx context.Context, t *testing.T) (cachetest.Harness, error) { + cache := setupCache(t) + return &harness{ + cache: cache, + }, nil } -func TestRedisCache_Set(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - err := c.Set(context.Background(), key, value) - require.NoError(t, err) - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, value, string(got)) -} - -func TestRedisCache_SetWithExpiry(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - key := testutil.UniqueKey(t) - value := "testValue" - expiry := 1 * time.Second - - err := c.SetWithExpiry(context.Background(), key, value, expiry) - require.NoError(t, err) - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, value, string(got)) - - // Wait for the key to expire - time.Sleep(expiry) - - _, err = c.Get(context.Background(), key) - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) -} - -func TestRedisCache_Ping(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - err := c.Ping(context.Background()) - require.NoError(t, err) -} - -func TestRedisCache_Close(t *testing.T) { - t.Parallel() - c := setupRedis(t) - - err := c.Close() - require.NoError(t, err) - - // After closing, pinging should result in an error - err = c.Ping(context.Background()) - require.Error(t, err) +func TestConformance(t *testing.T) { + cachetest.RunConformanceTests(t, newHarness) } diff --git a/rediscluster/rediscluster_test.go b/rediscluster/rediscluster_test.go index 990c901..c05ab0e 100644 --- a/rediscluster/rediscluster_test.go +++ b/rediscluster/rediscluster_test.go @@ -7,8 +7,7 @@ import ( "time" cache "github.com/bartventer/gocache" - "github.com/bartventer/gocache/internal/testutil" - "github.com/bartventer/gocache/keymod" + "github.com/bartventer/gocache/internal/cachetest" "github.com/docker/docker/api/types/container" "github.com/docker/go-connections/nat" "github.com/redis/go-redis/v9" @@ -27,8 +26,39 @@ var exposedPorts = []string{ "7005", } +func TestRedisCache_OpenCacheURL(t *testing.T) { + t.Parallel() + r := &redisClusterCache{} + + u, err := url.Parse("rediscluster://localhost:7000,localhost:7001,localhost:7002,localhost:7003,localhost:7004,localhost:7005?maxretries=5&minretrybackoff=1000ms") + require.NoError(t, err) + + _, err = r.OpenCacheURL(context.Background(), u, &cache.Options{}) + require.NoError(t, err) + assert.NotNil(t, r.client) +} + +func TestRedisCache_New(t *testing.T) { + t.Parallel() + ctx := context.Background() + config := cache.Config{} + options := redis.ClusterOptions{ + Addrs: []string{ + "localhost:7000", + "localhost:7001", + "localhost:7002", + "localhost:7003", + "localhost:7004", + "localhost:7005", + }, + } + r := New(ctx, &config, options) + require.NotNil(t, r) + assert.NotNil(t, r.client) +} + // setupRedisCluster creates a new Redis cluster container. -func setupRedisClusterCache(t *testing.T) *redisClusterCache { +func setupCache(t *testing.T) *redisClusterCache { t.Helper() // Create a new Redis cluster container ctx := context.Background() @@ -86,229 +116,32 @@ func setupRedisClusterCache(t *testing.T) *redisClusterCache { return &redisClusterCache{client: client, config: &cache.Config{CountLimit: 100}} } -func TestRedisCache_OpenCacheURL(t *testing.T) { - t.Parallel() - r := &redisClusterCache{} - - u, err := url.Parse("rediscluster://localhost:7000,localhost:7001,localhost:7002,localhost:7003,localhost:7004,localhost:7005?maxretries=5&minretrybackoff=1000ms") - require.NoError(t, err) - - _, err = r.OpenCacheURL(context.Background(), u, &cache.Options{}) - require.NoError(t, err) - assert.NotNil(t, r.client) +type harness struct { + cache *redisClusterCache } -func TestRedisCache_New(t *testing.T) { - t.Parallel() - ctx := context.Background() - config := cache.Config{} - options := redis.ClusterOptions{ - Addrs: []string{ - "localhost:7000", - "localhost:7001", - "localhost:7002", - "localhost:7003", - "localhost:7004", - "localhost:7005", - }, - } - r := New(ctx, &config, options) - require.NotNil(t, r) - assert.NotNil(t, r.client) +func (h *harness) MakeCache(ctx context.Context) (cache.Cache, error) { + return h.cache, nil } -func TestRedisClusterCache_Count(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - key := testutil.UniqueKey(t) - value := "testValue1" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - count, err := c.Count(context.Background(), "*") - require.NoError(t, err) - assert.Equal(t, int64(1), count) +func (h *harness) Close() { + // Cleanup is handled in setup function } -func TestRedisClusterCache_Exists(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) +func (h *harness) Options() cachetest.Options { + return cachetest.Options{ + PatternMatchingDisabled: false, + CloseIsNoop: false, } - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.True(t, exists) } -func TestRedisClusterCache_Del(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - - err := c.Del(context.Background(), key) - require.NoError(t, err) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.False(t, exists) - - // Non-existent key - err = c.Del(context.Background(), "nonExistentKey") - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) +func newHarness(ctx context.Context, t *testing.T) (cachetest.Harness, error) { + cache := setupCache(t) + return &harness{ + cache: cache, + }, nil } -func TestRedisClusterCache_DelKeys(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - keys := []string{"testKey1", "testKey2", "testKey3", "testKey4", "testKey5"} - hashTag := testutil.UniqueKey(t) - for _, key := range keys { - if err := c.Set(context.Background(), key, "testValue", keymod.HashTagModifier(hashTag)); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - } - - count, err := c.Count(context.Background(), "testKey*", keymod.HashTagModifier(hashTag)) - require.NoError(t, err) - if !assert.Equal(t, int64(5), count) { - t.FailNow() - } - - err = c.DelKeys(context.Background(), "testKey*", keymod.HashTagModifier(hashTag)) - require.NoError(t, err) - - res, err := c.Count(context.Background(), "testKey*", keymod.HashTagModifier(hashTag)) - require.NoError(t, err) - assert.Equal(t, int64(0), res) - - // Non-existent key - err = c.DelKeys(context.Background(), "nonExistentKey*", keymod.HashTagModifier(hashTag)) - require.NoError(t, err) -} - -func TestRedisClusterCache_Clear(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - - err := c.Clear(context.Background()) - require.NoError(t, err) - - exists, err := c.Exists(context.Background(), key) - require.NoError(t, err) - assert.False(t, exists) -} - -func TestRedisClusterCache_Get(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - if err := c.Set(context.Background(), key, value); err != nil { - t.Fatalf("Failed to set key: %v", err) - } - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, value, string(got)) - - // Non-existent key - _, err = c.Get(context.Background(), "nonExistentKey") - require.Error(t, err) -} - -func TestRedisClusterCache_Set(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - key := testutil.UniqueKey(t) - value := "testValue" - - err := c.Set(context.Background(), key, value) - require.NoError(t, err) - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, value, string(got)) -} - -func TestRedisClusterCache_SetWithExpiry(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - key := testutil.UniqueKey(t) + time.Now().String() - value := "testValue" - expiry := 1 * time.Second - - err := c.SetWithExpiry(context.Background(), key, value, expiry) - require.NoError(t, err) - t.Cleanup(func() { - c.client.Del(context.Background(), key) - }) - - got, err := c.Get(context.Background(), key) - require.NoError(t, err) - assert.Equal(t, value, string(got)) - - // Wait for the key to expire - time.Sleep(expiry) - - _, err = c.Get(context.Background(), key) - require.Error(t, err) - assert.Contains(t, err.Error(), cache.ErrKeyNotFound.Error()) -} - -func TestRedisClusterCache_Ping(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - err := c.Ping(context.Background()) - require.NoError(t, err) -} - -func TestRedisClusterCache_Close(t *testing.T) { - t.Parallel() - c := setupRedisClusterCache(t) - - err := c.Close() - require.NoError(t, err) - - // After closing, pinging should result in an error - err = c.Ping(context.Background()) - require.Error(t, err) +func TestConformance(t *testing.T) { + cachetest.RunConformanceTests(t, newHarness) }