Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5269ebf

Browse files
fixd func slaveInfoFieldByName when slaveInfo have same key and value
1 parent 6ab4a8d commit 5269ebf

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

‎pkg/client/redis/client.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ func (c *client) GetNumberSentinelSlavesInMemory(ip string, auth *util.AuthConfi
115115

116116
func slaveInfoFieldByName(name string, slaveInfoBlob interface{}) string {
117117
slaveInfo := slaveInfoBlob.([]interface{})
118-
for key, value := range slaveInfo {
119-
stringValue := value.(string)
118+
infoLens := len(slaveInfo)
119+
i := 0
120+
for i < infoLens {
121+
stringValue := slaveInfo[i].(string)
120122
if stringValue == name {
121-
return slaveInfo[key+1].(string)
123+
return slaveInfo[i+1].(string)
122124
}
125+
i += 2
123126
}
124127
return ""
125128
}

‎pkg/client/redis/client_test.go‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package redis_test
1+
package redis
22

33
import (
4-
rediscli "github.com/go-redis/redis"
5-
"github.com/ucloud/redis-operator/pkg/client/redis"
64
"strings"
75
"testing"
6+
7+
rediscli "github.com/go-redis/redis"
88
)
99

1010
func newClient() *rediscli.Client {
@@ -18,7 +18,7 @@ func newClient() *rediscli.Client {
1818
func TestGetAllRedisConfig(t *testing.T) {
1919
cli := newClient()
2020
//var client redis.Client
21-
client := redis.New()
21+
client := New()
2222
result, err := client.GetAllRedisConfig(cli)
2323
if err != nil {
2424
t.Fatal(err)
@@ -33,3 +33,41 @@ func TestGetAllRedisConfig(t *testing.T) {
3333
}
3434

3535
}
36+
37+
func Test_slaveInfoFieldByName(t *testing.T) {
38+
slaveInfoBlobA := []interface{}{"name", "[xxxxA]:6379", "ip", "xxxxA", "port", "6379", "runid", "6f792839ab551e8dbec58e0eb3b3838d14f19a37", "flags", "slave", "link-pending-commands", "1", "link-refcount", "1", "last-ping-sent", "0", "last-ok-ping-reply", "1055", "last-ping-reply", "1055", "down-after-milliseconds", "5000", "info-refresh", "2074", "role-reported", "slave", "role-reported-time", "2983115", "master-link-down-time", "0", "master-link-status", "ok", "master-host", "xxxxA", "master-port", "6379", "slave-priority", "1", "slave-repl-offset", "124614695"}
39+
slaveInfoBlobB := []interface{}{"name", "[xxxxB]:6371", "ip", "xxxxB", "port", "6371", "runid", "fake_slave_8bb90711-8f37-44e8-b3b2-589af", "flags", "slave", "link-pending-commands", "1", "link-refcount", "1", "last-ping-sent", "0", "last-ok-ping-reply", "1055", "last-ping-reply", "1055", "down-after-milliseconds", "5000", "info-refresh", "2075", "role-reported", "slave", "role-reported-time", "2983114", "master-link-down-time", "0", "master-link-status", "ok", "master-host", "xxxxB", "master-port", "6379", "slave-priority", "0", "slave-repl-offset", "124614695"}
40+
type args struct {
41+
name string
42+
slaveInfoBlob interface{}
43+
}
44+
tests := []struct {
45+
name string
46+
args args
47+
want string
48+
}{
49+
{
50+
name: "slaveA",
51+
args: args{
52+
name: "slave-priority",
53+
slaveInfoBlob: slaveInfoBlobA,
54+
},
55+
want: "1",
56+
},
57+
{
58+
name: "slaveB",
59+
args: args{
60+
name: "slave-priority",
61+
slaveInfoBlob: slaveInfoBlobB,
62+
},
63+
want: "0",
64+
},
65+
}
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
if got := slaveInfoFieldByName(tt.args.name, tt.args.slaveInfoBlob); got != tt.want {
69+
t.Errorf("slaveInfoFieldByName() = %v, want %v", got, tt.want)
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /