Skip to content

Commit

Permalink
fixed func slaveInfoFieldByName range panic
Browse files Browse the repository at this point in the history
  • Loading branch information
polefishu committed Dec 17, 2019
1 parent 5269ebf commit 1bd62d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/client/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func slaveInfoFieldByName(name string, slaveInfoBlob interface{}) string {
slaveInfo := slaveInfoBlob.([]interface{})
infoLens := len(slaveInfo)
i := 0
for i < infoLens {
for i+1 < infoLens {
stringValue := slaveInfo[i].(string)
if stringValue == name {
return slaveInfo[i+1].(string)
Expand Down
18 changes: 18 additions & 0 deletions pkg/client/redis/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func TestGetAllRedisConfig(t *testing.T) {
func Test_slaveInfoFieldByName(t *testing.T) {
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"}
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"}
slaveInfoBlobC := []interface{}{"name", "[xxxxB]:6371", "ip", "slave-priority", "slave-priority", "100"}
slaveInfoBlobD := []interface{}{"name", "[xxxxB]:6371", "ip", "xxxxB", "slave-priority"}
type args struct {
name string
slaveInfoBlob interface{}
Expand All @@ -62,6 +64,22 @@ func Test_slaveInfoFieldByName(t *testing.T) {
},
want: "0",
},
{
name: "slaveC",
args: args{
name: "slave-priority",
slaveInfoBlob: slaveInfoBlobC,
},
want: "100",
},
{
name: "slaveD",
args: args{
name: "slave-priority",
slaveInfoBlob: slaveInfoBlobD,
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 1bd62d6

Please sign in to comment.