lolly/internal/server/server_test.go
xfy 9d24263918 feat(stream,server,handler): 实现 Phase 6 性能优化和热升级
新增功能:
- stream 模块: 流式传输支持,优化大文件和实时数据传输
- Goroutine 池: 限制并发数量,减少调度开销
- 优雅升级: 零停机热升级,继承父进程监听器
- sendfile: 零拷贝文件传输,大文件直接从内核传输

重构改进:
- App 结构体封装,支持热升级和信号处理
- 配置结构字段对齐和代码清理
- 完善错误处理和日志记录

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-03 10:39:22 +08:00

108 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package server
import (
"testing"
"time"
"rua.plus/lolly/internal/config"
)
// TestNew 测试服务器创建
func TestNew(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{
Listen: ":8080",
Static: config.StaticConfig{
Root: "./static",
Index: []string{"index.html"},
},
},
}
s := New(cfg)
if s == nil {
t.Fatal("New() returned nil, expected non-nil Server")
}
if s.config != cfg {
t.Error("Server.config not set correctly")
}
if s.running {
t.Error("Server.running should be false initially")
}
if s.fastServer != nil {
t.Error("Server.fastServer should be nil before Start()")
}
}
// TestStopWithoutServer 测试无服务器时调用 Stop
func TestStopWithoutServer(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{
Listen: ":8080",
},
}
s := New(cfg)
// 在未启动时调用 Stop应返回 nil
err := s.Stop()
if err != nil {
t.Errorf("Stop() on non-started server returned error: %v", err)
}
}
// TestGracefulStop 测试 GracefulStop 调用
func TestGracefulStop(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{
Listen: ":8080",
},
}
s := New(cfg)
// 在未启动时调用 GracefulStop应返回 nil
err := s.GracefulStop(5 * time.Second)
if err != nil {
t.Errorf("GracefulStop() on non-started server returned error: %v", err)
}
}
// TestStopAfterStop 测试多次调用 Stop
func TestStopAfterStop(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{
Listen: ":8080",
},
}
s := New(cfg)
// 多次调用 Stop 应该都是安全的
for i := 0; i < 3; i++ {
err := s.Stop()
if err != nil {
t.Errorf("Stop() call %d returned error: %v", i+1, err)
}
}
}
// TestGracefulStopWithZeroTimeout 测试零超时的 GracefulStop
func TestGracefulStopWithZeroTimeout(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{
Listen: ":8080",
},
}
s := New(cfg)
err := s.GracefulStop(0)
if err != nil {
t.Errorf("GracefulStop(0) returned error: %v", err)
}
}