chore: add tests (#3931)
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package stat
|
package stat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/core/logx/logtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMetrics(t *testing.T) {
|
func TestMetrics(t *testing.T) {
|
||||||
@@ -30,6 +32,34 @@ func TestMetrics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTopDurationWithEmpty(t *testing.T) {
|
||||||
|
assert.Equal(t, float32(0), getTopDuration(nil))
|
||||||
|
assert.Equal(t, float32(0), getTopDuration([]Task{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLogAndReport(t *testing.T) {
|
||||||
|
buf := logtest.NewCollector(t)
|
||||||
|
old := logEnabled.True()
|
||||||
|
logEnabled.Set(true)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
logEnabled.Set(old)
|
||||||
|
})
|
||||||
|
|
||||||
|
log(&StatReport{})
|
||||||
|
assert.NotEmpty(t, buf.String())
|
||||||
|
|
||||||
|
writerLock.Lock()
|
||||||
|
writer := reportWriter
|
||||||
|
writerLock.Unlock()
|
||||||
|
buf = logtest.NewCollector(t)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
SetReportWriter(writer)
|
||||||
|
})
|
||||||
|
SetReportWriter(&badWriter{})
|
||||||
|
writeReport(&StatReport{})
|
||||||
|
assert.NotEmpty(t, buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
type mockedWriter struct {
|
type mockedWriter struct {
|
||||||
report *StatReport
|
report *StatReport
|
||||||
}
|
}
|
||||||
@@ -38,3 +68,9 @@ func (m *mockedWriter) Write(report *StatReport) error {
|
|||||||
m.report = report
|
m.report = report
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type badWriter struct{}
|
||||||
|
|
||||||
|
func (b *badWriter) Write(report *StatReport) error {
|
||||||
|
return errors.New("bad")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user