From ec1de4f48dd828cd8fbc2c16e8a0bc7582bd2500 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 3 May 2022 21:22:15 +0800 Subject: [PATCH] test: add codecov (#1861) * test: add codecov * test: add codecov --- core/stores/mon/collection_test.go | 40 ++++++++++++++++++ core/stores/mon/util_test.go | 27 ++++++++++++ core/stores/mongo/collection_test.go | 42 +++++++++++++++++++ .../statinterceptor_test.go | 6 +++ 4 files changed, 115 insertions(+) diff --git a/core/stores/mon/collection_test.go b/core/stores/mon/collection_test.go index 2a3f62ae..3b39b373 100644 --- a/core/stores/mon/collection_test.go +++ b/core/stores/mon/collection_test.go @@ -3,7 +3,9 @@ package mon import ( "context" "errors" + "strings" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/zeromicro/go-zero/core/breaker" @@ -567,6 +569,44 @@ func TestCollection_UpdateMany(t *testing.T) { }) } +func Test_DecoratedCollectionLogDuration(t *testing.T) { + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + defer mt.Close() + c := decoratedCollection{ + Collection: mt.Coll, + brk: breaker.NewBreaker(), + } + + var buf strings.Builder + w := logx.NewWriter(&buf) + o := logx.Reset() + logx.SetWriter(w) + + defer func() { + logx.Reset() + logx.SetWriter(o) + }() + + buf.Reset() + c.logDuration("foo", time.Millisecond, nil, "bar") + assert.Contains(t, buf.String(), "foo") + assert.Contains(t, buf.String(), "bar") + + buf.Reset() + c.logDuration("foo", time.Millisecond, errors.New("bar"), make(chan int)) + assert.Contains(t, buf.String(), "bar") + + buf.Reset() + c.logDuration("foo", slowThreshold.Load()+time.Millisecond, errors.New("bar")) + assert.Contains(t, buf.String(), "foo") + assert.Contains(t, buf.String(), "slowcall") + + buf.Reset() + c.logDuration("foo", slowThreshold.Load()+time.Millisecond, nil) + assert.Contains(t, buf.String(), "foo") + assert.Contains(t, buf.String(), "slowcall") +} + type mockPromise struct { accepted bool reason string diff --git a/core/stores/mon/util_test.go b/core/stores/mon/util_test.go index 559ad919..c23a7933 100644 --- a/core/stores/mon/util_test.go +++ b/core/stores/mon/util_test.go @@ -1,9 +1,13 @@ package mon import ( + "errors" + "strings" "testing" + "time" "github.com/stretchr/testify/assert" + "github.com/zeromicro/go-zero/core/logx" ) func TestFormatAddrs(t *testing.T) { @@ -33,3 +37,26 @@ func TestFormatAddrs(t *testing.T) { assert.Equal(t, test.expect, FormatAddr(test.addrs)) } } + +func Test_logDuration(t *testing.T) { + var buf strings.Builder + w := logx.NewWriter(&buf) + o := logx.Reset() + logx.SetWriter(w) + + defer func() { + logx.Reset() + logx.SetWriter(o) + }() + + buf.Reset() + logDuration("foo", "bar", time.Millisecond, nil) + assert.Contains(t, buf.String(), "foo") + assert.Contains(t, buf.String(), "bar") + + buf.Reset() + logDuration("foo", "bar", time.Millisecond, errors.New("bar")) + assert.Contains(t, buf.String(), "foo") + assert.Contains(t, buf.String(), "bar") + assert.Contains(t, buf.String(), "fail") +} diff --git a/core/stores/mongo/collection_test.go b/core/stores/mongo/collection_test.go index 042a8382..4ae5f8c0 100644 --- a/core/stores/mongo/collection_test.go +++ b/core/stores/mongo/collection_test.go @@ -2,7 +2,9 @@ package mongo import ( "errors" + "strings" "testing" + "time" "github.com/globalsign/mgo" "github.com/golang/mock/gomock" @@ -266,6 +268,46 @@ func TestCollectionUpsert(t *testing.T) { assert.Equal(t, errDummy, err) } +func Test_logDuration(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + col := internal.NewMockMgoCollection(ctrl) + c := decoratedCollection{ + collection: col, + brk: breaker.NewBreaker(), + } + + var buf strings.Builder + w := logx.NewWriter(&buf) + o := logx.Reset() + logx.SetWriter(w) + + defer func() { + logx.Reset() + logx.SetWriter(o) + }() + + buf.Reset() + c.logDuration("foo", time.Millisecond, nil, "bar") + assert.Contains(t, buf.String(), "foo") + assert.Contains(t, buf.String(), "bar") + + buf.Reset() + c.logDuration("foo", time.Millisecond, errors.New("bar"), make(chan int)) + assert.Contains(t, buf.String(), "bar") + + buf.Reset() + c.logDuration("foo", slowThreshold.Load()+time.Millisecond, errors.New("bar")) + assert.Contains(t, buf.String(), "bar") + assert.Contains(t, buf.String(), "slowcall") + + buf.Reset() + c.logDuration("foo", slowThreshold.Load()+time.Millisecond, nil) + assert.Contains(t, buf.String(), "foo") + assert.Contains(t, buf.String(), "slowcall") +} + type mockPromise struct { accepted bool reason string diff --git a/zrpc/internal/serverinterceptors/statinterceptor_test.go b/zrpc/internal/serverinterceptors/statinterceptor_test.go index dd653f52..e3e3f424 100644 --- a/zrpc/internal/serverinterceptors/statinterceptor_test.go +++ b/zrpc/internal/serverinterceptors/statinterceptor_test.go @@ -75,6 +75,12 @@ func TestLogDuration(t *testing.T) { }), req: "foo", }, + { + name: "timeout", + ctx: context.Background(), + req: "foo", + duration: slowThreshold.Load() + time.Second, + }, } for _, test := range tests {