refactor(test): 统一测试文件错误处理风格
使用空白标识符忽略测试辅助函数中 Close、ReadFrom、Set 等返回值, 与主代码风格保持一致。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
96ed39e162
commit
fc71cf4835
@ -25,13 +25,13 @@ func captureStdout(t *testing.T) (func() string, func()) {
|
||||
os.Stdout = w
|
||||
|
||||
return func() string {
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
return buf.String()
|
||||
}, func() {
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
}
|
||||
}
|
||||
@ -47,13 +47,13 @@ func captureStderr(t *testing.T) (func() string, func()) {
|
||||
os.Stderr = w
|
||||
|
||||
return func() string {
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stderr = old
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
return buf.String()
|
||||
}, func() {
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stderr = old
|
||||
}
|
||||
}
|
||||
|
||||
26
internal/cache/cache_test.go
vendored
26
internal/cache/cache_test.go
vendored
@ -35,7 +35,7 @@ func TestFileCacheSetGet(t *testing.T) {
|
||||
func TestFileCacheDelete(t *testing.T) {
|
||||
fc := NewFileCache(10, 1024, 1*time.Hour)
|
||||
|
||||
fc.Set("/test.txt", []byte("data"), 4, time.Now())
|
||||
_ = fc.Set("/test.txt", []byte("data"), 4, time.Now())
|
||||
|
||||
fc.Delete("/test.txt")
|
||||
|
||||
@ -49,12 +49,12 @@ func TestFileCacheLRUEviction(t *testing.T) {
|
||||
// 最大 3 个条目
|
||||
fc := NewFileCache(3, 0, 1*time.Hour)
|
||||
|
||||
fc.Set("/a", []byte("a"), 1, time.Now())
|
||||
fc.Set("/b", []byte("b"), 1, time.Now())
|
||||
fc.Set("/c", []byte("c"), 1, time.Now())
|
||||
_ = fc.Set("/a", []byte("a"), 1, time.Now())
|
||||
_ = fc.Set("/b", []byte("b"), 1, time.Now())
|
||||
_ = fc.Set("/c", []byte("c"), 1, time.Now())
|
||||
|
||||
// 再添加一个,应该淘汰 /a
|
||||
fc.Set("/d", []byte("d"), 1, time.Now())
|
||||
_ = fc.Set("/d", []byte("d"), 1, time.Now())
|
||||
|
||||
_, ok := fc.Get("/a")
|
||||
if ok {
|
||||
@ -74,11 +74,11 @@ func TestFileCacheSizeEviction(t *testing.T) {
|
||||
// 最大 10 字节
|
||||
fc := NewFileCache(0, 10, 1*time.Hour)
|
||||
|
||||
fc.Set("/a", []byte("12345"), 5, time.Now())
|
||||
fc.Set("/b", []byte("12345"), 5, time.Now())
|
||||
_ = fc.Set("/a", []byte("12345"), 5, time.Now())
|
||||
_ = fc.Set("/b", []byte("12345"), 5, time.Now())
|
||||
|
||||
// 再添加 6 字节,应该淘汰一个
|
||||
fc.Set("/c", []byte("123456"), 6, time.Now())
|
||||
_ = fc.Set("/c", []byte("123456"), 6, time.Now())
|
||||
|
||||
stats := fc.Stats()
|
||||
if stats.Size > 10 {
|
||||
@ -89,7 +89,7 @@ func TestFileCacheSizeEviction(t *testing.T) {
|
||||
func TestFileCacheInactiveEviction(t *testing.T) {
|
||||
fc := NewFileCache(10, 1024, 100*time.Millisecond)
|
||||
|
||||
fc.Set("/test", []byte("data"), 4, time.Now())
|
||||
_ = fc.Set("/test", []byte("data"), 4, time.Now())
|
||||
|
||||
// 立即获取应该成功
|
||||
_, ok := fc.Get("/test")
|
||||
@ -110,8 +110,8 @@ func TestFileCacheInactiveEviction(t *testing.T) {
|
||||
func TestFileCacheClear(t *testing.T) {
|
||||
fc := NewFileCache(10, 1024, 1*time.Hour)
|
||||
|
||||
fc.Set("/a", []byte("a"), 1, time.Now())
|
||||
fc.Set("/b", []byte("b"), 1, time.Now())
|
||||
_ = fc.Set("/a", []byte("a"), 1, time.Now())
|
||||
_ = fc.Set("/b", []byte("b"), 1, time.Now())
|
||||
|
||||
fc.Clear()
|
||||
|
||||
@ -124,8 +124,8 @@ func TestFileCacheClear(t *testing.T) {
|
||||
func TestFileCacheStats(t *testing.T) {
|
||||
fc := NewFileCache(100, 1024, 1*time.Hour)
|
||||
|
||||
fc.Set("/a", []byte("12345"), 5, time.Now())
|
||||
fc.Set("/b", []byte("12345"), 5, time.Now())
|
||||
_ = fc.Set("/a", []byte("12345"), 5, time.Now())
|
||||
_ = fc.Set("/b", []byte("12345"), 5, time.Now())
|
||||
|
||||
stats := fc.Stats()
|
||||
if stats.Entries != 2 {
|
||||
|
||||
@ -14,7 +14,7 @@ func TestRouterGET(t *testing.T) {
|
||||
var called bool
|
||||
handler := func(ctx *fasthttp.RequestCtx) {
|
||||
called = true
|
||||
ctx.WriteString("GET response")
|
||||
_, _ = ctx.WriteString("GET response")
|
||||
}
|
||||
|
||||
r.GET("/test", handler)
|
||||
@ -41,7 +41,7 @@ func TestRouterPOST(t *testing.T) {
|
||||
var called bool
|
||||
handler := func(ctx *fasthttp.RequestCtx) {
|
||||
called = true
|
||||
ctx.WriteString("POST response")
|
||||
_, _ = ctx.WriteString("POST response")
|
||||
}
|
||||
|
||||
r.POST("/submit", handler)
|
||||
@ -69,12 +69,12 @@ func TestRouterMultipleMethods(t *testing.T) {
|
||||
|
||||
r.GET("/api", func(ctx *fasthttp.RequestCtx) {
|
||||
getCalled = true
|
||||
ctx.WriteString("GET api")
|
||||
_, _ = ctx.WriteString("GET api")
|
||||
})
|
||||
|
||||
r.POST("/api", func(ctx *fasthttp.RequestCtx) {
|
||||
postCalled = true
|
||||
ctx.WriteString("POST api")
|
||||
_, _ = ctx.WriteString("POST api")
|
||||
})
|
||||
|
||||
// 测试 GET 请求
|
||||
@ -131,7 +131,7 @@ func TestRouterMultipleRoutes(t *testing.T) {
|
||||
|
||||
for path, response := range routes {
|
||||
r.GET(path, func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.WriteString(response)
|
||||
_, _ = ctx.WriteString(response)
|
||||
})
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ func TestRouterPUT(t *testing.T) {
|
||||
var called bool
|
||||
r.PUT("/update", func(ctx *fasthttp.RequestCtx) {
|
||||
called = true
|
||||
ctx.WriteString("PUT response")
|
||||
_, _ = ctx.WriteString("PUT response")
|
||||
})
|
||||
|
||||
var ctx fasthttp.RequestCtx
|
||||
@ -176,7 +176,7 @@ func TestRouterDELETE(t *testing.T) {
|
||||
var called bool
|
||||
r.DELETE("/remove", func(ctx *fasthttp.RequestCtx) {
|
||||
called = true
|
||||
ctx.WriteString("DELETE response")
|
||||
_, _ = ctx.WriteString("DELETE response")
|
||||
})
|
||||
|
||||
var ctx fasthttp.RequestCtx
|
||||
@ -216,7 +216,7 @@ func TestRouterNotFound(t *testing.T) {
|
||||
r := NewRouter()
|
||||
|
||||
r.GET("/exists", func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.WriteString("found")
|
||||
_, _ = ctx.WriteString("found")
|
||||
})
|
||||
|
||||
var ctx fasthttp.RequestCtx
|
||||
|
||||
@ -83,7 +83,7 @@ func TestPlatformSendfile(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
// 测试平台 sendfile(小文件会 fallback 到 copyFile)
|
||||
// 由于没有真实的网络连接,这个测试主要验证不会崩溃
|
||||
@ -122,7 +122,7 @@ func TestCopyFile(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@ -164,7 +164,7 @@ func TestCopyFile(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 重置文件位置
|
||||
file.Seek(0, io.SeekStart)
|
||||
_, _ = file.Seek(0, io.SeekStart)
|
||||
|
||||
// 创建响应上下文
|
||||
ctx := &fasthttp.RequestCtx{}
|
||||
@ -210,7 +210,7 @@ func TestPlatformSendfile_NonLinux(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
err = platformSendfile(nil, file, 0, int64(len(content)))
|
||||
if err != syscall.ENOTSUP {
|
||||
@ -263,7 +263,7 @@ func TestSendFile_SmallFile(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
ctx := &fasthttp.RequestCtx{}
|
||||
ctx.Init(&fasthttp.Request{}, nil, nil)
|
||||
@ -293,7 +293,7 @@ func TestSendFile_WithOffset(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
ctx := &fasthttp.RequestCtx{}
|
||||
ctx.Init(&fasthttp.Request{}, nil, nil)
|
||||
@ -323,7 +323,7 @@ func TestSendFile_ZeroLength(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
ctx := &fasthttp.RequestCtx{}
|
||||
ctx.Init(&fasthttp.Request{}, nil, nil)
|
||||
@ -358,6 +358,7 @@ func TestSendFile_NilFile(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
// 没有 panic,检查是否有错误返回
|
||||
t.Log("expected panic did not occur for nil file")
|
||||
}
|
||||
}()
|
||||
|
||||
@ -378,7 +379,7 @@ func TestCopyFile_Error(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
ctx := &fasthttp.RequestCtx{}
|
||||
ctx.Init(&fasthttp.Request{}, nil, nil)
|
||||
@ -399,13 +400,13 @@ func TestLinuxSendfile_NilConn(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
tmpFile := filepath.Join(tmpDir, "test.txt")
|
||||
content := []byte("test")
|
||||
os.WriteFile(tmpFile, content, 0644)
|
||||
_ = os.WriteFile(tmpFile, content, 0644)
|
||||
|
||||
file, err := os.Open(tmpFile)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
err = linuxSendfile(nil, file.Fd(), 0, int64(len(content)))
|
||||
if err == nil {
|
||||
|
||||
@ -305,7 +305,7 @@ func TestFastHTTPHandler(t *testing.T) {
|
||||
stdHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte("Hello from std http"))
|
||||
_, _ = w.Write([]byte("Hello from std http"))
|
||||
})
|
||||
|
||||
ctx := &fasthttp.RequestCtx{}
|
||||
|
||||
@ -106,7 +106,7 @@ func TestLoggerWithFile(t *testing.T) {
|
||||
|
||||
logger.LogAccess(ctx, 200, 10, 100*time.Millisecond)
|
||||
logger.Error().Str("test", "value").Msg("test error")
|
||||
logger.Close()
|
||||
_ = logger.Close()
|
||||
|
||||
if _, err := os.Stat(accessPath); os.IsNotExist(err) {
|
||||
t.Error("Expected access log file to be created")
|
||||
@ -143,11 +143,11 @@ func TestLoggerNginxFormat(t *testing.T) {
|
||||
|
||||
logger.LogAccess(ctx, 201, 512, 250*time.Millisecond)
|
||||
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
_, _ = io.Copy(&buf, r)
|
||||
|
||||
output := buf.String()
|
||||
expectedFields := []string{"request", "status", "body_bytes_sent", "request_time", "remote_addr"}
|
||||
@ -169,11 +169,11 @@ func TestLoggerDebug(t *testing.T) {
|
||||
logger.Debug().Msg("debug message")
|
||||
logger.Info().Msg("info message")
|
||||
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
_, _ = io.Copy(&buf, r)
|
||||
|
||||
output := buf.String()
|
||||
if !strings.Contains(output, "debug message") {
|
||||
|
||||
@ -46,7 +46,7 @@ func TestAccessLog_Process(t *testing.T) {
|
||||
}
|
||||
|
||||
// 清理
|
||||
al.Close()
|
||||
_ = al.Close()
|
||||
}
|
||||
|
||||
func TestAccessLog_ProcessWithDuration(t *testing.T) {
|
||||
@ -75,5 +75,5 @@ func TestAccessLog_ProcessWithDuration(t *testing.T) {
|
||||
t.Errorf("expected duration >= 10ms, got %v", elapsed)
|
||||
}
|
||||
|
||||
al.Close()
|
||||
_ = al.Close()
|
||||
}
|
||||
|
||||
@ -154,7 +154,7 @@ func TestProcessNoCompression(t *testing.T) {
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.Response.Header.Set("Content-Type", "text/html")
|
||||
ctx.WriteString("Short response")
|
||||
_, _ = ctx.WriteString("Short response")
|
||||
}
|
||||
|
||||
handler := m.Process(nextHandler)
|
||||
@ -189,7 +189,7 @@ func TestProcessWithGzip(t *testing.T) {
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.Response.Header.Set("Content-Type", "text/html")
|
||||
ctx.Write(longResponse)
|
||||
_, _ = ctx.Write(longResponse)
|
||||
}
|
||||
|
||||
handler := m.Process(nextHandler)
|
||||
@ -223,7 +223,7 @@ func TestProcessWithBrotli(t *testing.T) {
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.Response.Header.Set("Content-Type", "text/html")
|
||||
ctx.Write(longResponse)
|
||||
_, _ = ctx.Write(longResponse)
|
||||
}
|
||||
|
||||
handler := m.Process(nextHandler)
|
||||
@ -247,7 +247,7 @@ func TestProcessUnsupportedEncoding(t *testing.T) {
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.Response.Header.Set("Content-Type", "text/html")
|
||||
ctx.WriteString("Test response")
|
||||
_, _ = ctx.WriteString("Test response")
|
||||
}
|
||||
|
||||
handler := m.Process(nextHandler)
|
||||
@ -275,7 +275,7 @@ func TestProcessNonCompressibleType(t *testing.T) {
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.Response.Header.Set("Content-Type", "image/png")
|
||||
ctx.Write(longResponse)
|
||||
_, _ = ctx.Write(longResponse)
|
||||
}
|
||||
|
||||
handler := m.Process(nextHandler)
|
||||
|
||||
@ -93,7 +93,7 @@ func TestRewriteMiddlewareLast(t *testing.T) {
|
||||
if string(ctx.Path()) != "/new/test" {
|
||||
t.Errorf("Expected path /new/test, got %s", ctx.Path())
|
||||
}
|
||||
ctx.WriteString("OK")
|
||||
_, _ = ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
handler := m.Process(nextHandler)
|
||||
@ -152,7 +152,7 @@ func TestRewriteMiddlewarePermanent(t *testing.T) {
|
||||
}
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.WriteString("OK")
|
||||
_, _ = ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
handler := m.Process(nextHandler)
|
||||
|
||||
@ -187,7 +187,7 @@ func TestAccessControlProcess(t *testing.T) {
|
||||
|
||||
// Create a simple handler
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.WriteString("OK")
|
||||
_, _ = ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
handler := ac.Process(nextHandler)
|
||||
|
||||
@ -189,7 +189,7 @@ func TestBasicAuthProcess(t *testing.T) {
|
||||
}
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.WriteString("OK")
|
||||
_, _ = ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
handler := auth.Process(nextHandler)
|
||||
@ -270,7 +270,7 @@ func TestBasicAuthUserCount(t *testing.T) {
|
||||
t.Errorf("Expected UserCount 2, got %d", count)
|
||||
}
|
||||
|
||||
auth.AddUser("user3", "$2b$12$hash3")
|
||||
_ = auth.AddUser("user3", "$2b$12$hash3")
|
||||
if count := auth.UserCount(); count != 3 {
|
||||
t.Errorf("Expected UserCount 3, got %d", count)
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ func TestSecurityHeadersProcess(t *testing.T) {
|
||||
handlerCalled := false
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
handlerCalled = true
|
||||
ctx.WriteString("OK")
|
||||
_, _ = ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
handler := sh.Process(nextHandler)
|
||||
@ -105,7 +105,7 @@ func TestSecurityHeadersHSTS(t *testing.T) {
|
||||
sh := NewSecurityHeaders(cfg)
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.WriteString("OK")
|
||||
_, _ = ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
handler := sh.Process(nextHandler)
|
||||
|
||||
@ -242,7 +242,7 @@ func TestRateLimiterProcess(t *testing.T) {
|
||||
}
|
||||
|
||||
nextHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.WriteString("OK")
|
||||
_, _ = ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
handler := mw.Process(nextHandler)
|
||||
|
||||
@ -189,7 +189,7 @@ func TestServeHTTP_NoHealthyTargets(t *testing.T) {
|
||||
func TestServeHTTP_RequestForwarding(t *testing.T) {
|
||||
// 创建本地测试服务器
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
defer ln.Close()
|
||||
defer func() { _ = ln.Close() }()
|
||||
|
||||
// 启动后端服务器
|
||||
go func() {
|
||||
@ -200,7 +200,7 @@ func TestServeHTTP_RequestForwarding(t *testing.T) {
|
||||
ctx.Response.Header.Set("X-Backend-Header", "test-value")
|
||||
},
|
||||
}
|
||||
s.Serve(ln)
|
||||
_ = s.Serve(ln)
|
||||
}()
|
||||
|
||||
// 等待服务器启动
|
||||
@ -980,7 +980,7 @@ func TestGetClient(t *testing.T) {
|
||||
func TestProxyCache(t *testing.T) {
|
||||
// 创建内存监听器作为后端服务器
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
defer ln.Close()
|
||||
defer func() { _ = ln.Close() }()
|
||||
|
||||
requestCount := 0
|
||||
go func() {
|
||||
@ -992,7 +992,7 @@ func TestProxyCache(t *testing.T) {
|
||||
ctx.Response.Header.Set("X-Request-Count", string(rune(requestCount)))
|
||||
},
|
||||
}
|
||||
s.Serve(ln)
|
||||
_ = s.Serve(ln)
|
||||
}()
|
||||
|
||||
// 等待服务器启动
|
||||
|
||||
@ -13,8 +13,8 @@ import (
|
||||
func TestNewWebSocketBridge(t *testing.T) {
|
||||
clientConn, _ := net.Pipe()
|
||||
targetConn, _ := net.Pipe()
|
||||
defer clientConn.Close()
|
||||
defer targetConn.Close()
|
||||
defer func() { _ = clientConn.Close() }()
|
||||
defer func() { _ = targetConn.Close() }()
|
||||
|
||||
bridge := NewWebSocketBridge(clientConn, targetConn)
|
||||
|
||||
@ -155,8 +155,8 @@ func TestWebSocketBridge_Bridge(t *testing.T) {
|
||||
// 创建管道连接
|
||||
client1, client2 := net.Pipe()
|
||||
target1, target2 := net.Pipe()
|
||||
defer client2.Close()
|
||||
defer target2.Close()
|
||||
defer func() { _ = client2.Close() }()
|
||||
defer func() { _ = target2.Close() }()
|
||||
|
||||
bridge := NewWebSocketBridge(client1, target1)
|
||||
|
||||
@ -169,7 +169,7 @@ func TestWebSocketBridge_Bridge(t *testing.T) {
|
||||
// 发送数据从客户端到后端
|
||||
testData := []byte("hello from client")
|
||||
go func() {
|
||||
client2.Write(testData)
|
||||
_, _ = client2.Write(testData)
|
||||
}()
|
||||
|
||||
// 在后端读取数据
|
||||
@ -185,7 +185,7 @@ func TestWebSocketBridge_Bridge(t *testing.T) {
|
||||
// 发送数据从后端到客户端
|
||||
testData2 := []byte("hello from target")
|
||||
go func() {
|
||||
target2.Write(testData2)
|
||||
_, _ = target2.Write(testData2)
|
||||
}()
|
||||
|
||||
// 在客户端读取数据
|
||||
@ -199,8 +199,8 @@ func TestWebSocketBridge_Bridge(t *testing.T) {
|
||||
}
|
||||
|
||||
// 关闭连接以结束桥接
|
||||
client2.Close()
|
||||
target2.Close()
|
||||
_ = client2.Close()
|
||||
_ = target2.Close()
|
||||
|
||||
// 等待桥接完成
|
||||
select {
|
||||
@ -258,8 +258,8 @@ func TestCopyData(t *testing.T) {
|
||||
// 创建管道连接
|
||||
src1, src2 := net.Pipe()
|
||||
dst1, dst2 := net.Pipe()
|
||||
defer src2.Close()
|
||||
defer dst2.Close()
|
||||
defer func() { _ = src2.Close() }()
|
||||
defer func() { _ = dst2.Close() }()
|
||||
|
||||
bridge := &WebSocketBridge{}
|
||||
|
||||
@ -271,7 +271,7 @@ func TestCopyData(t *testing.T) {
|
||||
|
||||
// 发送数据
|
||||
testData := []byte("test data")
|
||||
src2.Write(testData)
|
||||
_, _ = src2.Write(testData)
|
||||
|
||||
// 接收数据
|
||||
buf := make([]byte, 1024)
|
||||
@ -284,8 +284,8 @@ func TestCopyData(t *testing.T) {
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
src2.Close()
|
||||
dst2.Close()
|
||||
_ = src2.Close()
|
||||
_ = dst2.Close()
|
||||
|
||||
// 等待复制完成
|
||||
select {
|
||||
|
||||
@ -131,7 +131,7 @@ func TestPoolConcurrentSubmit(t *testing.T) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
p.Submit(nil, func(ctx *fasthttp.RequestCtx) {
|
||||
_ = p.Submit(nil, func(ctx *fasthttp.RequestCtx) {
|
||||
counter.Add(1)
|
||||
})
|
||||
}()
|
||||
|
||||
@ -392,13 +392,13 @@ func TestSetListeners(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create listener: %v", err)
|
||||
}
|
||||
defer listener1.Close()
|
||||
defer func() { _ = listener1.Close() }()
|
||||
|
||||
listener2, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create listener: %v", err)
|
||||
}
|
||||
defer listener2.Close()
|
||||
defer func() { _ = listener2.Close() }()
|
||||
|
||||
listeners := []net.Listener{listener1, listener2}
|
||||
s.SetListeners(listeners)
|
||||
|
||||
@ -28,8 +28,8 @@ func TestIsChild(t *testing.T) {
|
||||
}
|
||||
|
||||
// 设置环境变量
|
||||
os.Setenv("GRACEFUL_UPGRADE", "1")
|
||||
defer os.Unsetenv("GRACEFUL_UPGRADE")
|
||||
_ = os.Setenv("GRACEFUL_UPGRADE", "1")
|
||||
defer func() { _ = os.Unsetenv("GRACEFUL_UPGRADE") }()
|
||||
|
||||
if !mgr.IsChild() {
|
||||
t.Error("Expected IsChild to be true when GRACEFUL_UPGRADE=1")
|
||||
@ -38,7 +38,7 @@ func TestIsChild(t *testing.T) {
|
||||
|
||||
func TestPidFile(t *testing.T) {
|
||||
tmpFile := "/tmp/lolly-test.pid"
|
||||
defer os.Remove(tmpFile)
|
||||
defer func() { _ = os.Remove(tmpFile) }()
|
||||
|
||||
mgr := NewUpgradeManager(nil)
|
||||
mgr.SetPidFile(tmpFile)
|
||||
@ -111,13 +111,13 @@ func TestUpgradeSetListeners(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create listener: %v", err)
|
||||
}
|
||||
defer listener1.Close()
|
||||
defer func() { _ = listener1.Close() }()
|
||||
|
||||
listener2, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create listener: %v", err)
|
||||
}
|
||||
defer listener2.Close()
|
||||
defer func() { _ = listener2.Close() }()
|
||||
|
||||
listeners := []net.Listener{listener1, listener2}
|
||||
mgr.SetListeners(listeners)
|
||||
@ -141,7 +141,7 @@ func TestWritePid_NoPidFile(t *testing.T) {
|
||||
// TestReadOldPid_InvalidContent 测试 PID 文件内容无效时的错误处理
|
||||
func TestReadOldPid_InvalidContent(t *testing.T) {
|
||||
tmpFile := "/tmp/lolly-test-invalid.pid"
|
||||
defer os.Remove(tmpFile)
|
||||
defer func() { _ = os.Remove(tmpFile) }()
|
||||
|
||||
// 写入无效内容
|
||||
if err := os.WriteFile(tmpFile, []byte("not-a-pid"), 0644); err != nil {
|
||||
@ -161,7 +161,7 @@ func TestReadOldPid_InvalidContent(t *testing.T) {
|
||||
func TestGetInheritedListeners_InvalidFds(t *testing.T) {
|
||||
// 保存原始环境变量
|
||||
origFds := os.Getenv("LISTEN_FDS")
|
||||
defer os.Setenv("LISTEN_FDS", origFds)
|
||||
defer func() { _ = os.Setenv("LISTEN_FDS", origFds) }()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@ -187,7 +187,7 @@ func TestGetInheritedListeners_InvalidFds(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
os.Setenv("LISTEN_FDS", tt.fdsEnv)
|
||||
_ = os.Setenv("LISTEN_FDS", tt.fdsEnv)
|
||||
|
||||
mgr := NewUpgradeManager(nil)
|
||||
_, err := mgr.GetInheritedListeners()
|
||||
@ -196,10 +196,9 @@ func TestGetInheritedListeners_InvalidFds(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid LISTEN_FDS")
|
||||
}
|
||||
} else {
|
||||
// 零或负数应该返回空列表,无错误
|
||||
// 注意:对于 -1,后续逻辑可能会尝试访问无效的 FD
|
||||
}
|
||||
// 零或负数应该返回空列表,无错误
|
||||
// 注意:对于 -1,后续逻辑可能会尝试访问无效的 FD
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -225,14 +224,14 @@ func TestListenerFile_TCPListener(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create listener: %v", err)
|
||||
}
|
||||
defer listener.Close()
|
||||
defer func() { _ = listener.Close() }()
|
||||
|
||||
file, err := listenerFile(listener)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get listener file: %v", err)
|
||||
}
|
||||
if file != nil {
|
||||
file.Close()
|
||||
_ = file.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,7 +278,7 @@ func TestNotifyOldProcess_WithCurrentPid(t *testing.T) {
|
||||
// TestReadOldPid_EmptyFile 测试空 PID 文件
|
||||
func TestReadOldPid_EmptyFile(t *testing.T) {
|
||||
tmpFile := "/tmp/lolly-test-empty.pid"
|
||||
defer os.Remove(tmpFile)
|
||||
defer func() { _ = os.Remove(tmpFile) }()
|
||||
|
||||
// 写入空内容
|
||||
if err := os.WriteFile(tmpFile, []byte(""), 0644); err != nil {
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
func mockHandler(name string, called *bool) fasthttp.RequestHandler {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
*called = true
|
||||
ctx.WriteString(name)
|
||||
_, _ = ctx.WriteString(name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -338,7 +338,7 @@ func TestOCSPManagerRegisterCertificate(t *testing.T) {
|
||||
ocspServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Return a simple OCSP response
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("mock-ocsp-response"))
|
||||
_, _ = w.Write([]byte("mock-ocsp-response"))
|
||||
}))
|
||||
defer ocspServer.Close()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user