From da1a93e9325b898533756e0f6054b6294f0350c4 Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 11 Oct 2020 22:07:50 +0800 Subject: [PATCH] faster the tests --- core/stores/cache/cache_test.go | 19 +++++++------- core/stores/cache/cachenode_test.go | 39 +++++++++++------------------ core/stores/cache/util_test.go | 22 ++++++++++++++++ 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/core/stores/cache/cache_test.go b/core/stores/cache/cache_test.go index 26336c4d..43e873d8 100644 --- a/core/stores/cache/cache_test.go +++ b/core/stores/cache/cache_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "github.com/alicebob/miniredis" "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/core/errorx" "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) { const total = 1000 - r1 := miniredis.NewMiniRedis() - assert.Nil(t, r1.Start()) - defer r1.Close() - r2 := miniredis.NewMiniRedis() - assert.Nil(t, r2.Start()) - defer r2.Close() + r1, clean1, err := createMiniRedis() + assert.Nil(t, err) + defer clean1() + r2, clean2, err := createMiniRedis() + assert.Nil(t, err) + defer clean2() conf := ClusterConf{ { RedisConf: redis.RedisConf{ @@ -124,9 +123,9 @@ func TestCache_SetDel(t *testing.T) { func TestCache_OneNode(t *testing.T) { const total = 1000 - r := miniredis.NewMiniRedis() - assert.Nil(t, r.Start()) - defer r.Close() + r, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() conf := ClusterConf{ { RedisConf: redis.RedisConf{ diff --git a/core/stores/cache/cachenode_test.go b/core/stores/cache/cachenode_test.go index b6c1fad5..ccd3276f 100644 --- a/core/stores/cache/cachenode_test.go +++ b/core/stores/cache/cachenode_test.go @@ -11,7 +11,6 @@ import ( "github.com/alicebob/miniredis" "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/mathx" "github.com/tal-tech/go-zero/core/stat" @@ -27,9 +26,9 @@ func init() { } func TestCacheNode_DelCache(t *testing.T) { - s, err := miniredis.Run() + s, clean, err := createMiniRedis() assert.Nil(t, err) - defer s.Close() + defer clean() cn := cacheNode{ rds: redis.NewRedis(s.Addr(), redis.NodeType), @@ -50,9 +49,9 @@ func TestCacheNode_DelCache(t *testing.T) { } func TestCacheNode_InvalidCache(t *testing.T) { - s, err := miniredis.Run() + s, clean, err := createMiniRedis() assert.Nil(t, err) - defer s.Close() + defer clean() cn := cacheNode{ rds: redis.NewRedis(s.Addr(), redis.NodeType), @@ -71,9 +70,9 @@ func TestCacheNode_InvalidCache(t *testing.T) { } func TestCacheNode_Take(t *testing.T) { - s, err := miniredis.Run() + s, clean, err := createMiniRedis() assert.Nil(t, err) - defer s.Close() + defer clean() cn := cacheNode{ rds: redis.NewRedis(s.Addr(), redis.NodeType), @@ -98,9 +97,9 @@ func TestCacheNode_Take(t *testing.T) { } func TestCacheNode_TakeNotFound(t *testing.T) { - s, err := miniredis.Run() + s, clean, err := createMiniRedis() assert.Nil(t, err) - defer s.Close() + defer clean() cn := cacheNode{ rds: redis.NewRedis(s.Addr(), redis.NodeType), @@ -137,9 +136,9 @@ func TestCacheNode_TakeNotFound(t *testing.T) { } func TestCacheNode_TakeWithExpire(t *testing.T) { - s, err := miniredis.Run() + s, clean, err := createMiniRedis() assert.Nil(t, err) - defer s.Close() + defer clean() cn := cacheNode{ rds: redis.NewRedis(s.Addr(), redis.NodeType), @@ -164,9 +163,9 @@ func TestCacheNode_TakeWithExpire(t *testing.T) { } func TestCacheNode_String(t *testing.T) { - s, err := miniredis.Run() + s, clean, err := createMiniRedis() assert.Nil(t, err) - defer s.Close() + defer clean() cn := cacheNode{ rds: redis.NewRedis(s.Addr(), redis.NodeType), @@ -181,19 +180,9 @@ func TestCacheNode_String(t *testing.T) { } func TestCacheValueWithBigInt(t *testing.T) { - s, err := miniredis.Run() + s, clean, err := createMiniRedis() assert.Nil(t, err) - defer func() { - ch := make(chan lang.PlaceholderType) - go func() { - s.Close() - close(ch) - }() - select { - case <-ch: - case <-time.After(time.Second): - } - }() + defer clean() cn := cacheNode{ rds: redis.NewRedis(s.Addr(), redis.NodeType), diff --git a/core/stores/cache/util_test.go b/core/stores/cache/util_test.go index 95e48b62..184e2217 100644 --- a/core/stores/cache/util_test.go +++ b/core/stores/cache/util_test.go @@ -2,8 +2,11 @@ package cache import ( "testing" + "time" + "github.com/alicebob/miniredis" "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/lang" ) func TestFormatKeys(t *testing.T) { @@ -24,3 +27,22 @@ func TestTotalWeights(t *testing.T) { }) 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 +}