faster the tests

master
kevin 4 years ago
parent 7e61555d42
commit da1a93e932

@ -8,7 +8,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/alicebob/miniredis"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/errorx" "github.com/tal-tech/go-zero/core/errorx"
"github.com/tal-tech/go-zero/core/hash" "github.com/tal-tech/go-zero/core/hash"
@ -76,12 +75,12 @@ func (mc *mockedNode) TakeWithExpire(v interface{}, key string, query func(v int
func TestCache_SetDel(t *testing.T) { func TestCache_SetDel(t *testing.T) {
const total = 1000 const total = 1000
r1 := miniredis.NewMiniRedis() r1, clean1, err := createMiniRedis()
assert.Nil(t, r1.Start()) assert.Nil(t, err)
defer r1.Close() defer clean1()
r2 := miniredis.NewMiniRedis() r2, clean2, err := createMiniRedis()
assert.Nil(t, r2.Start()) assert.Nil(t, err)
defer r2.Close() defer clean2()
conf := ClusterConf{ conf := ClusterConf{
{ {
RedisConf: redis.RedisConf{ RedisConf: redis.RedisConf{
@ -124,9 +123,9 @@ func TestCache_SetDel(t *testing.T) {
func TestCache_OneNode(t *testing.T) { func TestCache_OneNode(t *testing.T) {
const total = 1000 const total = 1000
r := miniredis.NewMiniRedis() r, clean, err := createMiniRedis()
assert.Nil(t, r.Start()) assert.Nil(t, err)
defer r.Close() defer clean()
conf := ClusterConf{ conf := ClusterConf{
{ {
RedisConf: redis.RedisConf{ RedisConf: redis.RedisConf{

@ -11,7 +11,6 @@ import (
"github.com/alicebob/miniredis" "github.com/alicebob/miniredis"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/mathx" "github.com/tal-tech/go-zero/core/mathx"
"github.com/tal-tech/go-zero/core/stat" "github.com/tal-tech/go-zero/core/stat"
@ -27,9 +26,9 @@ func init() {
} }
func TestCacheNode_DelCache(t *testing.T) { func TestCacheNode_DelCache(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
assert.Nil(t, err) assert.Nil(t, err)
defer s.Close() defer clean()
cn := cacheNode{ cn := cacheNode{
rds: redis.NewRedis(s.Addr(), redis.NodeType), rds: redis.NewRedis(s.Addr(), redis.NodeType),
@ -50,9 +49,9 @@ func TestCacheNode_DelCache(t *testing.T) {
} }
func TestCacheNode_InvalidCache(t *testing.T) { func TestCacheNode_InvalidCache(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
assert.Nil(t, err) assert.Nil(t, err)
defer s.Close() defer clean()
cn := cacheNode{ cn := cacheNode{
rds: redis.NewRedis(s.Addr(), redis.NodeType), rds: redis.NewRedis(s.Addr(), redis.NodeType),
@ -71,9 +70,9 @@ func TestCacheNode_InvalidCache(t *testing.T) {
} }
func TestCacheNode_Take(t *testing.T) { func TestCacheNode_Take(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
assert.Nil(t, err) assert.Nil(t, err)
defer s.Close() defer clean()
cn := cacheNode{ cn := cacheNode{
rds: redis.NewRedis(s.Addr(), redis.NodeType), rds: redis.NewRedis(s.Addr(), redis.NodeType),
@ -98,9 +97,9 @@ func TestCacheNode_Take(t *testing.T) {
} }
func TestCacheNode_TakeNotFound(t *testing.T) { func TestCacheNode_TakeNotFound(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
assert.Nil(t, err) assert.Nil(t, err)
defer s.Close() defer clean()
cn := cacheNode{ cn := cacheNode{
rds: redis.NewRedis(s.Addr(), redis.NodeType), rds: redis.NewRedis(s.Addr(), redis.NodeType),
@ -137,9 +136,9 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
} }
func TestCacheNode_TakeWithExpire(t *testing.T) { func TestCacheNode_TakeWithExpire(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
assert.Nil(t, err) assert.Nil(t, err)
defer s.Close() defer clean()
cn := cacheNode{ cn := cacheNode{
rds: redis.NewRedis(s.Addr(), redis.NodeType), rds: redis.NewRedis(s.Addr(), redis.NodeType),
@ -164,9 +163,9 @@ func TestCacheNode_TakeWithExpire(t *testing.T) {
} }
func TestCacheNode_String(t *testing.T) { func TestCacheNode_String(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
assert.Nil(t, err) assert.Nil(t, err)
defer s.Close() defer clean()
cn := cacheNode{ cn := cacheNode{
rds: redis.NewRedis(s.Addr(), redis.NodeType), rds: redis.NewRedis(s.Addr(), redis.NodeType),
@ -181,19 +180,9 @@ func TestCacheNode_String(t *testing.T) {
} }
func TestCacheValueWithBigInt(t *testing.T) { func TestCacheValueWithBigInt(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
assert.Nil(t, err) assert.Nil(t, err)
defer func() { defer clean()
ch := make(chan lang.PlaceholderType)
go func() {
s.Close()
close(ch)
}()
select {
case <-ch:
case <-time.After(time.Second):
}
}()
cn := cacheNode{ cn := cacheNode{
rds: redis.NewRedis(s.Addr(), redis.NodeType), rds: redis.NewRedis(s.Addr(), redis.NodeType),

@ -2,8 +2,11 @@ package cache
import ( import (
"testing" "testing"
"time"
"github.com/alicebob/miniredis"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/lang"
) )
func TestFormatKeys(t *testing.T) { func TestFormatKeys(t *testing.T) {
@ -24,3 +27,22 @@ func TestTotalWeights(t *testing.T) {
}) })
assert.Equal(t, 1, val) assert.Equal(t, 1, val)
} }
func createMiniRedis() (r *miniredis.Miniredis, clean func(), err error) {
r, err = miniredis.Run()
if err != nil {
return nil, nil, err
}
return r, func() {
ch := make(chan lang.PlaceholderType)
go func() {
r.Close()
close(ch)
}()
select {
case <-ch:
case <-time.After(time.Second):
}
}, nil
}

Loading…
Cancel
Save