refactor(ops-cleanup): 拆分 executor + table-driven + 提取常量 + 补测试
代码审查反馈: 1. 文件行数超标:ops_cleanup_service.go 594→413 行。 拆 opsCleanupPlan / deleteOldRowsByID / truncateOpsTable / isMissingRelationError + counts struct 到 ops_cleanup_executor.go (164 行)。 2. runCleanupOnce 89 行→30 行(table-driven): 用 []opsCleanupTarget 循环替代三组重复的 opsCleanupPlan → runOne → assign。 3. 魔法值提取常量: opsCleanupDefaultSchedule / opsCleanupBatchSize / opsCleanupCronStopTimeout / opsCleanupRunTimeout / opsCleanupHeartbeatTimeout。 ops_settings.go 中 "0 2 * * *" 也统一引用 opsCleanupDefaultSchedule。 4. 补 5 个缺失测试: - Reload 未 Start 时 no-op - Reload 已 Stop 后 no-op - cleanupReloader==nil 时 Update 不 panic - Start 重复调用幂等 - refreshEffectiveBeforeRun 正确更新 snapshot
This commit is contained in:
@@ -194,3 +194,64 @@ func TestUpdateOpsAdvancedSettings_TriggersReload(t *testing.T) {
|
||||
t.Fatalf("expected reloader.Reload called once, got %d", reloader.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReload_BeforeStart_IsNoop(t *testing.T) {
|
||||
svc := &OpsCleanupService{}
|
||||
if err := svc.Reload(context.Background()); err != nil {
|
||||
t.Fatalf("Reload before Start should return nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReload_AfterStop_IsNoop(t *testing.T) {
|
||||
svc := &OpsCleanupService{started: true, stopped: true}
|
||||
if err := svc.Reload(context.Background()); err != nil {
|
||||
t.Fatalf("Reload after Stop should return nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateOpsAdvancedSettings_NilReloader_NoPanic(t *testing.T) {
|
||||
repo := newRuntimeSettingRepoStub()
|
||||
svc := &OpsService{settingRepo: repo}
|
||||
// cleanupReloader intentionally nil
|
||||
|
||||
cfg := defaultOpsAdvancedSettings()
|
||||
cfg.DataRetention.ErrorLogRetentionDays = 7
|
||||
|
||||
// should not panic
|
||||
if _, err := svc.UpdateOpsAdvancedSettings(context.Background(), cfg); err != nil {
|
||||
t.Fatalf("update with nil reloader: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStart_IdempotentSecondCall(t *testing.T) {
|
||||
svc := &OpsCleanupService{started: true}
|
||||
svc.Start() // second call should be noop, not panic
|
||||
}
|
||||
|
||||
func TestRefreshEffectiveBeforeRun_UpdatesSnapshot(t *testing.T) {
|
||||
repo := newRuntimeSettingRepoStub()
|
||||
base := config.OpsCleanupConfig{
|
||||
Enabled: true,
|
||||
Schedule: "0 2 * * *",
|
||||
ErrorLogRetentionDays: 30,
|
||||
}
|
||||
svc := makeOverlayService(repo, base)
|
||||
svc.computeEffectiveLocked(context.Background())
|
||||
|
||||
if svc.effective.ErrorLogRetentionDays != 30 {
|
||||
t.Fatalf("initial retention should be 30, got %d", svc.effective.ErrorLogRetentionDays)
|
||||
}
|
||||
|
||||
// simulate UI change
|
||||
writeAdvancedSettings(t, repo, OpsDataRetentionSettings{
|
||||
CleanupEnabled: true,
|
||||
CleanupSchedule: "0 * * * *",
|
||||
ErrorLogRetentionDays: 7,
|
||||
})
|
||||
|
||||
svc.refreshEffectiveBeforeRun(context.Background())
|
||||
snap := svc.snapshotEffective()
|
||||
if snap.ErrorLogRetentionDays != 7 {
|
||||
t.Fatalf("after refresh, retention should be 7, got %d", snap.ErrorLogRetentionDays)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user