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
master
Kevin Wan 2 years ago committed by GitHub
parent 963b52fb1b
commit 5f02e623f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,6 +110,7 @@ func (mc *mockedNode) TakeWithExpireCtx(ctx context.Context, val interface{}, ke
}
func TestCache_SetDel(t *testing.T) {
t.Run("test set del", func(t *testing.T) {
const total = 1000
r1, clean1, err := redistest.CreateRedis()
assert.Nil(t, err)
@ -149,11 +151,44 @@ func TestCache_SetDel(t *testing.T) {
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) {

@ -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)

@ -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{
{

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

Loading…
Cancel
Save