chore: add more tests (#2795)

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests
This commit is contained in:
Kevin Wan 2023-01-15 21:32:41 +08:00 committed by GitHub
parent 963b52fb1b
commit 5f02e623f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 210 additions and 74 deletions

View File

@ -10,6 +10,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/alicebob/miniredis/v2"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/errorx" "github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/core/hash" "github.com/zeromicro/go-zero/core/hash"
@ -109,6 +110,7 @@ func (mc *mockedNode) TakeWithExpireCtx(ctx context.Context, val interface{}, ke
} }
func TestCache_SetDel(t *testing.T) { func TestCache_SetDel(t *testing.T) {
t.Run("test set del", func(t *testing.T) {
const total = 1000 const total = 1000
r1, clean1, err := redistest.CreateRedis() r1, clean1, err := redistest.CreateRedis()
assert.Nil(t, err) assert.Nil(t, err)
@ -149,11 +151,44 @@ func TestCache_SetDel(t *testing.T) {
for i := 0; i < total; i++ { for i := 0; i < total; i++ {
assert.Nil(t, c.Del(fmt.Sprintf("key/%d", 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++ { for i := 0; i < total; i++ {
var val int var val int
assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &val))) assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &val)))
assert.Equal(t, 0, 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) { func TestCache_OneNode(t *testing.T) {

View File

@ -2278,12 +2278,25 @@ func (s *Redis) ZrangeWithScoresByFloatCtx(ctx context.Context, key string, star
} }
// ZRevRangeWithScores is the implementation of redis zrevrange command with scores. // 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) { 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. // 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) ( 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) { val []Pair, err error) {
err = s.brk.DoWithAcceptable(func() error { err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s) 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. // 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) { 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. // 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) ( 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) { val []FloatPair, err error) {
err = s.brk.DoWithAcceptable(func() error { err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s) conn, err := getRedis(s)

View File

@ -959,7 +959,11 @@ func TestRedis_SortedSet(t *testing.T) {
assert.Equal(t, int64(2), val) assert.Equal(t, int64(2), val)
_, err = New(client.Addr, badType()).ZRevRangeWithScores("key", 1, 3) _, err = New(client.Addr, badType()).ZRevRangeWithScores("key", 1, 3)
assert.NotNil(t, err) 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.Nil(t, err)
assert.EqualValues(t, []Pair{ assert.EqualValues(t, []Pair{
{ {
@ -1129,65 +1133,105 @@ func TestRedis_SortedSet(t *testing.T) {
runOnRedisWithError(t, func(client *Redis) { runOnRedisWithError(t, func(client *Redis) {
_, err := client.ZaddFloat("key", 1, "value") _, err := client.ZaddFloat("key", 1, "value")
assert.Error(t, err) assert.Error(t, err)
})
_, err = client.Zadds("key") runOnRedisWithError(t, func(client *Redis) {
_, err := client.Zadds("key")
assert.Error(t, err) assert.Error(t, err)
})
_, err = client.Zcard("key") runOnRedisWithError(t, func(client *Redis) {
_, err := client.Zcard("key")
assert.Error(t, err) 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) 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) assert.Error(t, err)
})
_, err = client.Zscore("key", "value") runOnRedisWithError(t, func(client *Redis) {
_, err := client.Zscore("key", "value")
assert.Error(t, err) assert.Error(t, err)
})
_, err = client.Zrem("key", "value") runOnRedisWithError(t, func(client *Redis) {
_, err := client.Zrem("key", "value")
assert.Error(t, err) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) assert.Error(t, err)
}) })
}) })
@ -1206,7 +1250,11 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
_, _ = client.ZaddFloat("key", 10.346, "value2") _, _ = client.ZaddFloat("key", 10.346, "value2")
_, err = New(client.Addr, badType()).ZRevRangeWithScoresByFloat("key", 0, -1) _, err = New(client.Addr, badType()).ZRevRangeWithScoresByFloat("key", 0, -1)
assert.NotNil(t, err) 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.Nil(t, err)
assert.EqualValues(t, []FloatPair{ assert.EqualValues(t, []FloatPair{
{ {

View File

@ -8,12 +8,39 @@ import (
) )
func TestBlockingNode(t *testing.T) { func TestBlockingNode(t *testing.T) {
t.Run("test blocking node", func(t *testing.T) {
r, err := miniredis.Run() r, err := miniredis.Run()
assert.Nil(t, err) assert.NoError(t, err)
defer r.Close()
node, err := CreateBlockingNode(New(r.Addr())) node, err := CreateBlockingNode(New(r.Addr()))
assert.Nil(t, err) assert.NoError(t, err)
node.Close() node.Close()
node, err = CreateBlockingNode(New(r.Addr(), Cluster())) // close again to make sure it's safe
assert.Nil(t, err) assert.NotPanics(t, func() {
node.Close() 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)
})
} }