mirror of
https://github.com/gofiber/storage.git
synced 2025-11-03 02:43:22 +08:00
feat(redis): use testcontainers-go for testing redis
This commit is contained in:
@@ -1,36 +1,137 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/modules/redis"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
)
|
||||
|
||||
const (
|
||||
// redisImage is the default image used for running Redis in tests.
|
||||
redisImage = "docker.io/redis:7"
|
||||
redisImageEnvVar = "TEST_REDIS_IMAGE"
|
||||
redisPort = "6379/tcp"
|
||||
)
|
||||
|
||||
type testStoreSettings struct {
|
||||
withAddress bool
|
||||
withHostPort bool
|
||||
withURL bool
|
||||
}
|
||||
|
||||
type testStoreOption func(*testStoreSettings)
|
||||
|
||||
func withAddress() testStoreOption {
|
||||
return func(o *testStoreSettings) {
|
||||
o.withAddress = true
|
||||
}
|
||||
}
|
||||
|
||||
func withHostPort() testStoreOption {
|
||||
return func(o *testStoreSettings) {
|
||||
o.withHostPort = true
|
||||
}
|
||||
}
|
||||
|
||||
// withURL sets the test store to use a URL.
|
||||
// Use it when you want to explicitly combine multiple addresses in the same test
|
||||
// to verify which one is being used.
|
||||
// - true: the URL will receive the URI provided by the testcontainer
|
||||
// - false: the URL will be set to an empty string
|
||||
func withURL(b bool) testStoreOption {
|
||||
return func(o *testStoreSettings) {
|
||||
o.withURL = b
|
||||
}
|
||||
}
|
||||
|
||||
func newTestStore(t testing.TB, opts ...testStoreOption) *Storage {
|
||||
t.Helper()
|
||||
|
||||
settings := &testStoreSettings{
|
||||
withURL: true, // by default, the URL will be set to the URI provided by the testcontainer
|
||||
withAddress: false,
|
||||
withHostPort: false,
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(settings)
|
||||
}
|
||||
|
||||
img := redisImage
|
||||
if imgFromEnv := os.Getenv(redisImageEnvVar); imgFromEnv != "" {
|
||||
img = imgFromEnv
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := redis.Run(
|
||||
ctx, img,
|
||||
testcontainers.WithWaitStrategy(wait.ForListeningPort(redisPort).WithStartupTimeout(time.Second*10)),
|
||||
)
|
||||
testcontainers.CleanupContainer(t, c)
|
||||
require.NoError(t, err)
|
||||
|
||||
uri, err := c.ConnectionString(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg := Config{
|
||||
Reset: true,
|
||||
}
|
||||
|
||||
if settings.withHostPort {
|
||||
host, err := c.Host(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
port, err := c.MappedPort(ctx, redisPort)
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg.Host = host
|
||||
cfg.Port = port.Int()
|
||||
}
|
||||
|
||||
if settings.withAddress {
|
||||
// trim the scheme from the URI
|
||||
cfg.Addrs = []string{strings.TrimPrefix(uri, "redis://")}
|
||||
}
|
||||
|
||||
if settings.withURL {
|
||||
cfg.URL = uri
|
||||
}
|
||||
|
||||
return New(cfg)
|
||||
}
|
||||
|
||||
func Test_Redis_Set(t *testing.T) {
|
||||
var (
|
||||
testStore = New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_Redis_Set_Override(t *testing.T) {
|
||||
var (
|
||||
testStore = New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -44,13 +145,13 @@ func Test_Redis_Set_Override(t *testing.T) {
|
||||
|
||||
func Test_Redis_Get(t *testing.T) {
|
||||
var (
|
||||
testStore = New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -65,14 +166,14 @@ func Test_Redis_Get(t *testing.T) {
|
||||
|
||||
func Test_Redis_Expiration(t *testing.T) {
|
||||
var (
|
||||
testStore = New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -88,9 +189,9 @@ func Test_Redis_Expiration(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Redis_Get_NotExist(t *testing.T) {
|
||||
testStore := New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
result, err := testStore.Get("notexist")
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, len(result))
|
||||
@@ -98,13 +199,13 @@ func Test_Redis_Get_NotExist(t *testing.T) {
|
||||
|
||||
func Test_Redis_Delete(t *testing.T) {
|
||||
var (
|
||||
testStore = New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -121,11 +222,11 @@ func Test_Redis_Delete(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Redis_Reset(t *testing.T) {
|
||||
testStore := New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
val := []byte("doe")
|
||||
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set("john1", val, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -153,39 +254,35 @@ func Test_Redis_Reset(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Redis_Close(t *testing.T) {
|
||||
testStore := New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
require.Nil(t, testStore.Close())
|
||||
testStore := newTestStore(t)
|
||||
require.NoError(t, testStore.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Conn(t *testing.T) {
|
||||
testStore := New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
testStore := newTestStore(t)
|
||||
defer testStore.Close()
|
||||
|
||||
require.True(t, testStore.Conn() != nil)
|
||||
}
|
||||
|
||||
func Test_Redis_Initalize_WithURL(t *testing.T) {
|
||||
testStoreUrl := New(Config{
|
||||
URL: "redis://localhost:6379",
|
||||
})
|
||||
func Test_Redis_Initalize_WithHostPort(t *testing.T) {
|
||||
var (
|
||||
key = "clark"
|
||||
val = []byte("kent")
|
||||
)
|
||||
|
||||
err := testStoreUrl.Set(key, val, 0)
|
||||
testStore := newTestStore(t, withHostPort())
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := testStoreUrl.Get(key)
|
||||
result, err := testStore.Get(key)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, val, result)
|
||||
|
||||
err = testStoreUrl.Delete(key)
|
||||
err = testStore.Delete(key)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Nil(t, testStoreUrl.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Initalize_WithURL_TLS(t *testing.T) {
|
||||
@@ -211,6 +308,7 @@ func Test_Redis_Initalize_WithURL_TLS(t *testing.T) {
|
||||
URL: "redis://localhost:6380",
|
||||
TLSConfig: tlsCfg,
|
||||
})
|
||||
defer testStoreUrl.Close()
|
||||
|
||||
var (
|
||||
key = "clark"
|
||||
@@ -230,8 +328,6 @@ func Test_Redis_Initalize_WithURL_TLS(t *testing.T) {
|
||||
keys, err := testStoreUrl.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUrl.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Initalize_WithURL_TLS_Verify(t *testing.T) {
|
||||
@@ -257,6 +353,7 @@ func Test_Redis_Initalize_WithURL_TLS_Verify(t *testing.T) {
|
||||
URL: "redis://localhost:6380",
|
||||
TLSConfig: tlsCfg,
|
||||
})
|
||||
defer testStoreUrl.Close()
|
||||
|
||||
var (
|
||||
key = "clark"
|
||||
@@ -276,14 +373,13 @@ func Test_Redis_Initalize_WithURL_TLS_Verify(t *testing.T) {
|
||||
keys, err := testStoreUrl.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUrl.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Initalize_With_Secure_URL(t *testing.T) {
|
||||
testStoreUrl := New(Config{
|
||||
URL: "rediss://localhost:16380",
|
||||
})
|
||||
defer testStoreUrl.Close()
|
||||
|
||||
var (
|
||||
key = "clark"
|
||||
@@ -303,16 +399,12 @@ func Test_Redis_Initalize_With_Secure_URL(t *testing.T) {
|
||||
keys, err := testStoreUrl.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUrl.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Universal_Addrs(t *testing.T) {
|
||||
// This should failover and create a Single Node connection.
|
||||
testStoreUniversal := New(Config{
|
||||
Addrs: []string{"localhost:6379"},
|
||||
})
|
||||
|
||||
testStoreUniversal := newTestStore(t, withAddress())
|
||||
defer testStoreUniversal.Close()
|
||||
var (
|
||||
key = "bruce"
|
||||
val = []byte("wayne")
|
||||
@@ -331,18 +423,14 @@ func Test_Redis_Universal_Addrs(t *testing.T) {
|
||||
keys, err := testStoreUniversal.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUniversal.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Universal_With_URL_Undefined(t *testing.T) {
|
||||
// This should failover to creating a regular *redis.Client
|
||||
// The URL should get ignored since it's empty
|
||||
testStoreUniversal := New(Config{
|
||||
URL: "",
|
||||
Addrs: []string{"localhost:6379"},
|
||||
})
|
||||
|
||||
// the withURL option goes last to include it in the config
|
||||
testStoreUniversal := newTestStore(t, withAddress(), withURL(false))
|
||||
defer testStoreUniversal.Close()
|
||||
var (
|
||||
key = "bruce"
|
||||
val = []byte("wayne")
|
||||
@@ -361,17 +449,13 @@ func Test_Redis_Universal_With_URL_Undefined(t *testing.T) {
|
||||
keys, err := testStoreUniversal.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUniversal.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Universal_With_URL_Defined(t *testing.T) {
|
||||
// This should failover to creating a regular *redis.Client
|
||||
// The Addrs field should get ignored since URL is defined
|
||||
testStoreUniversal := New(Config{
|
||||
URL: "redis://localhost:6379",
|
||||
Addrs: []string{"localhost:6355"},
|
||||
})
|
||||
testStoreUniversal := newTestStore(t, withAddress(), withURL(true))
|
||||
defer testStoreUniversal.Close()
|
||||
|
||||
var (
|
||||
key = "bruce"
|
||||
@@ -391,19 +475,13 @@ func Test_Redis_Universal_With_URL_Defined(t *testing.T) {
|
||||
keys, err := testStoreUniversal.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUniversal.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Universal_With_HostPort(t *testing.T) {
|
||||
// This should failover to creating a regular *redis.Client
|
||||
// The Host and Port should get ignored since Addrs is defined
|
||||
testStoreUniversal := New(Config{
|
||||
Host: "localhost",
|
||||
Port: 6388,
|
||||
Addrs: []string{"localhost:6379"},
|
||||
})
|
||||
|
||||
testStoreUniversal := newTestStore(t, withAddress(), withHostPort(), withURL(false))
|
||||
defer testStoreUniversal.Close()
|
||||
var (
|
||||
key = "bruce"
|
||||
val = []byte("wayne")
|
||||
@@ -422,19 +500,13 @@ func Test_Redis_Universal_With_HostPort(t *testing.T) {
|
||||
keys, err := testStoreUniversal.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUniversal.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Universal_With_HostPort_And_URL(t *testing.T) {
|
||||
// This should failover to creating a regular *redis.Client
|
||||
// The Host and Port should get ignored since Addrs is defined
|
||||
testStoreUniversal := New(Config{
|
||||
URL: "redis://localhost:6379",
|
||||
Host: "localhost",
|
||||
Port: 6388,
|
||||
Addrs: []string{"localhost:6399"},
|
||||
})
|
||||
testStoreUniversal := newTestStore(t, withAddress(), withHostPort(), withURL(true))
|
||||
defer testStoreUniversal.Close()
|
||||
|
||||
var (
|
||||
key = "bruce"
|
||||
@@ -454,8 +526,6 @@ func Test_Redis_Universal_With_HostPort_And_URL(t *testing.T) {
|
||||
keys, err := testStoreUniversal.Keys()
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, keys)
|
||||
|
||||
require.Nil(t, testStoreUniversal.Close())
|
||||
}
|
||||
|
||||
func Test_Redis_Cluster(t *testing.T) {
|
||||
@@ -493,9 +563,9 @@ func Test_Redis_Cluster(t *testing.T) {
|
||||
}
|
||||
|
||||
func Benchmark_Redis_Set(b *testing.B) {
|
||||
testStore := New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
testStore := newTestStore(b)
|
||||
defer testStore.Close()
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
|
||||
@@ -508,9 +578,9 @@ func Benchmark_Redis_Set(b *testing.B) {
|
||||
}
|
||||
|
||||
func Benchmark_Redis_Get(b *testing.B) {
|
||||
testStore := New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
testStore := newTestStore(b)
|
||||
defer testStore.Close()
|
||||
|
||||
err := testStore.Set("john", []byte("doe"), 0)
|
||||
require.NoError(b, err)
|
||||
|
||||
@@ -525,9 +595,9 @@ func Benchmark_Redis_Get(b *testing.B) {
|
||||
}
|
||||
|
||||
func Benchmark_Redis_SetAndDelete(b *testing.B) {
|
||||
testStore := New(Config{
|
||||
Reset: true,
|
||||
})
|
||||
testStore := newTestStore(b)
|
||||
defer testStore.Close()
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user