This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// seedResumableJob 建一条 failed/rollback 的库作业,并按 QueueJobIDFor 预置"上一次执行"留下的队列行,
|
||||
// 复现线上真实形态:队列行主键已存在、idempotency_key 与续作不同(因此旧实现 INSERT 撞主键)。
|
||||
func seedResumableJob(t *testing.T, pool *pgxpool.Pool, jobID, stage, state string, queueLease *time.Time) string {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
if _, err := pool.Exec(ctx, `
|
||||
INSERT INTO library_jobs (id, scope, kind, state, stage, idempotency_key, request_digest, request,
|
||||
queue_job_id, snapshot_id, target_snapshot_id, actor, error, recovery, created_at, updated_at, operation_id)
|
||||
VALUES ($1::text,'internal','restore',$2::text,$3::text,'idem-'||$1::text,'digest-'||$1::text,'{}'::jsonb,
|
||||
$4::text,'snap-before','snap-target','tester','boom','', $5::timestamptz, $5::timestamptz, 'op-'||$1::text)`,
|
||||
jobID, state, stage, governance.QueueJobIDFor(jobID), now); err != nil {
|
||||
t.Fatalf("seed library job: %v", err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `
|
||||
INSERT INTO jobs (id, type, payload, state, priority, available_at, max_attempts, idempotency_key,
|
||||
created_at, updated_at, lease_owner, lease_until, attempt)
|
||||
VALUES ($1::text,'library.restore','{}'::jsonb,'failed',0,$2::timestamptz,1,'library.restore.queue:'||$3::text,$2::timestamptz,$2::timestamptz,'old-worker',$4::timestamptz,1)`,
|
||||
governance.QueueJobIDFor(jobID), now, jobID, queueLease); err != nil {
|
||||
t.Fatalf("seed queue job: %v", err)
|
||||
}
|
||||
return governance.QueueJobIDFor(jobID)
|
||||
}
|
||||
|
||||
// RED 的真实形态:队列行已存在时,续作必须成功复用该行;旧实现会在 Enqueue 撞主键后
|
||||
// 留下 state=pending 且没有可用队列行的假 active。
|
||||
func TestR25ResumeReusesExistingQueueRowAtomically(t *testing.T) {
|
||||
store, pool := newIdentityStore(t)
|
||||
jobID := "librestore-internal-r25-atomic"
|
||||
past := time.Now().UTC().Add(-time.Minute)
|
||||
queueID := seedResumableJob(t, pool, jobID, "rollback", "failed", &past)
|
||||
|
||||
job, resumed, err := store.ResumeLibraryJob(context.Background(), jobID, queueID, "library.restore", []byte(`{}`), time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatalf("resume with existing queue row: %v", err)
|
||||
}
|
||||
if !resumed || job.State != governance.LibraryJobPending {
|
||||
t.Fatalf("resume did not take effect: resumed=%v state=%s", resumed, job.State)
|
||||
}
|
||||
row := pool.QueryRow(context.Background(),
|
||||
`SELECT state, attempt, coalesce(lease_owner,'') FROM jobs WHERE id=$1`, queueID)
|
||||
var state, owner string
|
||||
var attempt int
|
||||
if err := row.Scan(&state, &attempt, &owner); err != nil {
|
||||
t.Fatalf("queue row missing after resume: %v", err)
|
||||
}
|
||||
if state != "pending" || attempt != 0 || owner != "" {
|
||||
t.Fatalf("queue row not reset for resume: state=%s attempt=%d owner=%q", state, attempt, owner)
|
||||
}
|
||||
if job.QueueJobID != queueID {
|
||||
t.Fatalf("library job not attached to queue row: %q", job.QueueJobID)
|
||||
}
|
||||
}
|
||||
|
||||
// 有有效租约时必须整笔回滚:不能出现"业务作业 pending、队列仍被别人持有"的半态。
|
||||
func TestR25ResumeBusyLeaseDoesNotMutateState(t *testing.T) {
|
||||
store, pool := newIdentityStore(t)
|
||||
jobID := "librestore-internal-r25-busy"
|
||||
future := time.Now().UTC().Add(10 * time.Minute)
|
||||
queueID := seedResumableJob(t, pool, jobID, "rollback", "failed", &future)
|
||||
|
||||
_, resumed, err := store.ResumeLibraryJob(context.Background(), jobID, queueID, "library.restore", []byte(`{}`), time.Now().UTC())
|
||||
if !errors.Is(err, ErrLibraryResumeBusy) {
|
||||
t.Fatalf("want ErrLibraryResumeBusy, got %v (resumed=%v)", err, resumed)
|
||||
}
|
||||
var state, stage string
|
||||
if err := pool.QueryRow(context.Background(),
|
||||
`SELECT state, stage FROM library_jobs WHERE id=$1`, jobID).Scan(&state, &stage); err != nil {
|
||||
t.Fatalf("reload: %v", err)
|
||||
}
|
||||
if state != "failed" || stage != "rollback" {
|
||||
t.Fatalf("busy resume mutated job: state=%s stage=%s", state, stage)
|
||||
}
|
||||
var qState string
|
||||
if err := pool.QueryRow(context.Background(), `SELECT state FROM jobs WHERE id=$1`, queueID).Scan(&qState); err != nil {
|
||||
t.Fatalf("queue: %v", err)
|
||||
}
|
||||
if qState != "failed" {
|
||||
t.Fatalf("busy resume mutated queue row: %s", qState)
|
||||
}
|
||||
}
|
||||
|
||||
// 非失败/取消状态不得被续作改写(并发接管后的保护)。
|
||||
func TestR25ResumeRejectsNonFailedStateWithoutMutation(t *testing.T) {
|
||||
store, pool := newIdentityStore(t)
|
||||
jobID := "librestore-internal-r25-succeeded"
|
||||
past := time.Now().UTC().Add(-time.Minute)
|
||||
queueID := seedResumableJob(t, pool, jobID, "finalize", "succeeded", &past)
|
||||
|
||||
_, resumed, err := store.ResumeLibraryJob(context.Background(), jobID, queueID, "library.restore", []byte(`{}`), time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatalf("resume on succeeded job: %v", err)
|
||||
}
|
||||
if resumed {
|
||||
t.Fatal("succeeded job must not be resumed")
|
||||
}
|
||||
var state string
|
||||
if err := pool.QueryRow(context.Background(), `SELECT state FROM library_jobs WHERE id=$1`, jobID).Scan(&state); err != nil {
|
||||
t.Fatalf("reload: %v", err)
|
||||
}
|
||||
if state != "succeeded" {
|
||||
t.Fatalf("state changed on rejected resume: %s", state)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user