Files
platform/web/testutil/redis.go

31 lines
647 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package testutil
import (
g "platform/web/globals"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
)
// SetupRedisTest 创建一个测试用的Redis实例
// 返回miniredis实例使用t.Cleanup自动清理资源
func SetupRedisTest(t *testing.T) *miniredis.Miniredis {
mr, err := miniredis.Run()
if err != nil {
t.Fatalf("设置 miniredis 失败: %v", err)
}
// 替换 Redis 客户端为测试客户端
g.Redis = redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
// 使用t.Cleanup确保测试结束后恢复原始客户端并关闭miniredis
t.Cleanup(func() {
mr.Close()
})
return mr
}