From a25cd0614fa6e5db80ee77fe36a3c38a1f809446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=99=A8=E8=BE=89?= Date: Fri, 4 Aug 2023 15:31:00 +0800 Subject: [PATCH] update(): add redis unit test. fix cluster client, ring client may lead err. "skyWalking failed to create exit span, got error: parameter are nil" --- plugins/go-redisv9/hook.go | 2 +- plugins/go-redisv9/intercepter.go | 1 - plugins/go-redisv9/intercepter_test.go | 46 ++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/plugins/go-redisv9/hook.go b/plugins/go-redisv9/hook.go index 380c80bf..aa9a8ac9 100644 --- a/plugins/go-redisv9/hook.go +++ b/plugins/go-redisv9/hook.go @@ -50,7 +50,7 @@ func (r *redisHook) DialHook(next redis.DialHook) redis.DialHook { GoRedisCacheType+"/"+"dial", // peer - r.Addr, + addr, // injector func(k, v string) error { diff --git a/plugins/go-redisv9/intercepter.go b/plugins/go-redisv9/intercepter.go index bf36d646..52af836f 100644 --- a/plugins/go-redisv9/intercepter.go +++ b/plugins/go-redisv9/intercepter.go @@ -37,7 +37,6 @@ func (g *GoRedisInterceptor) AfterInvoke(invocation operator.Invocation, result if !ok { return fmt.Errorf("go-redis :skyWalking cannot create hook for client not match UniversalClient: %T", rdb) } - switch c := rdb.(type) { case *redis.Client: c.AddHook(newRedisHook(c.Options().Addr)) diff --git a/plugins/go-redisv9/intercepter_test.go b/plugins/go-redisv9/intercepter_test.go index a8805b3a..db4c4e6b 100644 --- a/plugins/go-redisv9/intercepter_test.go +++ b/plugins/go-redisv9/intercepter_test.go @@ -13,12 +13,12 @@ func init() { core.ResetTracingContext() } -func TestInvoke(t *testing.T) { +func TestRedisClientInvoke(t *testing.T) { defer core.ResetTracingContext() interceptor := &GoRedisInterceptor{} clusterClient := redis.NewClusterClient(&redis.ClusterOptions{ - Addrs: []string{"localhost:6379", "localhost:7379"}, + Addrs: []string{"localhost:6379"}, DialTimeout: time.Second * 10, PoolSize: 10, RouteByLatency: true, @@ -26,8 +26,42 @@ func TestInvoke(t *testing.T) { err := interceptor.AfterInvoke(nil, clusterClient) assert.Nil(t, err, "failed to invoke AfterInvoke") status, err := clusterClient.Ping(context.Background()).Result() - if status != "PONG" { - t.Fatalf("err: %v", err) - } - clusterClient.Get(context.Background(), "test").Val() + assert.Nil(t, err, "ping err") + assert.Equal(t, "PONG", status, "should be PONG") +} + +func TestRedisClusterClientInvoke(t *testing.T) { + defer core.ResetTracingContext() + + interceptor := &GoRedisInterceptor{} + clusterClient := redis.NewClusterClient(&redis.ClusterOptions{ + Addrs: []string{"localhost:6479", "localhost:6579"}, + DialTimeout: time.Second * 10, + PoolSize: 10, + RouteByLatency: true, + }) + err := interceptor.AfterInvoke(nil, clusterClient) + assert.Nil(t, err, "failed to invoke AfterInvoke") + status, err := clusterClient.Ping(context.Background()).Result() + assert.Nil(t, err, "ping err") + assert.Equal(t, "PONG", status, "should be PONG") +} + +func TestRedisRingClientInvoke(t *testing.T) { + defer core.ResetTracingContext() + + interceptor := &GoRedisInterceptor{} + clusterClient := redis.NewRing(&redis.RingOptions{ + Addrs: map[string]string{ + "shard1": "localhost:7000", + "shard2": "localhost:7001", + }, + DialTimeout: time.Second * 10, + PoolSize: 10, + }) + err := interceptor.AfterInvoke(nil, clusterClient) + assert.Nil(t, err, "failed to invoke AfterInvoke") + status, err := clusterClient.Ping(context.Background()).Result() + assert.Nil(t, err, "ping err") + assert.Equal(t, "PONG", status, "should be PONG") }