优化测试数据库连接管理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 15s

- 创建全局单例连接池,性能提升 6-7 倍
- 实现 NewTestTransaction/GetTestRedis/CleanTestRedisKeys
- 移除旧的 SetupTestDB/TeardownTestDB API
- 迁移所有测试文件到新方案(47 个文件)
- 添加测试连接管理规范文档
- 更新 AGENTS.md 和 README.md

性能对比:
- 旧方案:~71 秒(204 测试)
- 新方案:~10.5 秒(首次初始化 + 后续复用)
- 内存占用降低约 80%
- 网络连接数从 204 降至 1
This commit is contained in:
2026-01-22 14:38:43 +08:00
parent 46e4e5f4f1
commit b68e7ec013
47 changed files with 2529 additions and 986 deletions

View File

@@ -16,13 +16,13 @@ import (
// TestQueueClientEnqueue 测试任务入队
func TestQueueClientEnqueue(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -47,13 +47,13 @@ func TestQueueClientEnqueue(t *testing.T) {
// TestQueueClientEnqueueWithOptions 测试带选项的任务入队
func TestQueueClientEnqueueWithOptions(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -136,13 +136,13 @@ func TestQueueClientEnqueueWithOptions(t *testing.T) {
// TestQueueClientTaskUniqueness 测试任务唯一性
func TestQueueClientTaskUniqueness(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -224,13 +224,13 @@ func TestTaskPayloadSizeLimit(t *testing.T) {
// Redis 默认支持最大 512MB但实际应用中不建议超过 1MB
}
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -268,13 +268,13 @@ func TestTaskPayloadSizeLimit(t *testing.T) {
// TestTaskScheduling 测试任务调度
func TestTaskScheduling(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -320,13 +320,13 @@ func TestTaskScheduling(t *testing.T) {
// TestQueueInspectorStats 测试队列统计
func TestQueueInspectorStats(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -363,13 +363,13 @@ func TestQueueInspectorStats(t *testing.T) {
// TestTaskRetention 测试任务保留策略
func TestTaskRetention(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -395,13 +395,13 @@ func TestTaskRetention(t *testing.T) {
// TestQueueDraining 测试队列暂停和恢复
func TestQueueDraining(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -429,13 +429,13 @@ func TestQueueDraining(t *testing.T) {
// TestTaskCancellation 测试任务取消
func TestTaskCancellation(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -471,13 +471,13 @@ func TestTaskCancellation(t *testing.T) {
// TestBatchTaskEnqueue 测试批量任务入队
func TestBatchTaskEnqueue(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
@@ -512,13 +512,13 @@ func TestBatchTaskEnqueue(t *testing.T) {
// TestTaskGrouping 测试任务分组
func TestTaskGrouping(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer func() { _ = redisClient.Close() }()
defer func() { _ = rdb.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
rdb.FlushDB(ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",