Skip to content

Instantly share code, notes, and snippets.

@ashleyconnor
Last active May 28, 2023 18:16
Show Gist options
  • Save ashleyconnor/732ce2b2ccce5f1b3dd9a0dcc9e77c79 to your computer and use it in GitHub Desktop.
Save ashleyconnor/732ce2b2ccce5f1b3dd9a0dcc9e77c79 to your computer and use it in GitHub Desktop.
Testing a redis client against real redis
package main
import (
"context"
"testing"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestWithRedis(t *testing.T) {
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "redis:latest",
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
}
redisC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
t.Error(err)
}
defer func() {
if err := redisC.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err.Error())
}
}()
endpoint, err := redisC.Endpoint(ctx, "")
if err != nil {
t.Error(err)
}
client := redis.NewClient(&redis.Options{
Addr: endpoint,
})
client.Set(ctx, "FOO", "BAR", 0)
result := client.Get(ctx, "FOO")
assert.Equal(t, "BAR", result.Val())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment