Skip to content

Commit

Permalink
Fix RedisCluster.GetMultiKey (TykTechnologies#2539)
Browse files Browse the repository at this point in the history
This method was not returning any error for the case of missing keys.

calling with `[]string{"1", "2"}` keys that dont exists resulted into
`[]string{"", ""} , nil` .

This changes adds additional check to ensure if no value retuned we get
`nil, ErrKeyNotFound`

Fixes TykTechnologies#2490
  • Loading branch information
gernest authored and buger committed Sep 24, 2019
1 parent f2f74d9 commit bf85793
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions storage/redis_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,12 @@ func (r *RedisCluster) GetMultiKey(keyNames []string) ([]string, error) {
log.WithError(err).Debug("Error trying to get value")
return nil, ErrKeyNotFound
}

return value, nil
for _, v := range value {
if v != "" {
return value, nil
}
}
return nil, ErrKeyNotFound
}

func (r *RedisCluster) GetKeyTTL(keyName string) (ttl int64, err error) {
Expand Down
27 changes: 27 additions & 0 deletions storage/redis_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package storage

import "testing"

func TestRedisClusterGetMultiKey(t *testing.T) {
keys := []string{"first", "second"}
r := RedisCluster{KeyPrefix: "test-cluster"}
for _, v := range keys {
r.DeleteKey(v)
}
_, err := r.GetMultiKey(keys)
if err != ErrKeyNotFound {
t.Errorf("expected %v got %v", ErrKeyNotFound, err)
}
err = r.SetKey(keys[0], keys[0], 0)
if err != nil {
t.Fatal(err)
}

v, err := r.GetMultiKey(keys)
if err != nil {
t.Fatal(err)
}
if v[0] != keys[0] {
t.Errorf("expected %s got %s", keys[0], v[0])
}
}

0 comments on commit bf85793

Please sign in to comment.