Skip to content

Commit

Permalink
test: add codecov (#1861)
Browse files Browse the repository at this point in the history
* test: add codecov

* test: add codecov
  • Loading branch information
kevwan committed May 3, 2022
1 parent bab72b7 commit ec1de4f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
40 changes: 40 additions & 0 deletions core/stores/mon/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package mon
import (
"context"
"errors"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/breaker"
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions core/stores/mon/util_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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")
}
42 changes: 42 additions & 0 deletions core/stores/mongo/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package mongo

import (
"errors"
"strings"
"testing"
"time"

"github.com/globalsign/mgo"
"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions zrpc/internal/serverinterceptors/statinterceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ec1de4f

Please sign in to comment.