Переведите приём и чтение вложений на асинхронный контракт

This commit is contained in:
2026-11-18 15:33:32 +03:00
parent efc56b1382
commit bf970227d6
19 changed files with 667 additions and 367 deletions
+44
View File
@@ -729,3 +729,47 @@ func TestReconcileLockRejectsMalformedHash(t *testing.T) {
t.Fatal("malformed hash accepted")
}
}
func TestGCRespectsJobSourcesAndVersionedOutputs(t *testing.T) {
ctx := context.Background()
db, err := testenv.StartPostgres(ctx)
if err != nil {
t.Fatal(err)
}
defer db.Close()
f := reconcileSetup(t, db)
hash := f.file("job input", "pending", 48*time.Hour, true, false)
var id uuid.UUID
if err := db.Pool.QueryRow(ctx, `SELECT id FROM attachments WHERE file_hash=$1`, hash).Scan(&id); err != nil {
t.Fatal(err)
}
stage := "staging/" + id.String()
output := "variants/" + id.String() + "/1/thumbnail/" + strings.Repeat("0", 64)
old := time.Now().Add(-48 * time.Hour)
f.b.add(stage, []byte("job input"), old)
f.b.add(output, []byte("partial"), old)
f.exec(`INSERT INTO attachment_jobs(attachment_id,source_key) VALUES ($1,$2)`, id, stage)
for _, apply := range []bool{false, true} {
stats, _, err := f.run(apply, false)
if err != nil || stats.Findings != 0 || !f.b.has(stage) || !f.b.has(output) {
t.Fatalf("active job lost: %+v %v", stats, err)
}
}
f.exec(`UPDATE attachment_jobs SET state='failed', error_code='retry_exhausted',updated_at=now()`)
stats, _, err := f.run(true, false)
if err != nil || stats.Repaired != 1 || !f.b.has(stage) || f.b.has(output) {
t.Fatalf("failed retention: %+v %v", stats, err)
}
f.exec(`UPDATE attachment_jobs SET updated_at=now()-interval '3 hours'`)
stats, _, err = f.run(true, false)
if err != nil || stats.Repaired != 0 || !f.b.has(stage) {
t.Fatalf("default failed retention: %+v %v", stats, err)
}
r, err := attachment.NewReconcilerWithRetention(db.Pool, f.b, time.Hour, 2*time.Hour)
if err != nil {
t.Fatal(err)
}
stats, err = r.Run(ctx, attachment.ReconcileOptions{Apply: true}, nil)
if err != nil || stats.Repaired != 1 || f.b.has(stage) {
t.Fatalf("expired source: %+v %v", stats, err)
}
}