You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stat/metrics_test.go

77 lines
1.5 KiB
Go

4 years ago
package stat
import (
"errors"
4 years ago
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx/logtest"
4 years ago
)
func TestMetrics(t *testing.T) {
DisableLog()
defer logEnabled.Set(true)
4 years ago
counts := []int{1, 5, 10, 100, 1000, 1000}
for _, count := range counts {
m := NewMetrics("foo")
m.SetName("bar")
for i := 0; i < count; i++ {
m.Add(Task{
Duration: time.Millisecond * time.Duration(i),
Description: strconv.Itoa(i),
})
}
m.AddDrop()
var writer mockedWriter
SetReportWriter(&writer)
m.executor.Flush()
assert.Equal(t, "bar", writer.report.Name)
}
}
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())
}
4 years ago
type mockedWriter struct {
report *StatReport
}
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")
}