mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 17:58:45 +02:00
refactor: move mock time to utils
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
. "github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
. "github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
expect "github.com/yusing/go-proxy/internal/utils/testing"
|
expect "github.com/yusing/go-proxy/internal/utils/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ func fmtLog(cfg *Config) (ts string, line string) {
|
|||||||
|
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
logger := NewMockAccessLogger(testTask, cfg)
|
logger := NewMockAccessLogger(testTask, cfg)
|
||||||
MockTimeNow(t)
|
utils.MockTimeNow(t)
|
||||||
buf = logger.AppendLog(buf, req, resp)
|
buf = logger.AppendLog(buf, req, resp)
|
||||||
return t.Format(LogTimeFormat), string(buf)
|
return t.Format(LogTimeFormat), string(buf)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
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, clientIP(req)...)
|
||||||
line = append(line, " - - ["...)
|
line = append(line, " - - ["...)
|
||||||
|
|
||||||
line = TimeNow().AppendFormat(line, LogTimeFormat)
|
line = utils.TimeNow().AppendFormat(line, LogTimeFormat)
|
||||||
line = append(line, `] "`...)
|
line = append(line, `] "`...)
|
||||||
|
|
||||||
line = append(line, req.Method...)
|
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)
|
writer := bytes.NewBuffer(line)
|
||||||
logger := zerolog.New(writer).With().Logger()
|
logger := zerolog.New(writer).With().Logger()
|
||||||
event := logger.Info().
|
event := logger.Info().
|
||||||
Str("time", TimeNow().Format(LogTimeFormat)).
|
Str("time", utils.TimeNow().Format(LogTimeFormat)).
|
||||||
Str("ip", clientIP(req)).
|
Str("ip", clientIP(req)).
|
||||||
Str("method", req.Method).
|
Str("method", req.Method).
|
||||||
Str("scheme", scheme(req)).
|
Str("scheme", scheme(req)).
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"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/strutils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/synk"
|
"github.com/yusing/go-proxy/internal/utils/synk"
|
||||||
)
|
)
|
||||||
@@ -63,13 +64,13 @@ func rotateLogFile(file supportRotate, config *Retention) (result *RotateResult,
|
|||||||
}
|
}
|
||||||
|
|
||||||
var shouldStop func() bool
|
var shouldStop func() bool
|
||||||
t := TimeNow()
|
t := utils.TimeNow()
|
||||||
|
|
||||||
if config.Last > 0 {
|
if config.Last > 0 {
|
||||||
shouldStop = func() bool { return result.NumLinesKeep-result.NumLinesInvalid == int(config.Last) }
|
shouldStop = func() bool { return result.NumLinesKeep-result.NumLinesInvalid == int(config.Last) }
|
||||||
// not needed to parse time for last N lines
|
// not needed to parse time for last N lines
|
||||||
} else if config.Days > 0 {
|
} 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) }
|
shouldStop = func() bool { return t.Before(cutoff) }
|
||||||
} else {
|
} else {
|
||||||
return nil, nil // should not happen
|
return nil, nil // should not happen
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
. "github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
. "github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
expect "github.com/yusing/go-proxy/internal/utils/testing"
|
expect "github.com/yusing/go-proxy/internal/utils/testing"
|
||||||
)
|
)
|
||||||
@@ -55,7 +56,7 @@ func TestRotateKeepLast(t *testing.T) {
|
|||||||
for _, format := range AvailableFormats {
|
for _, format := range AvailableFormats {
|
||||||
t.Run(string(format)+" keep last", func(t *testing.T) {
|
t.Run(string(format)+" keep last", func(t *testing.T) {
|
||||||
file := NewMockFile()
|
file := NewMockFile()
|
||||||
MockTimeNow(testTime)
|
utils.MockTimeNow(testTime)
|
||||||
logger := NewAccessLoggerWithIO(task.RootTask("test", false), file, &Config{
|
logger := NewAccessLoggerWithIO(task.RootTask("test", false), file, &Config{
|
||||||
Format: format,
|
Format: format,
|
||||||
})
|
})
|
||||||
@@ -90,7 +91,7 @@ func TestRotateKeepLast(t *testing.T) {
|
|||||||
expect.Nil(t, logger.Config().Retention)
|
expect.Nil(t, logger.Config().Retention)
|
||||||
nLines := 10
|
nLines := 10
|
||||||
for i := range nLines {
|
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.Log(req, resp)
|
||||||
}
|
}
|
||||||
logger.Flush()
|
logger.Flush()
|
||||||
@@ -102,7 +103,7 @@ func TestRotateKeepLast(t *testing.T) {
|
|||||||
expect.Equal(t, retention.KeepSize, 0)
|
expect.Equal(t, retention.KeepSize, 0)
|
||||||
logger.Config().Retention = retention
|
logger.Config().Retention = retention
|
||||||
|
|
||||||
MockTimeNow(testTime)
|
utils.MockTimeNow(testTime)
|
||||||
result, err := logger.Rotate()
|
result, err := logger.Rotate()
|
||||||
expect.NoError(t, err)
|
expect.NoError(t, err)
|
||||||
expect.Equal(t, file.NumLines(), int(retention.Days))
|
expect.Equal(t, file.NumLines(), int(retention.Days))
|
||||||
@@ -135,7 +136,7 @@ func TestRotateKeepFileSize(t *testing.T) {
|
|||||||
expect.Nil(t, logger.Config().Retention)
|
expect.Nil(t, logger.Config().Retention)
|
||||||
nLines := 10
|
nLines := 10
|
||||||
for i := range nLines {
|
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.Log(req, resp)
|
||||||
}
|
}
|
||||||
logger.Flush()
|
logger.Flush()
|
||||||
@@ -147,7 +148,7 @@ func TestRotateKeepFileSize(t *testing.T) {
|
|||||||
expect.Equal(t, retention.Last, 0)
|
expect.Equal(t, retention.Last, 0)
|
||||||
logger.Config().Retention = retention
|
logger.Config().Retention = retention
|
||||||
|
|
||||||
MockTimeNow(testTime)
|
utils.MockTimeNow(testTime)
|
||||||
result, err := logger.Rotate()
|
result, err := logger.Rotate()
|
||||||
expect.NoError(t, err)
|
expect.NoError(t, err)
|
||||||
|
|
||||||
@@ -165,7 +166,7 @@ func TestRotateKeepFileSize(t *testing.T) {
|
|||||||
expect.Nil(t, logger.Config().Retention)
|
expect.Nil(t, logger.Config().Retention)
|
||||||
nLines := 100
|
nLines := 100
|
||||||
for i := range nLines {
|
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.Log(req, resp)
|
||||||
}
|
}
|
||||||
logger.Flush()
|
logger.Flush()
|
||||||
@@ -177,7 +178,7 @@ func TestRotateKeepFileSize(t *testing.T) {
|
|||||||
expect.Equal(t, retention.Last, 0)
|
expect.Equal(t, retention.Last, 0)
|
||||||
logger.Config().Retention = retention
|
logger.Config().Retention = retention
|
||||||
|
|
||||||
MockTimeNow(testTime)
|
utils.MockTimeNow(testTime)
|
||||||
result, err := logger.Rotate()
|
result, err := logger.Rotate()
|
||||||
expect.NoError(t, err)
|
expect.NoError(t, err)
|
||||||
expect.Equal(t, result.NumBytesKeep, int64(retention.KeepSize))
|
expect.Equal(t, result.NumBytesKeep, int64(retention.KeepSize))
|
||||||
@@ -197,7 +198,7 @@ func TestRotateSkipInvalidTime(t *testing.T) {
|
|||||||
expect.Nil(t, logger.Config().Retention)
|
expect.Nil(t, logger.Config().Retention)
|
||||||
nLines := 10
|
nLines := 10
|
||||||
for i := range nLines {
|
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.Log(req, resp)
|
||||||
logger.Flush()
|
logger.Flush()
|
||||||
|
|
||||||
@@ -236,7 +237,7 @@ func BenchmarkRotate(b *testing.B) {
|
|||||||
Retention: retention,
|
Retention: retention,
|
||||||
})
|
})
|
||||||
for i := range 100 {
|
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.Log(req, resp)
|
||||||
}
|
}
|
||||||
logger.Flush()
|
logger.Flush()
|
||||||
@@ -267,7 +268,7 @@ func BenchmarkRotateWithInvalidTime(b *testing.B) {
|
|||||||
Retention: retention,
|
Retention: retention,
|
||||||
})
|
})
|
||||||
for i := range 10000 {
|
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)
|
logger.Log(req, resp)
|
||||||
if i%10 == 0 {
|
if i%10 == 0 {
|
||||||
_, _ = file.Write([]byte("invalid time\n"))
|
_, _ = file.Write([]byte("invalid time\n"))
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package accesslog
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package accesslog
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
Reference in New Issue
Block a user