mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-21 04:52:35 +02:00

* feat: idle sleep for proxmox LXCs * refactor: replace deprecated docker api types * chore(api): remove debug task list endpoint * refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon * refactor: introduce Pool interface, move agent_pool to agent module * refactor: simplify api code * feat: introduce debug api * refactor: remove net.URL and net.CIDR types, improved unmarshal handling * chore: update Makefile for debug build tag, update README * chore: add gperr.Unwrap method * feat: relative time and duration formatting * chore: add ROOT_DIR environment variable, refactor * migration: move homepage override and icon cache to $BASE_DIR/data, add migration code * fix: nil dereference on marshalling service health * fix: wait for route deletion * chore: enhance tasks debuggability * feat: stdout access logger and MultiWriter * fix(agent): remove agent properly on verify error * fix(metrics): disk exclusion logic and added corresponding tests * chore: update schema and prettify, fix package.json and Makefile * fix: I/O buffer not being shrunk before putting back to pool * feat: enhanced error handling module * chore: deps upgrade * feat: better value formatting and handling --------- Co-authored-by: yusing <yusing@6uo.me>
94 lines
1.8 KiB
Go
94 lines
1.8 KiB
Go
package accesslog
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
|
|
. "github.com/yusing/go-proxy/internal/utils/testing"
|
|
|
|
"github.com/yusing/go-proxy/internal/task"
|
|
)
|
|
|
|
func TestConcurrentFileLoggersShareSameAccessLogIO(t *testing.T) {
|
|
var wg sync.WaitGroup
|
|
|
|
cfg := DefaultConfig()
|
|
cfg.Path = "test.log"
|
|
|
|
loggerCount := 10
|
|
accessLogIOs := make([]AccessLogIO, loggerCount)
|
|
|
|
// make test log file
|
|
file, err := os.Create(cfg.Path)
|
|
ExpectNoError(t, err)
|
|
file.Close()
|
|
t.Cleanup(func() {
|
|
ExpectNoError(t, os.Remove(cfg.Path))
|
|
})
|
|
|
|
for i := range loggerCount {
|
|
wg.Add(1)
|
|
go func(index int) {
|
|
defer wg.Done()
|
|
file, err := newFileIO(cfg.Path)
|
|
ExpectNoError(t, err)
|
|
accessLogIOs[index] = file
|
|
}(i)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
firstIO := accessLogIOs[0]
|
|
for _, io := range accessLogIOs {
|
|
ExpectEqual(t, io, firstIO)
|
|
}
|
|
}
|
|
|
|
func TestConcurrentAccessLoggerLogAndFlush(t *testing.T) {
|
|
var file MockFile
|
|
|
|
cfg := DefaultConfig()
|
|
cfg.BufferSize = 1024
|
|
parent := task.RootTask("test", false)
|
|
|
|
loggerCount := 5
|
|
logCountPerLogger := 10
|
|
loggers := make([]*AccessLogger, loggerCount)
|
|
|
|
for i := range loggerCount {
|
|
loggers[i] = NewAccessLoggerWithIO(parent, &file, cfg)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil)
|
|
resp := &http.Response{StatusCode: http.StatusOK}
|
|
|
|
for _, logger := range loggers {
|
|
wg.Add(1)
|
|
go func(l *AccessLogger) {
|
|
defer wg.Done()
|
|
parallelLog(l, req, resp, logCountPerLogger)
|
|
l.Flush()
|
|
}(logger)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
expected := loggerCount * logCountPerLogger
|
|
actual := file.LineCount()
|
|
ExpectEqual(t, actual, expected)
|
|
}
|
|
|
|
func parallelLog(logger *AccessLogger, req *http.Request, resp *http.Response, n int) {
|
|
var wg sync.WaitGroup
|
|
wg.Add(n)
|
|
for range n {
|
|
go func() {
|
|
defer wg.Done()
|
|
logger.Log(req, resp)
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|