redis client支持设置mock模式

This commit is contained in:
白茶清欢 2025-04-21 16:42:28 +08:00
parent ac633385b7
commit 5d1ebcf505
4 changed files with 28 additions and 2 deletions

View File

@ -10,8 +10,8 @@ package abstract
import (
"context"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/redis/define"
"github.com/go-redis/redismock/v9"
"go.uber.org/zap"
)
@ -28,4 +28,6 @@ type IRedisClient interface {
SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) // 设置日志实例, 全局生效, 而非针对某一个实例
Exec(ctx context.Context, instanceFlag string, command consts.RedisCmd, args ...any) *define.RedisResult // 执行任意命令
SetCommandWhiteList(command []consts.RedisCmd) // 设置命令的白名单,全局生效, 而非单独针对某一个实例
SetMockMode() // 设置mock模式, 这是单元测试专用的, 生产环境不要使用!!!!!!
GetMockInstance() redismock.ClientMock // 数据mock实例
}

View File

@ -10,6 +10,7 @@ package redis
import (
"context"
"git.zhangdeman.cn/zhangdeman/consts"
"github.com/go-redis/redismock/v9"
"strings"
"sync"
"time"
@ -40,6 +41,9 @@ type OwnClient struct {
whiteCommandTable map[string]bool
logger *zap.Logger
extraLogFieldList []string
mockMode bool
mockInstance redismock.ClientMock
mockClient *redisClient.Client
}
func (o *OwnClient) isAllowCommand(command string) bool {
@ -221,12 +225,27 @@ func (o *OwnClient) isWriteCommand(command consts.RedisCmd) bool {
}).Has(consts.RedisCmd(strings.ToUpper(command.String()))) >= 0
}
// SetMockMode 启用mock, 非单元测试不要使用!!!!!!!!
func (o *OwnClient) SetMockMode() {
o.mockMode = true
o.mockClient, o.mockInstance = redismock.NewClientMock()
}
// GetMockInstance 获取mock实例
func (o *OwnClient) GetMockInstance() redismock.ClientMock {
return o.mockInstance
}
// newClient 获取客户端连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:12 2024/10/8
func (o *OwnClient) newClient(instanceConfig *define.Options) *redisClient.Client {
if o.mockMode {
// mock模式下, 直接返回mock实例
return o.mockClient
}
return redisClient.NewClient(&redisClient.Options{
DB: instanceConfig.DB,
Addr: instanceConfig.Addr,

3
go.mod
View File

@ -5,7 +5,7 @@ go 1.23.0
toolchain go1.24.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420100501-55e6ac5d835d
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420101447-0b570213d5c7
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250321102712-1cbfbe959740
github.com/pkg/errors v0.9.1
@ -23,6 +23,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-redis/redismock/v9 v9.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mozillazg/go-pinyin v0.20.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect

4
go.sum
View File

@ -12,6 +12,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125081557-cacc6b3cafc6 h1:f24T6C
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125081557-cacc6b3cafc6/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420100501-55e6ac5d835d h1:kWfJiJcJCaN3xEby1fepTg8Vy3umi7L54K/18on2bfw=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420100501-55e6ac5d835d/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420101447-0b570213d5c7 h1:dmZ/mwRQ/AGlKXYWyk4Ci+KRiMcJwZii7nUCj2F2bpM=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420101447-0b570213d5c7/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw=
@ -56,6 +58,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-redis/redismock/v9 v9.2.0 h1:ZrMYQeKPECZPjOj5u9eyOjg8Nnb0BS9lkVIZ6IpsKLw=
github.com/go-redis/redismock/v9 v9.2.0/go.mod h1:18KHfGDK4Y6c2R0H38EUGWAdc7ZQS9gfYxc94k7rWT0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=