fixed some tests

This commit is contained in:
yusing 2025-01-03 16:31:49 +08:00
parent c1db404c0d
commit 3ecc0f95bf
5 changed files with 27 additions and 16 deletions

View file

@ -39,6 +39,7 @@ type (
Formatter interface { Formatter interface {
// Format writes a log line to line without a trailing newline // Format writes a log line to line without a trailing newline
Format(line *bytes.Buffer, req *http.Request, res *http.Response) Format(line *bytes.Buffer, req *http.Request, res *http.Response)
SetGetTimeNow(getTimeNow func() time.Time)
} }
) )
@ -54,14 +55,14 @@ func NewAccessLogger(parent task.Parent, io AccessLogIO, cfg *Config) *AccessLog
cfg.BufferSize = DefaultBufferSize cfg.BufferSize = DefaultBufferSize
} }
fmt := &CommonFormatter{cfg: &l.cfg.Fields, GetTimeNow: time.Now} fmt := CommonFormatter{cfg: &l.cfg.Fields, GetTimeNow: time.Now}
switch l.cfg.Format { switch l.cfg.Format {
case FormatCommon: case FormatCommon:
l.Formatter = fmt l.Formatter = &fmt
case FormatCombined: case FormatCombined:
l.Formatter = (*CombinedFormatter)(fmt) l.Formatter = &CombinedFormatter{fmt}
case FormatJSON: case FormatJSON:
l.Formatter = (*JSONFormatter)(fmt) l.Formatter = &JSONFormatter{fmt}
default: // should not happen, validation has done by validate tags default: // should not happen, validation has done by validate tags
panic("invalid access log format") panic("invalid access log format")
} }

View file

@ -11,6 +11,7 @@ import (
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
. "github.com/yusing/go-proxy/internal/net/http/accesslog" . "github.com/yusing/go-proxy/internal/net/http/accesslog"
"github.com/yusing/go-proxy/internal/task"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -28,6 +29,7 @@ const (
) )
var ( var (
testTask = task.RootTask("test", false)
testURL = E.Must(url.Parse("http://" + host + uri)) testURL = E.Must(url.Parse("http://" + host + uri))
req = &http.Request{ req = &http.Request{
RemoteAddr: remote, RemoteAddr: remote,
@ -55,10 +57,10 @@ func fmtLog(cfg *Config) (ts string, line string) {
var buf bytes.Buffer var buf bytes.Buffer
t := time.Now() t := time.Now()
logger := NewAccessLogger(nil, nil, cfg) logger := NewAccessLogger(testTask, nil, cfg)
logger.Formatter.(*CommonFormatter).GetTimeNow = func() time.Time { logger.Formatter.SetGetTimeNow(func() time.Time {
return t return t
} })
logger.Format(&buf, req, resp) logger.Format(&buf, req, resp)
return t.Format(LogTimeFormat), buf.String() return t.Format(LogTimeFormat), buf.String()
} }

View file

@ -15,8 +15,8 @@ type (
cfg *Fields cfg *Fields
GetTimeNow func() time.Time // for testing purposes only GetTimeNow func() time.Time // for testing purposes only
} }
CombinedFormatter CommonFormatter CombinedFormatter struct{ CommonFormatter }
JSONFormatter CommonFormatter JSONFormatter struct{ CommonFormatter }
JSONLogEntry struct { JSONLogEntry struct {
Time string `json:"time"` Time string `json:"time"`
@ -63,6 +63,11 @@ func clientIP(req *http.Request) string {
return req.RemoteAddr return req.RemoteAddr
} }
// debug only.
func (f *CommonFormatter) SetGetTimeNow(getTimeNow func() time.Time) {
f.GetTimeNow = getTimeNow
}
func (f *CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) { func (f *CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
query := f.cfg.Query.ProcessQuery(req.URL.Query()) query := f.cfg.Query.ProcessQuery(req.URL.Query())
@ -88,7 +93,7 @@ func (f *CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *htt
} }
func (f *CombinedFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) { func (f *CombinedFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
(*CommonFormatter)(f).Format(line, req, res) f.CommonFormatter.Format(line, req, res)
line.WriteString(" \"") line.WriteString(" \"")
line.WriteString(req.Referer()) line.WriteString(req.Referer())
line.WriteString("\" \"") line.WriteString("\" \"")

View file

@ -70,6 +70,7 @@ func TestRetentionCommonFormat(t *testing.T) {
} }
logger.Flush(true) logger.Flush(true)
// FIXME: keep days does not work
t.Run("keep days", func(t *testing.T) { t.Run("keep days", func(t *testing.T) {
logger.Config().Retention = strutils.MustParse[*Retention]("3 days") logger.Config().Retention = strutils.MustParse[*Retention]("3 days")
ExpectEqual(t, logger.Config().Retention.Days, 3) ExpectEqual(t, logger.Config().Retention.Days, 3)

View file

@ -6,8 +6,8 @@ import (
"time" "time"
) )
func TestRefCounter_AddSub(t *testing.T) { func TestRefCounterAddSub(t *testing.T) {
rc := NewRefCounter() rc := NewRefCounter() // Count starts at 1
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
@ -20,6 +20,7 @@ func TestRefCounter_AddSub(t *testing.T) {
go func() { go func() {
defer wg.Done() defer wg.Done()
rc.Sub() rc.Sub()
rc.Sub()
}() }()
wg.Wait() wg.Wait()
@ -32,7 +33,7 @@ func TestRefCounter_AddSub(t *testing.T) {
} }
} }
func TestRefCounter_MultipleAddSub(t *testing.T) { func TestRefCounterMultipleAddSub(t *testing.T) {
rc := NewRefCounter() rc := NewRefCounter()
var wg sync.WaitGroup var wg sync.WaitGroup
@ -51,6 +52,7 @@ func TestRefCounter_MultipleAddSub(t *testing.T) {
go func() { go func() {
defer wg.Done() defer wg.Done()
rc.Sub() rc.Sub()
rc.Sub()
}() }()
} }
@ -64,7 +66,7 @@ func TestRefCounter_MultipleAddSub(t *testing.T) {
} }
} }
func TestRefCounter_ZeroInitially(t *testing.T) { func TestRefCounterOneInitially(t *testing.T) {
rc := NewRefCounter() rc := NewRefCounter()
rc.Sub() // Bring count to zero rc.Sub() // Bring count to zero