diff --git a/redis/cache/redisx/redisxtest/doc.go b/redis/cache/redisx/redisxtest/doc.go new file mode 100644 index 000000000..f2a7db45e --- /dev/null +++ b/redis/cache/redisx/redisxtest/doc.go @@ -0,0 +1,4 @@ +// Package redisxtest provides utilities for baseplate.go/redids/cache/redisx testing. +// +// This includes functions for mocking redis instances. +package redisxtest diff --git a/redis/cache/redisx/redisxtest/redisxtest.go b/redis/cache/redisx/redisxtest/redisxtest.go new file mode 100644 index 000000000..2356be61a --- /dev/null +++ b/redis/cache/redisx/redisxtest/redisxtest.go @@ -0,0 +1,39 @@ +package redisxtest + +import ( + "context" + "testing" + + "github.com/joomcode/redispipe/redis" + "github.com/joomcode/redispipe/redisconn" + + "github.com/reddit/baseplate.go/redis/cache/redisx" +) + +// NewMockRedisClient sets up a redis client for use in testing +func NewMockRedisClient( + ctx context.Context, + tb testing.TB, + address string, + opts redisconn.Opts, +) (*redisx.BaseSync, error) { + tb.Helper() + + // Create connection to redis + conn, err := redisconn.Connect(ctx, address, opts) + if err != nil { + return nil, err + } + + // Create client + client := redisx.BaseSync{ + SyncCtx: redis.SyncCtx{S: conn}, + } + + // Cleanup + tb.Cleanup(func() { + conn.Close() + }) + + return &client, nil +}