From 914bcdcf2b6c72d1f69f73a8d1424317aa8c9975 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 23 Feb 2024 23:00:35 +0800 Subject: [PATCH] chore: add tests (#3931) --- core/stat/metrics_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core/stat/metrics_test.go b/core/stat/metrics_test.go index 7172994f..30f8d316 100644 --- a/core/stat/metrics_test.go +++ b/core/stat/metrics_test.go @@ -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") +}