修复 WS Token 主动轮换与自动化任务恢复
根据服务端过期时间提前轮换 WS Token,并对缺失期限使用保守刷新窗口。 Token 获取瞬时失败时保持账号运行并在过期前持续重试;统一 Cookie、WS 恢复后的自动化唤醒和安全动作游标重试。 修复多实例恢复租约覆盖、规则查询误隔离及人工核对状态落库错误,并等待自动化调度器、续期 watcher 和批量发布恢复任务优雅退出。 补充 SQLite、PostgreSQL 17、生命周期和竞态回归测试。
This commit is contained in:
1 parent
9c742f5e7f
commit
bdc64c68f6
24 files changed
+1080
-110
No files matched your search
@@ -639,6 +639,74 @@ func TestMultiDB_DeferredAutomationCredentialWake(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiDB_AutomationSafeCheckpointRetry(t *testing.T) {
|
||||
for _, tg := range allTestTargets(t) {
|
||||
t.Run(tg.name, func(t *testing.T) {
|
||||
defer tg.cleanup()
|
||||
ctx := context.Background()
|
||||
s := tg.store
|
||||
suffix := fmt.Sprintf("%d", atomic.AddUint64(&multidbCounter, 1))
|
||||
username := tg.name + "_checkpoint_user_" + suffix
|
||||
if ok, err := s.Users.Create(ctx, username, username+"@e.com", "pw"); err != nil || !ok {
|
||||
t.Fatalf("create user: ok=%v err=%v", ok, err)
|
||||
}
|
||||
user, _ := s.Users.GetByUsername(ctx, username)
|
||||
cookieID := tg.name + "_checkpoint_cookie_" + suffix
|
||||
if err := s.Cookies.Save(ctx, cookieID, "cv", user.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ruleID, err := s.Automation.Create(ctx, AutomationRuleInput{
|
||||
UserID: user.ID, CookieID: cookieID, Name: "checkpoint", TriggerType: "order_paid", Enabled: true,
|
||||
Actions: []AutomationActionInput{{ActionType: "send_text", Enabled: true}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runID, started, err := s.Automation.TryStartRun(ctx, AutomationRun{
|
||||
RuleID: ruleID, CookieID: cookieID, OrderID: "order-" + suffix,
|
||||
TriggerType: "order_paid", TriggerKey: "order_paid:order-" + suffix, RawEventJSON: `{}`,
|
||||
})
|
||||
if err != nil || !started {
|
||||
t.Fatalf("start run: started=%v err=%v", started, err)
|
||||
}
|
||||
if ok, err := s.Automation.StartRunAction(ctx, runID, 1, 0, time.Now().Add(time.Minute).Unix()); err != nil || !ok {
|
||||
t.Fatalf("start first action: ok=%v err=%v", ok, err)
|
||||
}
|
||||
if err := s.Automation.AdvanceRunAction(ctx, runID, 1, 0, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, err := s.Automation.StartRunAction(ctx, runID, 1, 1, time.Now().Add(time.Minute).Unix()); err != nil || !ok {
|
||||
t.Fatalf("start second action: ok=%v err=%v", ok, err)
|
||||
}
|
||||
if err := s.Automation.AbortRunAction(ctx, runID, 1, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Automation.FinishRun(ctx, runID, 1, "failed", 1, SafeRetryErrorPrefix+"session expired"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.DB.ExecContext(ctx, `UPDATE automation_runs SET next_retry_at=0 WHERE id=?`, runID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
due, err := s.Automation.DueRecoveryRuns(ctx, 10)
|
||||
if err != nil || len(due) != 1 || due[0].ID != runID || due[0].ActionCursor != 1 || due[0].SentCount != 1 {
|
||||
t.Fatalf("due=%+v err=%v", due, err)
|
||||
}
|
||||
newLease := time.Now().Add(5 * time.Minute).Unix()
|
||||
claimed, err := s.Automation.ClaimRecoveryRun(ctx, runID, newLease)
|
||||
if err != nil || !claimed {
|
||||
t.Fatalf("claim safe checkpoint: claimed=%v err=%v", claimed, err)
|
||||
}
|
||||
if err := s.Automation.PostponeRecoveryRun(ctx, runID, due[0].AttemptCount, time.Now().Add(time.Minute).Unix()); !errors.Is(err, ErrAutomationRunLeaseLost) {
|
||||
t.Fatalf("stale postpone err=%v want lease lost", err)
|
||||
}
|
||||
current, err := s.Automation.GetRun(ctx, runID)
|
||||
if err != nil || current.LeaseExpiresAt != newLease {
|
||||
t.Fatalf("claimed lease overwritten: run=%+v err=%v", current, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMultiDB_Notifications 验证通知渠道创建 + 账号绑定读写。
|
||||
func TestMultiDB_Notifications(t *testing.T) {
|
||||
for _, tg := range allTestTargets(t) {
|
||||
|
||||
Reference in new issue
Block a user