You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.2 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 测试覆盖率文档
## 测试状态
### 已完成的测试
#### 1. Connection 测试 (`internal/connection/connection_test.go`)
- ✅ ConnectionManager 基本功能测试
- ✅ 添加/获取/移除连接测试
- ✅ 最大连接数限制测试
- ✅ 连接分组测试
- ✅ 分组广播测试
- ✅ 非活动连接清理测试
- ✅ 错误处理测试nil连接、不存在的连接等
#### 2. Context 测试 (`test/context_test.go`)
- ✅ Context 基本功能测试
- ✅ Set/Get/MustGet 测试
- ✅ GetString/GetInt/GetBool 测试
- ✅ 带超时的 Context 测试
- ✅ Context Value 方法测试
- ✅ Request 和 Response 访问测试
- ✅ Connection 访问测试
#### 3. Unpacker 测试 (`test/unpacker_test.go`)
- ✅ DelimiterUnpacker 测试
- 单个消息拆包
- 多个消息拆包
- 不完整消息处理
- 打包功能
- ✅ LengthFieldUnpacker 测试
- 单个消息拆包
- 多个消息拆包
- 不完整消息处理
- 打包功能
- ✅ FixedLengthUnpacker 测试
- 单个消息拆包
- 多个消息拆包
- 不完整消息处理
- 打包功能(填充和截断)
- ✅ FrameHeaderUnpacker 测试
- 默认实现测试
- 自定义 GetLength 函数测试
- 打包功能
- ✅ 缓冲区大小限制测试
#### 4. Router 测试 (`test/router_test.go`)
- ✅ StringMatcher 测试
- ✅ Router 注册测试
- ✅ Router 匹配测试
- ✅ Router 分组测试
- ✅ Router 选项测试WithRequestType
- ✅ 自定义匹配器测试
### 测试覆盖率目标
目标覆盖率:**85%+**
### 运行测试
#### 运行所有测试
```bash
go test ./... -v
```
#### 运行特定包的测试
```bash
# Connection 测试
go test ./internal/connection/... -v
# Context 测试
go test ./pkg/context/... -v
# Unpacker 测试
go test ./internal/unpacker/... -v
# Router 测试
go test ./internal/router/... -v
```
#### 生成覆盖率报告
```bash
# 生成覆盖率文件
go test ./... -coverprofile=coverage.out
# 查看覆盖率报告
go tool cover -html=coverage.out
# 查看覆盖率百分比
go test ./... -cover
```
#### 按包生成覆盖率报告
```bash
# Connection
go test ./internal/connection/... -coverprofile=coverage_connection.out
go tool cover -html=coverage_connection.out -o coverage_connection.html
# Context
go test ./pkg/context/... -coverprofile=coverage_context.out
go tool cover -html=coverage_context.out -o coverage_context.html
# Unpacker
go test ./internal/unpacker/... -coverprofile=coverage_unpacker.out
go tool cover -html=coverage_unpacker.out -o coverage_unpacker.html
# Router
go test ./internal/router/... -coverprofile=coverage_router.out
go tool cover -html=coverage_router.out -o coverage_router.html
```
### 待完成的测试
#### 1. Server 测试
- [ ] Server 启动/停止测试
- [ ] Server 事件处理测试
- [ ] Server 连接管理测试
- [ ] Server 路由处理测试
- [ ] Server 错误处理测试
#### 2. Codec 测试
- [ ] JSONCodec 测试
- [ ] BinaryCodec 测试
- [ ] MessagePackCodec 测试
- [ ] ProtobufCodec 测试
- [ ] Codec Registry 测试
#### 3. Response 测试
- [ ] Response Write 测试
- [ ] Response WriteWithCodec 测试
- [ ] Response WriteBytes 测试
- [ ] Response WriteString 测试
- [ ] Response Header 测试
#### 4. Request 测试
- [ ] Request Raw 测试
- [ ] Request Body 测试
- [ ] Request Bind 测试
- [ ] Request Header 测试
#### 5. Protocol 测试
- [ ] NNET Protocol 测试
- [ ] Protocol Encode/Decode 测试
- [ ] Protocol FrameHeader 测试
#### 6. 集成测试
- [ ] 端到端测试
- [ ] 并发测试
- [ ] 性能测试
- [ ] 压力测试
### 测试最佳实践
1. **单元测试**
- 每个函数都应该有对应的测试
- 测试应该覆盖正常情况和错误情况
- 使用表格驱动测试Table-Driven Tests
2. **集成测试**
- 测试组件之间的交互
- 使用真实的数据和场景
- 测试边界条件和错误处理
3. **性能测试**
- 使用 `testing.B` 进行基准测试
- 测试并发性能
- 测试内存使用
4. **覆盖率**
- 目标是 85%+ 的覆盖率
- 关注关键路径的覆盖率
- 使用覆盖率工具找出未测试的代码
### 测试文件组织
- 测试文件应该与源代码文件在同一包中
- 测试文件命名:`*_test.go`
- 测试函数命名:`Test*`
- 基准测试函数命名:`Benchmark*`
- 示例函数命名:`Example*`
### 注意事项
1. **Windows 环境问题**
- 在某些 Windows 环境中可能会遇到测试执行问题
- 建议在 Linux 或 macOS 环境中运行测试
- 或者使用 WSL (Windows Subsystem for Linux)
2. **测试数据**
- 使用临时文件或内存数据
- 避免依赖外部资源
- 清理测试数据
3. **并发测试**
- 使用 `sync.WaitGroup``chan` 进行同步
- 避免竞态条件
- 使用 `go test -race` 检测竞态条件
### 下一步
1. 完成 Server 测试
2. 完成 Codec 测试
3. 完成 Response 和 Request 测试
4. 完成 Protocol 测试
5. 编写集成测试
6. 达到 85%+ 覆盖率目标
### 参考资料
- [Go Testing Package](https://golang.org/pkg/testing/)
- [Go Coverage Tool](https://blog.golang.org/cover)
- [Table-Driven Tests](https://github.com/golang/go/wiki/TableDrivenTests)
- [Advanced Testing in Go](https://about.sourcegraph.com/go/advanced-testing-in-go)