Skip to content

Commit

Permalink
update(): add redis unit test. fix cluster client, ring client may le…
Browse files Browse the repository at this point in the history
…ad err. "skyWalking failed to create exit span, got error: parameter are nil"
  • Loading branch information
JamesSunXX committed Aug 4, 2023
1 parent 1f1e207 commit a25cd06
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/go-redisv9/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion plugins/go-redisv9/intercepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
46 changes: 40 additions & 6 deletions plugins/go-redisv9/intercepter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,55 @@ 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,
})
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")
}

0 comments on commit a25cd06

Please sign in to comment.