From 84bf18c5760523ec91a12e6e2a338d290118349d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sat, 27 Feb 2021 23:27:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 5 +++ middleware/redis/{clent.go => client.go} | 47 +++++++++++++++++++++--- middleware/redis/redis_test.go | 8 +++- 4 files changed, 53 insertions(+), 8 deletions(-) rename middleware/redis/{clent.go => client.go} (81%) diff --git a/go.mod b/go.mod index 2ee8575..60510b2 100644 --- a/go.mod +++ b/go.mod @@ -9,5 +9,6 @@ require ( github.com/lestrrat-go/strftime v1.0.4 // indirect github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 + github.com/stretchr/testify v1.7.0 go.uber.org/zap v1.16.0 ) diff --git a/go.sum b/go.sum index 69bf7df..943bf14 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= @@ -68,13 +69,16 @@ github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= @@ -156,5 +160,6 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/middleware/redis/clent.go b/middleware/redis/client.go similarity index 81% rename from middleware/redis/clent.go rename to middleware/redis/client.go index a006d4d..efba2c8 100644 --- a/middleware/redis/clent.go +++ b/middleware/redis/client.go @@ -8,6 +8,7 @@ package redis import ( + "strings" "time" "github.com/go-developer/gopkg/convert" @@ -17,6 +18,30 @@ import ( "go.uber.org/zap" ) +// defaultParseError ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 10:59 下午 2021/2/27 +func defaultParseError(err error) error { + if nil == err { + return nil + } + errMsg := err.Error() + if errMsg == "nil" || errMsg == "" { + return nil + } + strArr := strings.Split(errMsg, ":") + if len(strArr) != 2 { + return err + } + msg := strings.ToLower(strings.TrimSpace(strArr[1])) + if msg == "nil" || msg == "" { + return nil + } + return err +} + // Options 连接选项,百分之百兼容第三方包的选项 // // Author : go_developer@163.com<张德满> @@ -41,10 +66,14 @@ type RealClient struct { // Author : go_developer@163.com<张德满> // // Date : 5:05 下午 2021/2/27 -func NewClient(config map[string]Options) (ClientInterface, error) { +func NewClient(config map[string]Options, parseErrorFunc func(err error) error) (ClientInterface, error) { c := &Client{ - instanceTable: make(map[string]*RealClient), - confTable: config, + instanceTable: make(map[string]*RealClient), + confTable: config, + parseErrorFunc: parseErrorFunc, + } + if nil == c.parseErrorFunc { + c.parseErrorFunc = defaultParseError } return c, c.init() } @@ -55,8 +84,9 @@ func NewClient(config map[string]Options) (ClientInterface, error) { // // Date : 4:52 下午 2021/2/27 type Client struct { - instanceTable map[string]*RealClient // redis 实例 - confTable map[string]Options // redis 配置 + instanceTable map[string]*RealClient // redis 实例 + confTable map[string]Options // redis 配置 + parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis: } // init 初始化redis连接 @@ -175,7 +205,7 @@ func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...in startTime := time.Now().Unix() cmdResult := realClient.Instance.Do(ctx.Ctx, redisCmd...) go c.log(ctx, realClient, cmdResult, startTime, time.Now().UnixNano()) - return cmdResult.Val(), cmdResult.Err() + return cmdResult.Val(), c.parseErrorFunc(cmdResult.Err()) } // CommandProxyWithReceiver 执行命令,并解析结果 @@ -199,6 +229,11 @@ func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver in return ResultConvertFail(convert.ConvertAssign(receiver, result)) } +// ClientInterface 定义redis client的接口实现,方便单元测试数据mock +// +// Author : go_developer@163.com<张德满> +// +// Date : 10:49 下午 2021/2/27 type ClientInterface interface { GetRedisClient(flag string) (*RealClient, error) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error) diff --git a/middleware/redis/redis_test.go b/middleware/redis/redis_test.go index b82daff..3d5f784 100644 --- a/middleware/redis/redis_test.go +++ b/middleware/redis/redis_test.go @@ -11,6 +11,8 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/assert" + redisInstance "github.com/go-redis/redis/v8" ) @@ -35,9 +37,11 @@ func TestCommandProxy(t *testing.T) { }, LoggerFieldConfig: nil, }, - }) + }, nil) if nil != err { panic(err.Error()) } - fmt.Println(instance.CommandProxy(nil, "test_redis", "set", "command_proxy", "hello world")) + r, cmdErr := instance.CommandProxy(nil, "test_redis", "set", "command_proxy", "hello world") + assert.Nil(t, cmdErr, "命令执行成功") + assert.Equal(t, "OK", fmt.Sprintf("%v", r)) }