Skip to content

Commit

Permalink
make tests faster
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Oct 21, 2020
1 parent efffb40 commit 1fd2ef9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 71 deletions.
135 changes: 68 additions & 67 deletions core/stores/sqlc/cachedsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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{
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions tools/goctl/k8s/k8s.go → tools/goctl/k8s/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var errUnknownServiceType = errors.New("unknown service type")

type (
ServiceType string
K8sRequest struct {

KubeRequest struct {
Env string
ServiceName string
ServiceType ServiceType
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1fd2ef9

Please sign in to comment.