From 1fd2ef9347a97a29f6ee3ccff3ea9810aa75a170 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 21 Oct 2020 21:43:41 +0800 Subject: [PATCH] make tests faster --- core/stores/sqlc/cachedsql_test.go | 135 ++++++++++++++-------------- tools/goctl/k8s/{k8s.go => kube.go} | 9 +- 2 files changed, 73 insertions(+), 71 deletions(-) rename tools/goctl/k8s/{k8s.go => kube.go} (94%) diff --git a/core/stores/sqlc/cachedsql_test.go b/core/stores/sqlc/cachedsql_test.go index 30579f8a..937d8798 100644 --- a/core/stores/sqlc/cachedsql_test.go +++ b/core/stores/sqlc/cachedsql_test.go @@ -16,6 +16,7 @@ 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/stat" "github.com/tal-tech/go-zero/core/stores/cache" @@ -30,10 +31,9 @@ func init() { func TestCachedConn_GetCache(t *testing.T) { resetStats() - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() r := redis.NewRedis(s.Addr(), redis.NodeType) c := NewNodeConn(dummySqlConn{}, r, cache.WithExpiry(time.Second*10)) @@ -48,10 +48,9 @@ func TestCachedConn_GetCache(t *testing.T) { func TestStat(t *testing.T) { resetStats() - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() r := redis.NewRedis(s.Addr(), redis.NodeType) c := NewNodeConn(dummySqlConn{}, r, cache.WithExpiry(time.Second*10)) @@ -73,10 +72,9 @@ func TestStat(t *testing.T) { func TestCachedConn_QueryRowIndex_NoCache(t *testing.T) { resetStats() - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() r := redis.NewRedis(s.Addr(), redis.NodeType) c := NewConn(dummySqlConn{}, cache.CacheConf{ @@ -124,10 +122,9 @@ func TestCachedConn_QueryRowIndex_NoCache(t *testing.T) { func TestCachedConn_QueryRowIndex_HasCache(t *testing.T) { resetStats() - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() r := redis.NewRedis(s.Addr(), redis.NodeType) c := NewNodeConn(dummySqlConn{}, r, cache.WithExpiry(time.Second*10), @@ -213,11 +210,9 @@ func TestCachedConn_QueryRowIndex_HasCache_IntPrimary(t *testing.T) { }, } - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } - defer s.Close() + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() for _, test := range tests { t.Run(test.name, func(t *testing.T) { @@ -261,12 +256,9 @@ func TestCachedConn_QueryRowIndex_HasWrongCache(t *testing.T) { for k, v := range caches { t.Run(k+"/"+v, func(t *testing.T) { resetStats() - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } - s.FlushAll() - defer s.Close() + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() r := redis.NewRedis(s.Addr(), redis.NodeType) c := NewNodeConn(dummySqlConn{}, r, cache.WithExpiry(time.Second*10), @@ -320,10 +312,9 @@ func TestStatCacheFails(t *testing.T) { func TestStatDbFails(t *testing.T) { resetStats() - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() r := redis.NewRedis(s.Addr(), redis.NodeType) c := NewNodeConn(dummySqlConn{}, r, cache.WithExpiry(time.Second*10)) @@ -343,10 +334,9 @@ func TestStatDbFails(t *testing.T) { func TestStatFromMemory(t *testing.T) { resetStats() - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() r := redis.NewRedis(s.Addr(), redis.NodeType) c := NewNodeConn(dummySqlConn{}, r, cache.WithExpiry(time.Second*10)) @@ -403,10 +393,9 @@ func TestStatFromMemory(t *testing.T) { } func TestCachedConnQueryRow(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() const ( key = "user" @@ -433,10 +422,9 @@ func TestCachedConnQueryRow(t *testing.T) { } func TestCachedConnQueryRowFromCache(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() const ( key = "user" @@ -464,10 +452,9 @@ func TestCachedConnQueryRowFromCache(t *testing.T) { } func TestQueryRowNotFound(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() const key = "user" var conn trackedConn @@ -486,10 +473,9 @@ func TestQueryRowNotFound(t *testing.T) { } func TestCachedConnExec(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() var conn trackedConn r := redis.NewRedis(s.Addr(), redis.NodeType) @@ -500,10 +486,9 @@ func TestCachedConnExec(t *testing.T) { } func TestCachedConnExecDropCache(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() const ( key = "user" @@ -539,10 +524,9 @@ func TestCachedConnExecDropCacheFailed(t *testing.T) { } func TestCachedConnQueryRows(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() var conn trackedConn r := redis.NewRedis(s.Addr(), redis.NodeType) @@ -554,10 +538,9 @@ func TestCachedConnQueryRows(t *testing.T) { } func TestCachedConnTransact(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() var conn trackedConn r := redis.NewRedis(s.Addr(), redis.NodeType) @@ -570,10 +553,9 @@ func TestCachedConnTransact(t *testing.T) { } func TestQueryRowNoCache(t *testing.T) { - s, err := miniredis.Run() - if err != nil { - t.Error(err) - } + s, clean, err := createMiniRedis() + assert.Nil(t, err) + defer clean() const ( key = "user" @@ -657,3 +639,22 @@ func (c *trackedConn) Transact(fn func(session sqlx.Session) error) error { c.transactValue = true return c.dummySqlConn.Transact(fn) } + +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 +} diff --git a/tools/goctl/k8s/k8s.go b/tools/goctl/k8s/kube.go similarity index 94% rename from tools/goctl/k8s/k8s.go rename to tools/goctl/k8s/kube.go index a0a60fbc..af065921 100644 --- a/tools/goctl/k8s/k8s.go +++ b/tools/goctl/k8s/kube.go @@ -18,7 +18,8 @@ var errUnknownServiceType = errors.New("unknown service type") type ( ServiceType string - K8sRequest struct { + + KubeRequest struct { Env string ServiceName string ServiceType ServiceType @@ -37,7 +38,7 @@ type ( } ) -func Gen(req K8sRequest) (string, error) { +func Gen(req KubeRequest) (string, error) { switch req.ServiceType { case ServiceTypeApi, ServiceTypeRpc: return genApiRpc(req) @@ -48,7 +49,7 @@ func Gen(req K8sRequest) (string, error) { } } -func genApiRpc(req K8sRequest) (string, error) { +func genApiRpc(req KubeRequest) (string, error) { t, err := template.New("api_rpc").Parse(apiRpcTmeplate) if err != nil { return "", err @@ -77,7 +78,7 @@ func genApiRpc(req K8sRequest) (string, error) { return buffer.String(), nil } -func genJob(req K8sRequest) (string, error) { +func genJob(req KubeRequest) (string, error) { t, err := template.New("job").Parse(jobTmeplate) if err != nil { return "", err