forked from mtusi2026/mtusi2026lab6
166 lines
5.8 KiB
Go
166 lines
5.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"mtusi2026/internal/idgen"
|
|
"mtusi2026/internal/jobs"
|
|
"mtusi2026/internal/model"
|
|
"mtusi2026/internal/store"
|
|
)
|
|
|
|
func TestPipeline_AtomicIntentClaimsFencingAndReplay(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
user := mustRegisterUser(t, srv, "pipeline_user")
|
|
chat := mustCreateChat(t, srv, user, "pipeline", []string{user.ID})
|
|
ctx := context.Background()
|
|
st := store.New(testPool, newUnlimitedLimiter())
|
|
id := idgen.GenerateUUIDv4()
|
|
att, err := st.CreateAttachment(ctx, model.Attachment{ID: id, ChatID: chat.ID, OwnerID: user.ID,
|
|
Filename: "test.png", ContentType: "image/png", SizeBytes: 100, SHA256: strings.Repeat("a", 64), ObjectKey: "attachments/" + id, StagingKey: "staging/" + id})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if att.Status != "queued" {
|
|
t.Fatal(att.Status)
|
|
}
|
|
confirmed, err := st.CreateMessage(ctx, chat.ID, user.ID, "queued-before-failure", "", []string{id})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
j := jobs.Job{SchemaVersion: 1, Type: "attachment.process.v1", AttachmentID: id, PipelineVersion: 1, Attempt: 1}
|
|
if err = testPool.QueryRow(ctx, `SELECT j.id,j.created_at FROM attachment_jobs j JOIN attachment_outbox o ON o.job_id=j.id WHERE j.attachment_id=$1 AND o.published_at IS NULL`, id).Scan(&j.ID, &j.CreatedAt); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := jobs.Repo{Pool: testPool, MaxAttempts: 2, Lease: time.Minute}
|
|
var claimed atomic.Int32
|
|
var work *jobs.Work
|
|
var mu sync.Mutex
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
w, err := repo.Claim(ctx, j)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if w != nil {
|
|
claimed.Add(1)
|
|
mu.Lock()
|
|
work = w
|
|
mu.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if claimed.Load() != 1 {
|
|
t.Fatalf("concurrent claims=%d", claimed.Load())
|
|
}
|
|
stale := *work
|
|
stale.Token = idgen.GenerateUUIDv4()
|
|
if ok, err := repo.Complete(ctx, &stale, []model.Variant{}); err != nil || ok {
|
|
t.Fatalf("stale commit ok=%v err=%v", ok, err)
|
|
}
|
|
if _, err = testPool.Exec(ctx, `UPDATE attachment_jobs SET lease_until=now()-interval '1 second' WHERE id=$1`, j.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = repo.Recover(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, err := repo.Complete(ctx, work, []model.Variant{}); err != nil || ok {
|
|
t.Fatalf("expired commit ok=%v err=%v", ok, err)
|
|
}
|
|
if _, err = testPool.Exec(ctx, `UPDATE attachment_jobs SET next_attempt_at=now() WHERE id=$1`, j.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
j.Attempt = 2
|
|
work, err = repo.Claim(ctx, j)
|
|
if err != nil || work == nil {
|
|
t.Fatalf("retry claim %v %v", work, err)
|
|
}
|
|
if err = repo.Fail(ctx, work, "storage_unavailable", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
failed, err := st.GetAttachmentForUser(ctx, id, user.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if failed.Status != "failed" || failed.ErrorCode == nil || *failed.ErrorCode != "retry_exhausted" {
|
|
t.Fatalf("failed: %+v", failed)
|
|
}
|
|
replayed, err := st.CreateMessage(ctx, chat.ID, user.ID, "queued-before-failure", "", []string{id})
|
|
if err != nil || replayed.ID != confirmed.ID {
|
|
t.Fatalf("confirmed message changed after attachment failure: %+v %v", replayed, err)
|
|
}
|
|
if _, err = st.CreateMessage(ctx, chat.ID, user.ID, "failed-att", "", []string{id}); err != store.ErrAttachmentFailed {
|
|
t.Fatalf("failed attachment allowed: %v", err)
|
|
}
|
|
if err = repo.Replay(ctx, j.ID, "storage recovered"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
j.Attempt = 3
|
|
work, err = repo.Claim(ctx, j)
|
|
if err != nil || work == nil {
|
|
t.Fatalf("replay claim %v %v", work, err)
|
|
}
|
|
if err = repo.Fail(ctx, work, "storage_unavailable", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = testPool.Exec(ctx, `UPDATE attachment_jobs SET next_attempt_at=now() WHERE id=$1`, j.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
j.Attempt = 4
|
|
work, err = repo.Claim(ctx, j)
|
|
if err != nil || work == nil {
|
|
t.Fatalf("replay retry claim %v %v", work, err)
|
|
}
|
|
if ok, err := repo.Complete(ctx, work, []model.Variant{}); err != nil || !ok {
|
|
t.Fatalf("complete %v %v", ok, err)
|
|
}
|
|
if w, err := repo.Claim(ctx, j); err != nil || w != nil {
|
|
t.Fatalf("ready redelivery executed %v %v", w, err)
|
|
}
|
|
if err = repo.Replay(ctx, j.ID, "must refuse"); err == nil {
|
|
t.Fatal("ready replay accepted")
|
|
}
|
|
var audits int
|
|
if err = testPool.QueryRow(ctx, `SELECT count(*) FROM job_replays WHERE job_id=$1`, j.ID).Scan(&audits); err != nil || audits != 1 {
|
|
t.Fatalf("audit %d %v", audits, err)
|
|
}
|
|
}
|
|
|
|
func TestPipeline_IntentRollsBackWithAttachment(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
defer srv.Close()
|
|
user := mustRegisterUser(t, srv, "pipeline_rollback")
|
|
chat := mustCreateChat(t, srv, user, "rollback", []string{user.ID})
|
|
ctx := context.Background()
|
|
_, err := testPool.Exec(ctx, `CREATE FUNCTION reject_test_outbox() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected outbox write failure'; END $$;
|
|
CREATE TRIGGER reject_test_outbox BEFORE INSERT ON attachment_outbox FOR EACH ROW EXECUTE FUNCTION reject_test_outbox()`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer testPool.Exec(ctx, `DROP TRIGGER reject_test_outbox ON attachment_outbox; DROP FUNCTION reject_test_outbox()`)
|
|
id := idgen.GenerateUUIDv4()
|
|
st := store.New(testPool, newUnlimitedLimiter())
|
|
_, err = st.CreateAttachment(ctx, model.Attachment{ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "x.png", ContentType: "image/png", SizeBytes: 10, SHA256: strings.Repeat("b", 64), ObjectKey: "attachments/" + id, StagingKey: "staging/" + id})
|
|
if err == nil {
|
|
t.Fatal("expected injected failure")
|
|
}
|
|
var count int
|
|
if err = testPool.QueryRow(ctx, `SELECT (SELECT count(*) FROM attachments WHERE id=$1)+(SELECT count(*) FROM attachment_jobs WHERE attachment_id=$1)`, id).Scan(&count); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 0 {
|
|
t.Fatalf("partial durable intent rows=%d", count)
|
|
}
|
|
}
|