chore: add tests (#3931)

master^2
Kevin Wan 8 months ago committed by GitHub
parent e38cb0118d
commit 914bcdcf2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,11 +1,13 @@
package stat
import (
"errors"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx/logtest"
)
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 {
report *StatReport
}
@ -38,3 +68,9 @@ func (m *mockedWriter) Write(report *StatReport) error {
m.report = report
return nil
}
type badWriter struct{}
func (b *badWriter) Write(report *StatReport) error {
return errors.New("bad")
}

Loading…
Cancel
Save