refactor: move mock time to utils

This commit is contained in:
yusing 2025-04-25 08:21:27 +08:00
parent 5afa93a8f1
commit 59490dcac0
6 changed files with 21 additions and 17 deletions

View file

@ -10,6 +10,7 @@ import (
. "github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
"github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/utils"
expect "github.com/yusing/go-proxy/internal/utils/testing"
)
@ -56,7 +57,7 @@ func fmtLog(cfg *Config) (ts string, line string) {
t := time.Now()
logger := NewMockAccessLogger(testTask, cfg)
MockTimeNow(t)
utils.MockTimeNow(t)
buf = logger.AppendLog(buf, req, resp)
return t.Format(LogTimeFormat), string(buf)
}

View file

@ -8,6 +8,7 @@ import (
"strconv"
"github.com/rs/zerolog"
"github.com/yusing/go-proxy/internal/utils"
)
type (
@ -64,7 +65,7 @@ func (f *CommonFormatter) AppendLog(line []byte, req *http.Request, res *http.Re
line = append(line, clientIP(req)...)
line = append(line, " - - ["...)
line = TimeNow().AppendFormat(line, LogTimeFormat)
line = utils.TimeNow().AppendFormat(line, LogTimeFormat)
line = append(line, `] "`...)
line = append(line, req.Method...)
@ -126,7 +127,7 @@ func (f *JSONFormatter) AppendLog(line []byte, req *http.Request, res *http.Resp
writer := bytes.NewBuffer(line)
logger := zerolog.New(writer).With().Logger()
event := logger.Info().
Str("time", TimeNow().Format(LogTimeFormat)).
Str("time", utils.TimeNow().Format(LogTimeFormat)).
Str("ip", clientIP(req)).
Str("method", req.Method).
Str("scheme", scheme(req)).

View file

@ -6,6 +6,7 @@ import (
"time"
"github.com/rs/zerolog"
"github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/utils/strutils"
"github.com/yusing/go-proxy/internal/utils/synk"
)
@ -63,13 +64,13 @@ func rotateLogFile(file supportRotate, config *Retention) (result *RotateResult,
}
var shouldStop func() bool
t := TimeNow()
t := utils.TimeNow()
if config.Last > 0 {
shouldStop = func() bool { return result.NumLinesKeep-result.NumLinesInvalid == int(config.Last) }
// not needed to parse time for last N lines
} else if config.Days > 0 {
cutoff := TimeNow().AddDate(0, 0, -int(config.Days)+1)
cutoff := utils.TimeNow().AddDate(0, 0, -int(config.Days)+1)
shouldStop = func() bool { return t.Before(cutoff) }
} else {
return nil, nil // should not happen

View file

@ -8,6 +8,7 @@ import (
. "github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
"github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/utils/strutils"
expect "github.com/yusing/go-proxy/internal/utils/testing"
)
@ -55,7 +56,7 @@ func TestRotateKeepLast(t *testing.T) {
for _, format := range AvailableFormats {
t.Run(string(format)+" keep last", func(t *testing.T) {
file := NewMockFile()
MockTimeNow(testTime)
utils.MockTimeNow(testTime)
logger := NewAccessLoggerWithIO(task.RootTask("test", false), file, &Config{
Format: format,
})
@ -90,7 +91,7 @@ func TestRotateKeepLast(t *testing.T) {
expect.Nil(t, logger.Config().Retention)
nLines := 10
for i := range nLines {
MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
logger.Log(req, resp)
}
logger.Flush()
@ -102,7 +103,7 @@ func TestRotateKeepLast(t *testing.T) {
expect.Equal(t, retention.KeepSize, 0)
logger.Config().Retention = retention
MockTimeNow(testTime)
utils.MockTimeNow(testTime)
result, err := logger.Rotate()
expect.NoError(t, err)
expect.Equal(t, file.NumLines(), int(retention.Days))
@ -135,7 +136,7 @@ func TestRotateKeepFileSize(t *testing.T) {
expect.Nil(t, logger.Config().Retention)
nLines := 10
for i := range nLines {
MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
logger.Log(req, resp)
}
logger.Flush()
@ -147,7 +148,7 @@ func TestRotateKeepFileSize(t *testing.T) {
expect.Equal(t, retention.Last, 0)
logger.Config().Retention = retention
MockTimeNow(testTime)
utils.MockTimeNow(testTime)
result, err := logger.Rotate()
expect.NoError(t, err)
@ -165,7 +166,7 @@ func TestRotateKeepFileSize(t *testing.T) {
expect.Nil(t, logger.Config().Retention)
nLines := 100
for i := range nLines {
MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
logger.Log(req, resp)
}
logger.Flush()
@ -177,7 +178,7 @@ func TestRotateKeepFileSize(t *testing.T) {
expect.Equal(t, retention.Last, 0)
logger.Config().Retention = retention
MockTimeNow(testTime)
utils.MockTimeNow(testTime)
result, err := logger.Rotate()
expect.NoError(t, err)
expect.Equal(t, result.NumBytesKeep, int64(retention.KeepSize))
@ -197,7 +198,7 @@ func TestRotateSkipInvalidTime(t *testing.T) {
expect.Nil(t, logger.Config().Retention)
nLines := 10
for i := range nLines {
MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1))
logger.Log(req, resp)
logger.Flush()
@ -236,7 +237,7 @@ func BenchmarkRotate(b *testing.B) {
Retention: retention,
})
for i := range 100 {
MockTimeNow(testTime.AddDate(0, 0, -100+i+1))
utils.MockTimeNow(testTime.AddDate(0, 0, -100+i+1))
logger.Log(req, resp)
}
logger.Flush()
@ -267,7 +268,7 @@ func BenchmarkRotateWithInvalidTime(b *testing.B) {
Retention: retention,
})
for i := range 10000 {
MockTimeNow(testTime.AddDate(0, 0, -10000+i+1))
utils.MockTimeNow(testTime.AddDate(0, 0, -10000+i+1))
logger.Log(req, resp)
if i%10 == 0 {
_, _ = file.Write([]byte("invalid time\n"))

View file

@ -1,4 +1,4 @@
package accesslog
package utils
import (
"time"

View file

@ -1,4 +1,4 @@
package accesslog
package utils
import (
"testing"