From 77fdb11b06eaf59893855a1baaa7cfc0d675d2b9 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 3 Apr 2026 18:33:10 +0800 Subject: [PATCH] =?UTF-8?q?test(server):=20=E5=AE=8C=E5=96=84=20server=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E6=B5=8B=E8=AF=95=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 覆盖率从 44.2% 提升至 45.8%,新增测试: - TestPoolWrapHandler: 协程池处理器包装测试 - TestPoolWrapHandler_WhenStopped: 池停止时包装测试 - TestServer_Proxies: 代理管理测试 - TestServer_Running: 运行状态测试 - TestServer_StopWithNilFastServer: 无服务器停止测试 - TestServer_GracefulStopWithNilFastServer: 无服务器优雅停止测试 - TestServer_GetProxyCacheStats: 代理缓存统计测试 - TestServer_BuildMiddlewareChain_EmptyConfig: 空配置中间件链测试 - TestServer_TrackStats_EmptyBody: 空响应体统计测试 注:Start/startSingleMode 等启动函数适合集成测试 Co-Authored-By: Claude Opus 4.6 --- internal/server/pool_test.go | 56 ++++++++++++++ internal/server/server_test.go | 136 +++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) diff --git a/internal/server/pool_test.go b/internal/server/pool_test.go index 888b57f..f0b3b41 100644 --- a/internal/server/pool_test.go +++ b/internal/server/pool_test.go @@ -168,3 +168,59 @@ func TestPoolSubmitWhenStopped(t *testing.T) { t.Error("Expected task to be executed directly when pool is stopped") } } + +func TestPoolWrapHandler(t *testing.T) { + p := NewGoroutinePool(PoolConfig{ + MaxWorkers: 10, + MinWorkers: 2, + QueueSize: 10, + IdleTimeout: 5 * time.Second, + }) + + p.Start() + defer p.Stop() + + var executed atomic.Bool + originalHandler := func(ctx *fasthttp.RequestCtx) { + executed.Store(true) + ctx.SetBodyString("wrapped response") + } + + wrappedHandler := p.WrapHandler(originalHandler) + + ctx := &fasthttp.RequestCtx{} + ctx.Init(&fasthttp.Request{}, nil, nil) + + wrappedHandler(ctx) + + // 等待异步执行 + time.Sleep(100 * time.Millisecond) + + if !executed.Load() { + t.Error("Expected wrapped handler to be executed") + } +} + +func TestPoolWrapHandler_WhenStopped(t *testing.T) { + p := NewGoroutinePool(PoolConfig{ + MaxWorkers: 10, + }) + // 不启动池 + + var executed atomic.Bool + originalHandler := func(ctx *fasthttp.RequestCtx) { + executed.Store(true) + } + + wrappedHandler := p.WrapHandler(originalHandler) + + ctx := &fasthttp.RequestCtx{} + ctx.Init(&fasthttp.Request{}, nil, nil) + + wrappedHandler(ctx) + + // 池停止时应该直接执行 + if !executed.Load() { + t.Error("Expected handler to be executed directly when pool is stopped") + } +} diff --git a/internal/server/server_test.go b/internal/server/server_test.go index e2f2b94..9f17f8e 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -488,3 +488,139 @@ func TestServer_Connections(t *testing.T) { t.Errorf("Expected 0 connections, got %d", s.connections.Load()) } } + +// TestServer_Proxies 测试代理管理 +func TestServer_Proxies(t *testing.T) { + cfg := &config.Config{ + Server: config.ServerConfig{ + Listen: ":8080", + }, + } + + s := New(cfg) + + // 初始代理列表应为空 + if len(s.proxies) != 0 { + t.Error("Initial proxies should be empty") + } +} + +// TestServer_Running 测试运行状态 +func TestServer_Running(t *testing.T) { + cfg := &config.Config{ + Server: config.ServerConfig{ + Listen: ":8080", + }, + } + + s := New(cfg) + + // 初始状态应为未运行 + if s.running { + t.Error("Initial running state should be false") + } +} + +// TestServer_StopWithNilFastServer 测试无 fastServer 时停止 +func TestServer_StopWithNilFastServer(t *testing.T) { + cfg := &config.Config{ + Server: config.ServerConfig{ + Listen: ":8080", + }, + } + + s := New(cfg) + s.fastServer = nil + + err := s.Stop() + if err != nil { + t.Errorf("Stop with nil fastServer should succeed: %v", err) + } +} + +// TestServer_GracefulStopWithNilFastServer 测试无 fastServer 时优雅停止 +func TestServer_GracefulStopWithNilFastServer(t *testing.T) { + cfg := &config.Config{ + Server: config.ServerConfig{ + Listen: ":8080", + }, + } + + s := New(cfg) + s.fastServer = nil + + err := s.GracefulStop(5 * time.Second) + if err != nil { + t.Errorf("GracefulStop with nil fastServer should succeed: %v", err) + } +} + +// TestServer_GetProxyCacheStats 测试代理缓存统计 +func TestServer_GetProxyCacheStats(t *testing.T) { + cfg := &config.Config{ + Server: config.ServerConfig{ + Listen: ":8080", + }, + } + + s := New(cfg) + + // 无代理时应返回空统计 + stats := s.getProxyCacheStats() + if stats.Entries != 0 { + t.Errorf("Expected 0 entries, got %d", stats.Entries) + } + if stats.Pending != 0 { + t.Errorf("Expected 0 pending, got %d", stats.Pending) + } +} + +// TestServer_BuildMiddlewareChain_EmptyConfig 测试空配置的中间件链 +func TestServer_BuildMiddlewareChain_EmptyConfig(t *testing.T) { + cfg := &config.Config{ + Server: config.ServerConfig{ + Listen: ":8080", + }, + } + + s := New(cfg) + + chain, err := s.buildMiddlewareChain(&cfg.Server) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if chain == nil { + t.Error("Expected non-nil chain") + } +} + +// TestServer_TrackStats_EmptyBody 测试空响应体的统计 +func TestServer_TrackStats_EmptyBody(t *testing.T) { + cfg := &config.Config{ + Server: config.ServerConfig{ + Listen: ":8080", + }, + } + + s := New(cfg) + + handler := func(ctx *fasthttp.RequestCtx) { + // 空响应 + } + + wrappedHandler := s.trackStats(handler) + + ctx := &fasthttp.RequestCtx{} + ctx.Init(&fasthttp.Request{}, nil, nil) + ctx.Request.SetBody(nil) + + wrappedHandler(ctx) + + if s.requests.Load() != 1 { + t.Errorf("Expected 1 request, got %d", s.requests.Load()) + } + + if s.bytesSent.Load() != 0 { + t.Errorf("Expected 0 bytes sent, got %d", s.bytesSent.Load()) + } +}