diff --git a/core/stores/cache/cache_test.go b/core/stores/cache/cache_test.go index 2e12734a..9c5b843a 100644 --- a/core/stores/cache/cache_test.go +++ b/core/stores/cache/cache_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/alicebob/miniredis/v2" "github.com/stretchr/testify/assert" "github.com/zeromicro/go-zero/core/errorx" "github.com/zeromicro/go-zero/core/hash" @@ -109,51 +110,85 @@ func (mc *mockedNode) TakeWithExpireCtx(ctx context.Context, val interface{}, ke } func TestCache_SetDel(t *testing.T) { - const total = 1000 - r1, clean1, err := redistest.CreateRedis() - assert.Nil(t, err) - defer clean1() - r2, clean2, err := redistest.CreateRedis() - assert.Nil(t, err) - defer clean2() - conf := ClusterConf{ - { - RedisConf: redis.RedisConf{ - Host: r1.Addr, - Type: redis.NodeType, + t.Run("test set del", func(t *testing.T) { + const total = 1000 + r1, clean1, err := redistest.CreateRedis() + assert.Nil(t, err) + defer clean1() + r2, clean2, err := redistest.CreateRedis() + assert.Nil(t, err) + defer clean2() + conf := ClusterConf{ + { + RedisConf: redis.RedisConf{ + Host: r1.Addr, + Type: redis.NodeType, + }, + Weight: 100, }, - Weight: 100, - }, - { - RedisConf: redis.RedisConf{ - Host: r2.Addr, - Type: redis.NodeType, + { + RedisConf: redis.RedisConf{ + Host: r2.Addr, + Type: redis.NodeType, + }, + Weight: 100, }, - Weight: 100, - }, - } - c := New(conf, syncx.NewSingleFlight(), NewStat("mock"), errPlaceholder) - for i := 0; i < total; i++ { - if i%2 == 0 { - assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i)) - } else { - assert.Nil(t, c.SetWithExpire(fmt.Sprintf("key/%d", i), i, 0)) } - } - for i := 0; i < total; i++ { - var val int - assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &val)) - assert.Equal(t, i, val) - } - assert.Nil(t, c.Del()) - for i := 0; i < total; i++ { - assert.Nil(t, c.Del(fmt.Sprintf("key/%d", i))) - } - for i := 0; i < total; i++ { - var val int - assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &val))) - assert.Equal(t, 0, val) - } + c := New(conf, syncx.NewSingleFlight(), NewStat("mock"), errPlaceholder) + for i := 0; i < total; i++ { + if i%2 == 0 { + assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i)) + } else { + assert.Nil(t, c.SetWithExpire(fmt.Sprintf("key/%d", i), i, 0)) + } + } + for i := 0; i < total; i++ { + var val int + assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &val)) + assert.Equal(t, i, val) + } + assert.Nil(t, c.Del()) + for i := 0; i < total; i++ { + assert.Nil(t, c.Del(fmt.Sprintf("key/%d", i))) + } + assert.Nil(t, c.Del("a", "b", "c")) + for i := 0; i < total; i++ { + var val int + assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &val))) + assert.Equal(t, 0, val) + } + }) + + t.Run("test set del error", func(t *testing.T) { + r1, err := miniredis.Run() + assert.NoError(t, err) + defer r1.Close() + r1.SetError("mock error") + + r2, err := miniredis.Run() + assert.NoError(t, err) + defer r2.Close() + r2.SetError("mock error") + + conf := ClusterConf{ + { + RedisConf: redis.RedisConf{ + Host: r1.Addr(), + Type: redis.NodeType, + }, + Weight: 100, + }, + { + RedisConf: redis.RedisConf{ + Host: r2.Addr(), + Type: redis.NodeType, + }, + Weight: 100, + }, + } + c := New(conf, syncx.NewSingleFlight(), NewStat("mock"), errPlaceholder) + assert.NoError(t, c.Del("a", "b", "c")) + }) } func TestCache_OneNode(t *testing.T) { diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go index ac11fbe5..f6884efe 100644 --- a/core/stores/redis/redis.go +++ b/core/stores/redis/redis.go @@ -2278,12 +2278,25 @@ func (s *Redis) ZrangeWithScoresByFloatCtx(ctx context.Context, key string, star } // ZRevRangeWithScores is the implementation of redis zrevrange command with scores. +// Deprecated: use ZrevrangeWithScores instead. func (s *Redis) ZRevRangeWithScores(key string, start, stop int64) ([]Pair, error) { - return s.ZRevRangeWithScoresCtx(context.Background(), key, start, stop) + return s.ZrevrangeWithScoresCtx(context.Background(), key, start, stop) +} + +// ZrevrangeWithScores is the implementation of redis zrevrange command with scores. +func (s *Redis) ZrevrangeWithScores(key string, start, stop int64) ([]Pair, error) { + return s.ZrevrangeWithScoresCtx(context.Background(), key, start, stop) } // ZRevRangeWithScoresCtx is the implementation of redis zrevrange command with scores. +// Deprecated: use ZrevrangeWithScoresCtx instead. func (s *Redis) ZRevRangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ( + val []Pair, err error) { + return s.ZrevrangeWithScoresCtx(ctx, key, start, stop) +} + +// ZrevrangeWithScoresCtx is the implementation of redis zrevrange command with scores. +func (s *Redis) ZrevrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ( val []Pair, err error) { err = s.brk.DoWithAcceptable(func() error { conn, err := getRedis(s) @@ -2304,12 +2317,25 @@ func (s *Redis) ZRevRangeWithScoresCtx(ctx context.Context, key string, start, s } // ZRevRangeWithScoresByFloat is the implementation of redis zrevrange command with scores by float. +// Deprecated: use ZrevrangeWithScoresByFloat instead. func (s *Redis) ZRevRangeWithScoresByFloat(key string, start, stop int64) ([]FloatPair, error) { - return s.ZRevRangeWithScoresByFloatCtx(context.Background(), key, start, stop) + return s.ZrevrangeWithScoresByFloatCtx(context.Background(), key, start, stop) +} + +// ZrevrangeWithScoresByFloat is the implementation of redis zrevrange command with scores by float. +func (s *Redis) ZrevrangeWithScoresByFloat(key string, start, stop int64) ([]FloatPair, error) { + return s.ZrevrangeWithScoresByFloatCtx(context.Background(), key, start, stop) } // ZRevRangeWithScoresByFloatCtx is the implementation of redis zrevrange command with scores by float. +// Deprecated: use ZrevrangeWithScoresByFloatCtx instead. func (s *Redis) ZRevRangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) ( + val []FloatPair, err error) { + return s.ZrevrangeWithScoresByFloatCtx(ctx, key, start, stop) +} + +// ZrevrangeWithScoresByFloatCtx is the implementation of redis zrevrange command with scores by float. +func (s *Redis) ZrevrangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) ( val []FloatPair, err error) { err = s.brk.DoWithAcceptable(func() error { conn, err := getRedis(s) diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index ad4e4dc3..2c7e52e2 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -959,7 +959,11 @@ func TestRedis_SortedSet(t *testing.T) { 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) + _, err = client.ZRevRangeWithScores("key", 1, 3) + assert.Nil(t, err) + _, err = client.ZRevRangeWithScoresCtx(context.Background(), "key", 1, 3) + assert.Nil(t, err) + pairs, err := client.ZrevrangeWithScores("key", 1, 3) assert.Nil(t, err) assert.EqualValues(t, []Pair{ { @@ -1129,65 +1133,105 @@ func TestRedis_SortedSet(t *testing.T) { runOnRedisWithError(t, func(client *Redis) { _, err := client.ZaddFloat("key", 1, "value") assert.Error(t, err) + }) - _, err = client.Zadds("key") + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zadds("key") assert.Error(t, err) + }) - _, err = client.Zcard("key") + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zcard("key") assert.Error(t, err) + }) - _, err = client.Zcount("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zcount("key", 1, 2) assert.Error(t, err) + }) - _, err = client.Zincrby("key", 1, "value") + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zincrby("key", 1, "value") assert.Error(t, err) + }) - _, err = client.Zscore("key", "value") + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zscore("key", "value") assert.Error(t, err) + }) - _, err = client.Zrem("key", "value") + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zrem("key", "value") assert.Error(t, err) + }) - _, err = client.Zremrangebyscore("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zremrangebyscore("key", 1, 2) assert.Error(t, err) + }) - _, err = client.Zremrangebyrank("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.Zremrangebyrank("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZrangeWithScores("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrangeWithScores("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZrangeWithScoresByFloat("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrangeWithScoresByFloat("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZRevRangeWithScores("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZRevRangeWithScores("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZRevRangeWithScoresByFloat("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZRevRangeWithScoresByFloat("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZrangebyscoreWithScores("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrangebyscoreWithScores("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZrangebyscoreWithScoresByFloat("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrangebyscoreWithScoresByFloat("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZrangebyscoreWithScoresAndLimit("key", 1, 2, 1, 1) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrangebyscoreWithScoresAndLimit("key", 1, 2, 1, 1) assert.Error(t, err) + }) - _, err = client.ZrangebyscoreWithScoresByFloatAndLimit("key", 1, 2, 1, 1) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrangebyscoreWithScoresByFloatAndLimit("key", 1, 2, 1, 1) assert.Error(t, err) + }) - _, err = client.ZrevrangebyscoreWithScores("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrevrangebyscoreWithScores("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZrevrangebyscoreWithScoresByFloat("key", 1, 2) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrevrangebyscoreWithScoresByFloat("key", 1, 2) assert.Error(t, err) + }) - _, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 1, 2, 1, 1) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrevrangebyscoreWithScoresAndLimit("key", 1, 2, 1, 1) assert.Error(t, err) + }) - _, err = client.ZrevrangebyscoreWithScoresByFloatAndLimit("key", 1, 2, 1, 1) + runOnRedisWithError(t, func(client *Redis) { + _, err := client.ZrevrangebyscoreWithScoresByFloatAndLimit("key", 1, 2, 1, 1) assert.Error(t, err) }) }) @@ -1206,7 +1250,11 @@ func TestRedis_SortedSetByFloat64(t *testing.T) { _, _ = client.ZaddFloat("key", 10.346, "value2") _, err = New(client.Addr, badType()).ZRevRangeWithScoresByFloat("key", 0, -1) assert.NotNil(t, err) - pairs, err := client.ZRevRangeWithScoresByFloat("key", 0, -1) + _, err = client.ZRevRangeWithScoresByFloat("key", 0, -1) + assert.Nil(t, err) + _, err = client.ZRevRangeWithScoresByFloatCtx(context.Background(), "key", 0, -1) + assert.Nil(t, err) + pairs, err := client.ZrevrangeWithScoresByFloat("key", 0, -1) assert.Nil(t, err) assert.EqualValues(t, []FloatPair{ { diff --git a/core/stores/redis/redisblockingnode_test.go b/core/stores/redis/redisblockingnode_test.go index 80ac70ce..dc0d4083 100644 --- a/core/stores/redis/redisblockingnode_test.go +++ b/core/stores/redis/redisblockingnode_test.go @@ -8,12 +8,39 @@ import ( ) func TestBlockingNode(t *testing.T) { - r, err := miniredis.Run() - assert.Nil(t, err) - node, err := CreateBlockingNode(New(r.Addr())) - assert.Nil(t, err) - node.Close() - node, err = CreateBlockingNode(New(r.Addr(), Cluster())) - assert.Nil(t, err) - node.Close() + t.Run("test blocking node", func(t *testing.T) { + r, err := miniredis.Run() + assert.NoError(t, err) + defer r.Close() + + node, err := CreateBlockingNode(New(r.Addr())) + assert.NoError(t, err) + node.Close() + // close again to make sure it's safe + assert.NotPanics(t, func() { + node.Close() + }) + }) + + t.Run("test blocking node with cluster", func(t *testing.T) { + r, err := miniredis.Run() + assert.NoError(t, err) + defer r.Close() + + node, err := CreateBlockingNode(New(r.Addr(), Cluster(), WithTLS())) + assert.NoError(t, err) + node.Close() + assert.NotPanics(t, func() { + node.Close() + }) + }) + + t.Run("test blocking node with bad type", func(t *testing.T) { + r, err := miniredis.Run() + assert.NoError(t, err) + defer r.Close() + + _, err = CreateBlockingNode(New(r.Addr(), badType())) + assert.Error(t, err) + }) }