From ec256be74b5428d4161da98ac5d21fff2c5bbc8f Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 24 Mar 2024 23:47:27 +0800 Subject: [PATCH] :white_check_mark: test: add a mock clock and test case for issues #138 --- rotatefile/issues_test.go | 51 +++++++++++++++++++++++++++++++++++++++ rotatefile/writer_test.go | 34 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 rotatefile/issues_test.go diff --git a/rotatefile/issues_test.go b/rotatefile/issues_test.go new file mode 100644 index 0000000..0d953ea --- /dev/null +++ b/rotatefile/issues_test.go @@ -0,0 +1,51 @@ +package rotatefile_test + +import ( + "testing" + "time" + + "github.com/gookit/goutil/fsutil" + "github.com/gookit/goutil/mathutil" + "github.com/gookit/goutil/testutil/assert" + "github.com/gookit/slog/rotatefile" +) + +// https://github.com/gookit/slog/issues/138 +// 日志按everyday自动滚动,文件名的日期对应的是前一天的日志 #138 +func TestIssues_138(t *testing.T) { + logfile := "testdata/rotate_day.log" + + mt := newMockTime("2023-11-16 23:59:58") + w, err := rotatefile.NewWriterWith(rotatefile.WithDebugMode, func(c *rotatefile.Config) { + c.TimeClock = mt + // c.MaxSize = 128 + c.Filepath = logfile + c.RotateTime = rotatefile.EveryDay + }) + + assert.NoErr(t, err) + defer w.MustClose() + + for i := 0; i < 5; i++ { + dt := mt.Datetime() + _, err = w.WriteString(dt + " [INFO] this is a log message, idx=" + mathutil.String(i) + "\n") + assert.NoErr(t, err) + mt.Add(time.Second) // add one second + } + + // Out: rotate_day.log, rotate_day.log.20231116 + files := fsutil.Glob(logfile + "*") + assert.Len(t, files, 2) + + // check contents + assert.True(t, fsutil.IsFile(logfile)) + s := fsutil.ReadString(logfile) + assert.StrContains(t, s, "2023-11-17 00:00:01 [INFO]") + assert.StrNoContains(t, s, "2023-11-16") + + oldFile := logfile + ".20231116" + assert.True(t, fsutil.IsFile(oldFile)) + s = fsutil.ReadString(oldFile) + assert.StrContains(t, s, "2023-11-16 23:59:59 [INFO]") + assert.StrNoContains(t, s, "2023-11-17") +} diff --git a/rotatefile/writer_test.go b/rotatefile/writer_test.go index 184030f..d14bfb8 100644 --- a/rotatefile/writer_test.go +++ b/rotatefile/writer_test.go @@ -5,10 +5,12 @@ import ( "testing" "time" + "github.com/gookit/goutil" "github.com/gookit/goutil/dump" "github.com/gookit/goutil/fsutil" "github.com/gookit/goutil/mathutil" "github.com/gookit/goutil/testutil/assert" + "github.com/gookit/goutil/timex" "github.com/gookit/slog/rotatefile" ) @@ -130,3 +132,35 @@ func TestWriter_Clean(t *testing.T) { assert.NoErr(t, err) }) } + +// TODO set github.com/benbjohnson/clock for mock clock +type constantClock time.Time + +func (c constantClock) Now() time.Time { return time.Time(c) } +func (c constantClock) NewTicker(d time.Duration) *time.Ticker { + return &time.Ticker{} +} + +type mockTime struct { + tt time.Time +} + +// newMockTime create a mock time instance from datetime string. +func newMockTime(datetime string) *mockTime { + nt := goutil.Must(timex.FromString(datetime)) + return &mockTime{tt: nt.Time} +} + +func (mt *mockTime) Now() time.Time { + return mt.tt +} + +// Add progresses time by the given duration. +func (mt *mockTime) Add(d time.Duration) { + mt.tt = mt.tt.Add(d) +} + +// Datetime returns the current time in the format "2006-01-02 15:04:05". +func (mt *mockTime) Datetime() string { + return mt.tt.Format("2006-01-02 15:04:05") +}