优化测试数据库连接管理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 15s
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:
@@ -33,8 +33,8 @@ import (
|
||||
|
||||
// regressionTestEnv 回归测试环境
|
||||
type regressionTestEnv struct {
|
||||
db *gorm.DB
|
||||
redisClient *redis.Client
|
||||
tx *gorm.DB
|
||||
rdb *redis.Client
|
||||
app *fiber.App
|
||||
postgresCleanup func()
|
||||
redisCleanup func()
|
||||
@@ -75,13 +75,13 @@ func setupRegressionTestEnv(t *testing.T) *regressionTestEnv {
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接数据库
|
||||
db, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
tx, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// 自动迁移
|
||||
err = db.AutoMigrate(
|
||||
err = tx.AutoMigrate(
|
||||
&model.Account{},
|
||||
&model.Role{},
|
||||
&model.Permission{},
|
||||
@@ -91,21 +91,21 @@ func setupRegressionTestEnv(t *testing.T) *regressionTestEnv {
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接 Redis
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort.Port()),
|
||||
})
|
||||
|
||||
// 初始化所有 Store
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
permStore := postgresStore.NewPermissionStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db, redisClient)
|
||||
accountStore := postgresStore.NewAccountStore(tx, rdb)
|
||||
roleStore := postgresStore.NewRoleStore(tx)
|
||||
permStore := postgresStore.NewPermissionStore(tx)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(tx, rdb)
|
||||
|
||||
// 初始化所有 Service
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
roleSvc := roleService.New(roleStore, permStore, rolePermStore)
|
||||
permSvc := permissionService.New(permStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permSvc := permissionService.New(permStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
// 初始化所有 Handler
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
@@ -136,8 +136,8 @@ func setupRegressionTestEnv(t *testing.T) *regressionTestEnv {
|
||||
routes.RegisterRoutes(app, services, middlewares)
|
||||
|
||||
return ®ressionTestEnv{
|
||||
db: db,
|
||||
redisClient: redisClient,
|
||||
tx: tx,
|
||||
rdb: rdb,
|
||||
app: app,
|
||||
postgresCleanup: func() {
|
||||
if err := pgContainer.Terminate(ctx); err != nil {
|
||||
@@ -212,7 +212,7 @@ func TestAPIRegression_RouteModularization(t *testing.T) {
|
||||
UserType: constants.UserTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(account)
|
||||
env.tx.Create(account)
|
||||
|
||||
// 测试获取账号
|
||||
req := httptest.NewRequest("GET", fmt.Sprintf("/api/admin/accounts/%d", account.ID), nil)
|
||||
@@ -234,7 +234,7 @@ func TestAPIRegression_RouteModularization(t *testing.T) {
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(role)
|
||||
env.tx.Create(role)
|
||||
|
||||
// 测试获取角色
|
||||
req := httptest.NewRequest("GET", fmt.Sprintf("/api/admin/roles/%d", role.ID), nil)
|
||||
@@ -257,7 +257,7 @@ func TestAPIRegression_RouteModularization(t *testing.T) {
|
||||
PermType: constants.PermissionTypeMenu,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(perm)
|
||||
env.tx.Create(perm)
|
||||
|
||||
// 测试获取权限
|
||||
req := httptest.NewRequest("GET", fmt.Sprintf("/api/admin/permissions/%d", perm.ID), nil)
|
||||
@@ -324,7 +324,7 @@ func TestAPIRegression_Pagination(t *testing.T) {
|
||||
UserType: constants.UserTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(account)
|
||||
env.tx.Create(account)
|
||||
}
|
||||
|
||||
t.Run("分页参数正常工作", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user