Skip to content

Commit

Permalink
✅ test: add a mock clock and test case for issues #138
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 24, 2024
1 parent be892f7 commit ec256be
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
51 changes: 51 additions & 0 deletions rotatefile/issues_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
34 changes: 34 additions & 0 deletions rotatefile/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}

0 comments on commit ec256be

Please sign in to comment.