From ee630b8b575fe535bc028f4bb99f9bc92837050f Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 9 Apr 2021 22:40:43 +0800 Subject: [PATCH] add code coverage (#615) * add code coverage * simplify redis code --- core/stores/redis/redis.go | 12 +- core/stores/redis/redis_test.go | 198 +++++++++++++++++++++-- core/stores/redis/redisclientmanager.go | 14 +- core/stores/redis/redisclustermanager.go | 14 +- 4 files changed, 201 insertions(+), 37 deletions(-) diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go index f2f29a19..8e68e659 100644 --- a/core/stores/redis/redis.go +++ b/core/stores/redis/redis.go @@ -1742,17 +1742,9 @@ func acceptable(err error) bool { func getRedis(r *Redis) (RedisNode, error) { switch r.Type { case ClusterType: - if r.tls { - return getClusterWithTLS(r.Addr, r.Pass, r.tls) - } else { - return getCluster(r.Addr, r.Pass) - } + return getCluster(r) case NodeType: - if r.tls { - return getClientWithTLS(r.Addr, r.Pass, r.tls) - } else { - return getClient(r.Addr, r.Pass) - } + return getClient(r) default: return nil, fmt.Errorf("redis type '%s' is not supported", r.Type) } diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index e88065df..10e4cc28 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -15,6 +15,8 @@ import ( func TestRedis_Exists(t *testing.T) { runOnRedis(t, func(client *Redis) { + _, err := New(client.Addr, badType()).Exists("a") + assert.NotNil(t, err) ok, err := client.Exists("a") assert.Nil(t, err) assert.False(t, ok) @@ -27,7 +29,7 @@ func TestRedis_Exists(t *testing.T) { func TestRedisTLS_Exists(t *testing.T) { runOnRedisTLS(t, func(client *Redis) { - _, err := New(client.Addr, WithTLS()).Exists("a") + _, err := New(client.Addr, badType()).Exists("a") assert.NotNil(t, err) ok, err := client.Exists("a") assert.NotNil(t, err) @@ -41,7 +43,9 @@ func TestRedisTLS_Exists(t *testing.T) { func TestRedis_Eval(t *testing.T) { runOnRedis(t, func(client *Redis) { - _, err := client.Eval(`redis.call("EXISTS", KEYS[1])`, []string{"notexist"}) + _, err := New(client.Addr, badType()).Eval(`redis.call("EXISTS", KEYS[1])`, []string{"notexist"}) + assert.NotNil(t, err) + _, err = client.Eval(`redis.call("EXISTS", KEYS[1])`, []string{"notexist"}) assert.Equal(t, Nil, err) err = client.Set("key1", "value1") assert.Nil(t, err) @@ -64,6 +68,8 @@ func TestRedis_Hgetall(t *testing.T) { runOnRedis(t, func(client *Redis) { assert.Nil(t, client.Hset("a", "aa", "aaa")) assert.Nil(t, client.Hset("a", "bb", "bbb")) + _, err := New(client.Addr, badType()).Hgetall("a") + assert.NotNil(t, err) vals, err := client.Hgetall("a") assert.Nil(t, err) assert.EqualValues(t, map[string]string{ @@ -75,8 +81,11 @@ func TestRedis_Hgetall(t *testing.T) { func TestRedis_Hvals(t *testing.T) { runOnRedis(t, func(client *Redis) { + assert.NotNil(t, New(client.Addr, badType()).Hset("a", "aa", "aaa")) assert.Nil(t, client.Hset("a", "aa", "aaa")) assert.Nil(t, client.Hset("a", "bb", "bbb")) + _, err := New(client.Addr, badType()).Hvals("a") + assert.NotNil(t, err) vals, err := client.Hvals("a") assert.Nil(t, err) assert.ElementsMatch(t, []string{"aaa", "bbb"}, vals) @@ -87,6 +96,8 @@ func TestRedis_Hsetnx(t *testing.T) { runOnRedis(t, func(client *Redis) { assert.Nil(t, client.Hset("a", "aa", "aaa")) assert.Nil(t, client.Hset("a", "bb", "bbb")) + _, err := New(client.Addr, badType()).Hsetnx("a", "bb", "ccc") + assert.NotNil(t, err) ok, err := client.Hsetnx("a", "bb", "ccc") assert.Nil(t, err) assert.False(t, ok) @@ -103,6 +114,8 @@ func TestRedis_HdelHlen(t *testing.T) { runOnRedis(t, func(client *Redis) { assert.Nil(t, client.Hset("a", "aa", "aaa")) assert.Nil(t, client.Hset("a", "bb", "bbb")) + _, err := New(client.Addr, badType()).Hlen("a") + assert.NotNil(t, err) num, err := client.Hlen("a") assert.Nil(t, err) assert.Equal(t, 2, num) @@ -117,6 +130,8 @@ func TestRedis_HdelHlen(t *testing.T) { func TestRedis_HIncrBy(t *testing.T) { runOnRedis(t, func(client *Redis) { + _, err := New(client.Addr, badType()).Hincrby("key", "field", 2) + assert.NotNil(t, err) val, err := client.Hincrby("key", "field", 2) assert.Nil(t, err) assert.Equal(t, 2, val) @@ -130,6 +145,8 @@ func TestRedis_Hkeys(t *testing.T) { runOnRedis(t, func(client *Redis) { assert.Nil(t, client.Hset("a", "aa", "aaa")) assert.Nil(t, client.Hset("a", "bb", "bbb")) + _, err := New(client.Addr, badType()).Hkeys("a") + assert.NotNil(t, err) vals, err := client.Hkeys("a") assert.Nil(t, err) assert.ElementsMatch(t, []string{"aa", "bb"}, vals) @@ -140,6 +157,8 @@ func TestRedis_Hmget(t *testing.T) { runOnRedis(t, func(client *Redis) { assert.Nil(t, client.Hset("a", "aa", "aaa")) assert.Nil(t, client.Hset("a", "bb", "bbb")) + _, err := New(client.Addr, badType()).Hmget("a", "aa", "bb") + assert.NotNil(t, err) vals, err := client.Hmget("a", "aa", "bb") assert.Nil(t, err) assert.EqualValues(t, []string{"aaa", "bbb"}, vals) @@ -151,7 +170,7 @@ func TestRedis_Hmget(t *testing.T) { func TestRedis_Hmset(t *testing.T) { runOnRedis(t, func(client *Redis) { - assert.NotNil(t, NewRedis(client.Addr, "").Hmset("a", nil)) + assert.NotNil(t, New(client.Addr, badType()).Hmset("a", nil)) assert.Nil(t, client.Hmset("a", map[string]string{ "aa": "aaa", "bb": "bbb", @@ -175,6 +194,8 @@ func TestRedis_Hscan(t *testing.T) { var cursor uint64 = 0 sum := 0 for { + _, _, err := New(client.Addr, badType()).Hscan(key, cursor, "*", 100) + assert.NotNil(t, err) reMap, next, err := client.Hscan(key, cursor, "*", 100) assert.Nil(t, err) sum += len(reMap) @@ -185,6 +206,8 @@ func TestRedis_Hscan(t *testing.T) { } assert.Equal(t, sum, 3100) + _, err = New(client.Addr, badType()).Del(key) + assert.NotNil(t, err) _, err = client.Del(key) assert.Nil(t, err) }) @@ -192,6 +215,8 @@ func TestRedis_Hscan(t *testing.T) { func TestRedis_Incr(t *testing.T) { runOnRedis(t, func(client *Redis) { + _, err := New(client.Addr, badType()).Incr("a") + assert.NotNil(t, err) val, err := client.Incr("a") assert.Nil(t, err) assert.Equal(t, int64(1), val) @@ -203,6 +228,8 @@ func TestRedis_Incr(t *testing.T) { func TestRedis_IncrBy(t *testing.T) { runOnRedis(t, func(client *Redis) { + _, err := New(client.Addr, badType()).Incrby("a", 2) + assert.NotNil(t, err) val, err := client.Incrby("a", 2) assert.Nil(t, err) assert.Equal(t, int64(2), val) @@ -218,6 +245,8 @@ func TestRedis_Keys(t *testing.T) { assert.Nil(t, err) err = client.Set("key2", "value2") assert.Nil(t, err) + _, err = New(client.Addr, badType()).Keys("*") + assert.NotNil(t, err) keys, err := client.Keys("*") assert.Nil(t, err) assert.ElementsMatch(t, []string{"key1", "key2"}, keys) @@ -227,7 +256,7 @@ func TestRedis_Keys(t *testing.T) { func TestRedis_HyperLogLog(t *testing.T) { runOnRedis(t, func(client *Redis) { client.Ping() - r := NewRedis(client.Addr, "") + r := New(client.Addr, badType()) _, err := r.Pfadd("key1") assert.NotNil(t, err) _, err = r.Pfcount("*") @@ -239,33 +268,47 @@ func TestRedis_HyperLogLog(t *testing.T) { func TestRedis_List(t *testing.T) { runOnRedis(t, func(client *Redis) { + _, err := New(client.Addr, badType()).Lpush("key", "value1", "value2") + assert.NotNil(t, err) val, err := client.Lpush("key", "value1", "value2") assert.Nil(t, err) assert.Equal(t, 2, val) + _, err = New(client.Addr, badType()).Rpush("key", "value3", "value4") + assert.NotNil(t, err) val, err = client.Rpush("key", "value3", "value4") assert.Nil(t, err) assert.Equal(t, 4, val) + _, err = New(client.Addr, badType()).Llen("key") + assert.NotNil(t, err) val, err = client.Llen("key") assert.Nil(t, err) assert.Equal(t, 4, val) vals, err := client.Lrange("key", 0, 10) assert.Nil(t, err) assert.EqualValues(t, []string{"value2", "value1", "value3", "value4"}, vals) + _, err = New(client.Addr, badType()).Lpop("key") + assert.NotNil(t, err) v, err := client.Lpop("key") assert.Nil(t, err) assert.Equal(t, "value2", v) val, err = client.Lpush("key", "value1", "value2") assert.Nil(t, err) assert.Equal(t, 5, val) + _, err = New(client.Addr, badType()).Rpop("key") + assert.NotNil(t, err) v, err = client.Rpop("key") assert.Nil(t, err) assert.Equal(t, "value4", v) val, err = client.Rpush("key", "value4", "value3", "value3") assert.Nil(t, err) assert.Equal(t, 7, val) + _, err = New(client.Addr, badType()).Lrem("key", 2, "value1") + assert.NotNil(t, err) n, err := client.Lrem("key", 2, "value1") assert.Nil(t, err) assert.Equal(t, 2, n) + _, err = New(client.Addr, badType()).Lrange("key", 0, 10) + assert.NotNil(t, err) vals, err = client.Lrange("key", 0, 10) assert.Nil(t, err) assert.EqualValues(t, []string{"value2", "value3", "value4", "value3", "value3"}, vals) @@ -284,6 +327,8 @@ func TestRedis_Mget(t *testing.T) { assert.Nil(t, err) err = client.Set("key2", "value2") assert.Nil(t, err) + _, err = New(client.Addr, badType()).Mget("key1", "key0", "key2", "key3") + assert.NotNil(t, err) vals, err := client.Mget("key1", "key0", "key2", "key3") assert.Nil(t, err) assert.EqualValues(t, []string{"value1", "", "value2", ""}, vals) @@ -292,7 +337,10 @@ func TestRedis_Mget(t *testing.T) { func TestRedis_SetBit(t *testing.T) { runOnRedis(t, func(client *Redis) { - assert.Nil(t, client.SetBit("key", 1, 1)) + err := New(client.Addr, badType()).SetBit("key", 1, 1) + assert.NotNil(t, err) + err = client.SetBit("key", 1, 1) + assert.Nil(t, err) }) } @@ -300,6 +348,8 @@ func TestRedis_GetBit(t *testing.T) { runOnRedis(t, func(client *Redis) { err := client.SetBit("key", 2, 1) assert.Nil(t, err) + _, err = New(client.Addr, badType()).GetBit("key", 2) + assert.NotNil(t, err) val, err := client.GetBit("key", 2) assert.Nil(t, err) assert.Equal(t, 1, val) @@ -313,6 +363,8 @@ func TestRedis_BitCount(t *testing.T) { assert.Nil(t, err) } + _, err := New(client.Addr, badType()).BitCount("key", 0, -1) + assert.NotNil(t, err) val, err := client.BitCount("key", 0, -1) assert.Nil(t, err) assert.Equal(t, int64(11), val) @@ -342,11 +394,14 @@ func TestRedis_BitOpAnd(t *testing.T) { assert.Nil(t, err) err = client.Set("key2", "1") assert.Nil(t, err) + _, err = New(client.Addr, badType()).BitOpAnd("destKey", "key1", "key2") + assert.NotNil(t, err) val, err := client.BitOpAnd("destKey", "key1", "key2") assert.Nil(t, err) assert.Equal(t, int64(1), val) valStr, err := client.Get("destKey") assert.Nil(t, err) + //destKey binary 110000 ascii 0 assert.Equal(t, "0", valStr) }) } @@ -355,6 +410,8 @@ func TestRedis_BitOpNot(t *testing.T) { runOnRedis(t, func(client *Redis) { err := client.Set("key1", "\u0000") assert.Nil(t, err) + _, err = New(client.Addr, badType()).BitOpNot("destKey", "key1") + assert.NotNil(t, err) val, err := client.BitOpNot("destKey", "key1") assert.Nil(t, err) assert.Equal(t, int64(1), val) @@ -370,6 +427,8 @@ func TestRedis_BitOpOr(t *testing.T) { assert.Nil(t, err) err = client.Set("key2", "0") assert.Nil(t, err) + _, err = New(client.Addr, badType()).BitOpOr("destKey", "key1", "key2") + assert.NotNil(t, err) val, err := client.BitOpOr("destKey", "key1", "key2") assert.Nil(t, err) assert.Equal(t, int64(1), val) @@ -385,6 +444,8 @@ func TestRedis_BitOpXor(t *testing.T) { assert.Nil(t, err) err = client.Set("key2", "\x0f") assert.Nil(t, err) + _, err = New(client.Addr, badType()).BitOpXor("destKey", "key1", "key2") + assert.NotNil(t, err) val, err := client.BitOpXor("destKey", "key1", "key2") assert.Nil(t, err) assert.Equal(t, int64(1), val) @@ -395,10 +456,12 @@ func TestRedis_BitOpXor(t *testing.T) { } func TestRedis_BitPos(t *testing.T) { runOnRedis(t, func(client *Redis) { - // 11111111 11110000 00000000 + //11111111 11110000 00000000 err := client.Set("key", "\xff\xf0\x00") assert.Nil(t, err) + _, err = New(client.Addr, badType()).BitPos("key", 0, 0, -1) + assert.NotNil(t, err) val, err := client.BitPos("key", 0, 0, 2) assert.Nil(t, err) assert.Equal(t, int64(12), val) @@ -424,6 +487,8 @@ func TestRedis_BitPos(t *testing.T) { func TestRedis_Persist(t *testing.T) { runOnRedis(t, func(client *Redis) { + _, err := New(client.Addr, badType()).Persist("key") + assert.NotNil(t, err) ok, err := client.Persist("key") assert.Nil(t, err) assert.False(t, ok) @@ -432,11 +497,15 @@ func TestRedis_Persist(t *testing.T) { ok, err = client.Persist("key") assert.Nil(t, err) assert.False(t, ok) + err = New(client.Addr, badType()).Expire("key", 5) + assert.NotNil(t, err) err = client.Expire("key", 5) assert.Nil(t, err) ok, err = client.Persist("key") assert.Nil(t, err) assert.True(t, ok) + err = New(client.Addr, badType()).Expireat("key", time.Now().Unix()+5) + assert.NotNil(t, err) err = client.Expireat("key", time.Now().Unix()+5) assert.Nil(t, err) ok, err = client.Persist("key") @@ -458,6 +527,8 @@ func TestRedis_Scan(t *testing.T) { assert.Nil(t, err) err = client.Set("key2", "value2") assert.Nil(t, err) + _, _, err = New(client.Addr, badType()).Scan(0, "*", 100) + assert.NotNil(t, err) keys, _, err := client.Scan(0, "*", 100) assert.Nil(t, err) assert.ElementsMatch(t, []string{"key1", "key2"}, keys) @@ -478,6 +549,8 @@ func TestRedis_Sscan(t *testing.T) { var cursor uint64 = 0 sum := 0 for { + _, _, err := New(client.Addr, badType()).Sscan(key, cursor, "", 100) + assert.NotNil(t, err) keys, next, err := client.Sscan(key, cursor, "", 100) assert.Nil(t, err) sum += len(keys) @@ -488,6 +561,8 @@ func TestRedis_Sscan(t *testing.T) { } assert.Equal(t, sum, 1550) + _, err = New(client.Addr, badType()).Del(key) + assert.NotNil(t, err) _, err = client.Del(key) assert.Nil(t, err) }) @@ -495,46 +570,72 @@ func TestRedis_Sscan(t *testing.T) { func TestRedis_Set(t *testing.T) { runOnRedis(t, func(client *Redis) { + _, err := New(client.Addr, badType()).Sadd("key", 1, 2, 3, 4) + assert.NotNil(t, err) num, err := client.Sadd("key", 1, 2, 3, 4) assert.Nil(t, err) assert.Equal(t, 4, num) + _, err = New(client.Addr, badType()).Scard("key") + assert.NotNil(t, err) val, err := client.Scard("key") assert.Nil(t, err) assert.Equal(t, int64(4), val) + _, err = New(client.Addr, badType()).Sismember("key", 2) + assert.NotNil(t, err) ok, err := client.Sismember("key", 2) assert.Nil(t, err) assert.True(t, ok) + _, err = New(client.Addr, badType()).Srem("key", 3, 4) + assert.NotNil(t, err) num, err = client.Srem("key", 3, 4) assert.Nil(t, err) assert.Equal(t, 2, num) + _, err = New(client.Addr, badType()).Smembers("key") + assert.NotNil(t, err) vals, err := client.Smembers("key") assert.Nil(t, err) assert.ElementsMatch(t, []string{"1", "2"}, vals) + _, err = New(client.Addr, badType()).Srandmember("key", 1) + assert.NotNil(t, err) members, err := client.Srandmember("key", 1) assert.Nil(t, err) assert.Len(t, members, 1) assert.Contains(t, []string{"1", "2"}, members[0]) + _, err = New(client.Addr, badType()).Spop("key") + assert.NotNil(t, err) member, err := client.Spop("key") assert.Nil(t, err) assert.Contains(t, []string{"1", "2"}, member) + _, err = New(client.Addr, badType()).Smembers("key") + assert.NotNil(t, err) vals, err = client.Smembers("key") assert.Nil(t, err) assert.NotContains(t, vals, member) + _, err = New(client.Addr, badType()).Sadd("key1", 1, 2, 3, 4) + assert.NotNil(t, err) num, err = client.Sadd("key1", 1, 2, 3, 4) assert.Nil(t, err) assert.Equal(t, 4, num) num, err = client.Sadd("key2", 2, 3, 4, 5) assert.Nil(t, err) assert.Equal(t, 4, num) + _, err = New(client.Addr, badType()).Sunion("key1", "key2") + assert.NotNil(t, err) vals, err = client.Sunion("key1", "key2") assert.Nil(t, err) assert.ElementsMatch(t, []string{"1", "2", "3", "4", "5"}, vals) + _, err = New(client.Addr, badType()).Sunionstore("key3", "key1", "key2") + assert.NotNil(t, err) num, err = client.Sunionstore("key3", "key1", "key2") assert.Nil(t, err) assert.Equal(t, 5, num) + _, err = New(client.Addr, badType()).Sdiff("key1", "key2") + assert.NotNil(t, err) vals, err = client.Sdiff("key1", "key2") assert.Nil(t, err) assert.EqualValues(t, []string{"1"}, vals) + _, err = New(client.Addr, badType()).Sdiffstore("key4", "key1", "key2") + assert.NotNil(t, err) num, err = client.Sdiffstore("key4", "key1", "key2") assert.Nil(t, err) assert.Equal(t, 1, num) @@ -543,8 +644,12 @@ func TestRedis_Set(t *testing.T) { func TestRedis_SetGetDel(t *testing.T) { runOnRedis(t, func(client *Redis) { - err := client.Set("hello", "world") + err := New(client.Addr, badType()).Set("hello", "world") + assert.NotNil(t, err) + err = client.Set("hello", "world") assert.Nil(t, err) + _, err = New(client.Addr, badType()).Get("hello") + assert.NotNil(t, err) val, err := client.Get("hello") assert.Nil(t, err) assert.Equal(t, "world", val) @@ -556,8 +661,12 @@ func TestRedis_SetGetDel(t *testing.T) { func TestRedis_SetExNx(t *testing.T) { runOnRedis(t, func(client *Redis) { - err := client.Setex("hello", "world", 5) + err := New(client.Addr, badType()).Setex("hello", "world", 5) + assert.NotNil(t, err) + err = client.Setex("hello", "world", 5) assert.Nil(t, err) + _, err = New(client.Addr, badType()).Setnx("hello", "newworld") + assert.NotNil(t, err) ok, err := client.Setnx("hello", "newworld") assert.Nil(t, err) assert.False(t, ok) @@ -573,6 +682,8 @@ func TestRedis_SetExNx(t *testing.T) { ttl, err := client.Ttl("hello") assert.Nil(t, err) assert.True(t, ttl > 0) + _, err = New(client.Addr, badType()).SetnxEx("newhello", "newworld", 5) + assert.NotNil(t, err) ok, err = client.SetnxEx("newhello", "newworld", 5) assert.Nil(t, err) assert.False(t, ok) @@ -592,12 +703,18 @@ func TestRedis_SetGetDelHashField(t *testing.T) { runOnRedis(t, func(client *Redis) { err := client.Hset("key", "field", "value") assert.Nil(t, err) + _, err = New(client.Addr, badType()).Hget("key", "field") + assert.NotNil(t, err) val, err := client.Hget("key", "field") assert.Nil(t, err) assert.Equal(t, "value", val) + _, err = New(client.Addr, badType()).Hexists("key", "field") + assert.NotNil(t, err) ok, err := client.Hexists("key", "field") assert.Nil(t, err) assert.True(t, ok) + _, err = New(client.Addr, badType()).Hdel("key", "field") + assert.NotNil(t, err) ret, err := client.Hdel("key", "field") assert.Nil(t, err) assert.True(t, ret) @@ -618,12 +735,18 @@ func TestRedis_SortedSet(t *testing.T) { val, err := client.Zscore("key", "value1") assert.Nil(t, err) assert.Equal(t, int64(2), val) + _, err = New(client.Addr, badType()).Zincrby("key", 3, "value1") + assert.NotNil(t, err) val, err = client.Zincrby("key", 3, "value1") assert.Nil(t, err) assert.Equal(t, int64(5), val) + _, err = New(client.Addr, badType()).Zscore("key", "value1") + assert.NotNil(t, err) val, err = client.Zscore("key", "value1") assert.Nil(t, err) assert.Equal(t, int64(5), val) + _, err = New(client.Addr, badType()).Zadds("key") + assert.NotNil(t, err) val, err = client.Zadds("key", Pair{ Key: "value2", Score: 6, @@ -633,6 +756,8 @@ func TestRedis_SortedSet(t *testing.T) { }) assert.Nil(t, err) assert.Equal(t, int64(2), val) + _, err = New(client.Addr, badType()).ZRevRangeWithScores("key", 1, 3) + assert.NotNil(t, err) pairs, err := client.ZRevRangeWithScores("key", 1, 3) assert.Nil(t, err) assert.EqualValues(t, []Pair{ @@ -651,8 +776,12 @@ func TestRedis_SortedSet(t *testing.T) { rank, err = client.Zrevrank("key", "value1") assert.Nil(t, err) assert.Equal(t, int64(2), rank) + _, err = New(client.Addr, badType()).Zrank("key", "value4") + assert.NotNil(t, err) _, err = client.Zrank("key", "value4") assert.Equal(t, Nil, err) + _, err = New(client.Addr, badType()).Zrem("key", "value2", "value3") + assert.NotNil(t, err) num, err := client.Zrem("key", "value2", "value3") assert.Nil(t, err) assert.Equal(t, 2, num) @@ -665,30 +794,46 @@ func TestRedis_SortedSet(t *testing.T) { ok, err = client.Zadd("key", 8, "value4") assert.Nil(t, err) assert.True(t, ok) + _, err = New(client.Addr, badType()).Zremrangebyscore("key", 6, 7) + assert.NotNil(t, err) num, err = client.Zremrangebyscore("key", 6, 7) assert.Nil(t, err) assert.Equal(t, 2, num) ok, err = client.Zadd("key", 6, "value2") assert.Nil(t, err) assert.True(t, ok) + _, err = New(client.Addr, badType()).Zadd("key", 7, "value3") + assert.NotNil(t, err) ok, err = client.Zadd("key", 7, "value3") assert.Nil(t, err) assert.True(t, ok) + _, err = New(client.Addr, badType()).Zcount("key", 6, 7) + assert.NotNil(t, err) num, err = client.Zcount("key", 6, 7) assert.Nil(t, err) assert.Equal(t, 2, num) + _, err = New(client.Addr, badType()).Zremrangebyrank("key", 1, 2) + assert.NotNil(t, err) num, err = client.Zremrangebyrank("key", 1, 2) assert.Nil(t, err) assert.Equal(t, 2, num) + _, err = New(client.Addr, badType()).Zcard("key") + assert.NotNil(t, err) card, err := client.Zcard("key") assert.Nil(t, err) assert.Equal(t, 2, card) + _, err = New(client.Addr, badType()).Zrange("key", 0, -1) + assert.NotNil(t, err) vals, err := client.Zrange("key", 0, -1) assert.Nil(t, err) assert.EqualValues(t, []string{"value1", "value4"}, vals) + _, err = New(client.Addr, badType()).Zrevrange("key", 0, -1) + assert.NotNil(t, err) vals, err = client.Zrevrange("key", 0, -1) assert.Nil(t, err) assert.EqualValues(t, []string{"value4", "value1"}, vals) + _, err = New(client.Addr, badType()).ZrangeWithScores("key", 0, -1) + assert.NotNil(t, err) pairs, err = client.ZrangeWithScores("key", 0, -1) assert.Nil(t, err) assert.EqualValues(t, []Pair{ @@ -701,6 +846,8 @@ func TestRedis_SortedSet(t *testing.T) { Score: 8, }, }, pairs) + _, err = New(client.Addr, badType()).ZrangebyscoreWithScores("key", 5, 8) + assert.NotNil(t, err) pairs, err = client.ZrangebyscoreWithScores("key", 5, 8) assert.Nil(t, err) assert.EqualValues(t, []Pair{ @@ -713,6 +860,9 @@ func TestRedis_SortedSet(t *testing.T) { Score: 8, }, }, pairs) + _, err = New(client.Addr, badType()).ZrangebyscoreWithScoresAndLimit( + "key", 5, 8, 1, 1) + assert.NotNil(t, err) pairs, err = client.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1) assert.Nil(t, err) assert.EqualValues(t, []Pair{ @@ -724,6 +874,8 @@ func TestRedis_SortedSet(t *testing.T) { pairs, err = client.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 0) assert.Nil(t, err) assert.Equal(t, 0, len(pairs)) + _, err = New(client.Addr, badType()).ZrevrangebyscoreWithScores("key", 5, 8) + assert.NotNil(t, err) pairs, err = client.ZrevrangebyscoreWithScores("key", 5, 8) assert.Nil(t, err) assert.EqualValues(t, []Pair{ @@ -736,6 +888,9 @@ func TestRedis_SortedSet(t *testing.T) { Score: 5, }, }, pairs) + _, err = New(client.Addr, badType()).ZrevrangebyscoreWithScoresAndLimit( + "key", 5, 8, 1, 1) + assert.NotNil(t, err) pairs, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1) assert.Nil(t, err) assert.EqualValues(t, []Pair{ @@ -747,6 +902,8 @@ func TestRedis_SortedSet(t *testing.T) { pairs, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 0) assert.Nil(t, err) assert.Equal(t, 0, len(pairs)) + _, err = New(client.Addr, badType()).Zrevrank("key", "value") + assert.NotNil(t, err) client.Zadd("second", 2, "aa") client.Zadd("third", 3, "bbb") val, err = client.Zunionstore("union", ZStore{ @@ -755,6 +912,8 @@ func TestRedis_SortedSet(t *testing.T) { }, "second", "third") assert.Nil(t, err) assert.Equal(t, int64(2), val) + _, err = New(client.Addr, badType()).Zunionstore("union", ZStore{}) + assert.NotNil(t, err) vals, err = client.Zrange("union", 0, 10000) assert.Nil(t, err) assert.EqualValues(t, []string{"aa", "bbb"}, vals) @@ -766,6 +925,9 @@ func TestRedis_SortedSet(t *testing.T) { func TestRedis_Pipelined(t *testing.T) { runOnRedis(t, func(client *Redis) { + assert.NotNil(t, New(client.Addr, badType()).Pipelined(func(pipeliner Pipeliner) error { + return nil + })) err := client.Pipelined( func(pipe Pipeliner) error { pipe.Incr("pipelined_counter") @@ -775,6 +937,8 @@ func TestRedis_Pipelined(t *testing.T) { }, ) assert.Nil(t, err) + _, err = New(client.Addr, badType()).Ttl("pipelined_counter") + assert.NotNil(t, err) ttl, err := client.Ttl("pipelined_counter") assert.Nil(t, err) assert.Equal(t, 3600, ttl) @@ -793,13 +957,16 @@ func TestRedisString(t *testing.T) { _, err := getRedis(NewRedis(client.Addr, ClusterType)) assert.Nil(t, err) assert.Equal(t, client.Addr, client.String()) + assert.NotNil(t, New(client.Addr, badType()).Ping()) }) } func TestRedisScriptLoad(t *testing.T) { runOnRedis(t, func(client *Redis) { client.Ping() - _, err := client.ScriptLoad("foo") + _, err := New(client.Addr, badType()).ScriptLoad("foo") + assert.NotNil(t, err) + _, err = client.ScriptLoad("foo") assert.NotNil(t, err) }) } @@ -897,6 +1064,13 @@ func TestRedisGeo(t *testing.T) { }) } +func TestRedis_WithPass(t *testing.T) { + runOnRedis(t, func(client *Redis) { + err := NewRedis(client.Addr, NodeType, "any").Ping() + assert.NotNil(t, err) + }) +} + func runOnRedis(t *testing.T, fn func(client *Redis)) { s, err := miniredis.Run() assert.Nil(t, err) @@ -936,6 +1110,12 @@ func runOnRedisTLS(t *testing.T, fn func(client *Redis)) { fn(New(s.Addr(), WithTLS())) } +func badType() Option { + return func(r *Redis) { + r.Type = "bad" + } +} + type mockedNode struct { RedisNode } diff --git a/core/stores/redis/redisclientmanager.go b/core/stores/redis/redisclientmanager.go index 4d18ded1..9467f34c 100644 --- a/core/stores/redis/redisclientmanager.go +++ b/core/stores/redis/redisclientmanager.go @@ -16,21 +16,17 @@ const ( var clientManager = syncx.NewResourceManager() -func getClient(server, pass string) (*red.Client, error) { - return getClientWithTLS(server, pass, false) -} - -func getClientWithTLS(server, pass string, tlsEnabled bool) (*red.Client, error) { - val, err := clientManager.GetResource(server, func() (io.Closer, error) { +func getClient(r *Redis) (*red.Client, error) { + val, err := clientManager.GetResource(r.Addr, func() (io.Closer, error) { var tlsConfig *tls.Config - if tlsEnabled { + if r.tls { tlsConfig = &tls.Config{ InsecureSkipVerify: true, } } store := red.NewClient(&red.Options{ - Addr: server, - Password: pass, + Addr: r.Addr, + Password: r.Pass, DB: defaultDatabase, MaxRetries: maxRetries, MinIdleConns: idleConns, diff --git a/core/stores/redis/redisclustermanager.go b/core/stores/redis/redisclustermanager.go index d2d5bdcd..73c6dd05 100644 --- a/core/stores/redis/redisclustermanager.go +++ b/core/stores/redis/redisclustermanager.go @@ -10,21 +10,17 @@ import ( var clusterManager = syncx.NewResourceManager() -func getCluster(server, pass string) (*red.ClusterClient, error) { - return getClusterWithTLS(server, pass, false) -} - -func getClusterWithTLS(server, pass string, tlsEnabled bool) (*red.ClusterClient, error) { - val, err := clusterManager.GetResource(server, func() (io.Closer, error) { +func getCluster(r *Redis) (*red.ClusterClient, error) { + val, err := clusterManager.GetResource(r.Addr, func() (io.Closer, error) { var tlsConfig *tls.Config - if tlsEnabled { + if r.tls { tlsConfig = &tls.Config{ InsecureSkipVerify: true, } } store := red.NewClusterClient(&red.ClusterOptions{ - Addrs: []string{server}, - Password: pass, + Addrs: []string{r.Addr}, + Password: r.Pass, MaxRetries: maxRetries, MinIdleConns: idleConns, TLSConfig: tlsConfig,