forked from mtusi2026/mtusi2026lab5
fix: preserve attachment pipeline invariants
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -204,18 +205,7 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
s.Equal(model.AttachmentProcessing, lease.Attachment.Status)
|
||||
s.NotEmpty(lease.Attachment.StagingKey)
|
||||
|
||||
variants := []model.Variant{
|
||||
{
|
||||
Name: "original", ContentType: "image/png", SizeBytes: 128,
|
||||
SHA256: strings.Repeat("a", 64), Width: 320, Height: 200,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/original.png",
|
||||
},
|
||||
{
|
||||
Name: "thumbnail", ContentType: "image/jpeg", SizeBytes: 64,
|
||||
SHA256: strings.Repeat("b", 64), Width: 256, Height: 160,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/thumbnail.jpg",
|
||||
},
|
||||
}
|
||||
variants := completedVariants(attachment.ID, 1)
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
@@ -228,8 +218,26 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.AttachmentReady, metadata.Status)
|
||||
s.Len(metadata.Variants, 1)
|
||||
s.Len(metadata.Variants, 3)
|
||||
s.Equal(variants[1].ObjectKey, variant.ObjectKey)
|
||||
listedKeys := make(map[string]struct{})
|
||||
var after string
|
||||
for {
|
||||
listed, next, err := s.store.ListAttachments(context.Background(), after, 1000)
|
||||
s.Require().NoError(err)
|
||||
for _, candidate := range listed {
|
||||
if candidate.ID == attachment.ID {
|
||||
listedKeys[candidate.ObjectKey] = struct{}{}
|
||||
}
|
||||
}
|
||||
if next == "" {
|
||||
break
|
||||
}
|
||||
after = next
|
||||
}
|
||||
for _, candidate := range variants {
|
||||
s.Contains(listedKeys, candidate.ObjectKey)
|
||||
}
|
||||
|
||||
v2 := model.AttachmentJob{ID: uuid.NewString(), PipelineVersion: 2}
|
||||
queuedV2, err := s.store.QueueAttachmentPipelineVersion(
|
||||
@@ -243,6 +251,46 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestPublishedOutboxRetentionIsBatched() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
_, firstJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
_, secondJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
events, err := s.store.ClaimOutboxEvents(context.Background(), 1000, time.Minute)
|
||||
s.Require().NoError(err)
|
||||
|
||||
eventIDs := make([]string, 0, 2)
|
||||
for _, event := range events {
|
||||
if event.JobID != firstJob.ID && event.JobID != secondJob.ID {
|
||||
continue
|
||||
}
|
||||
published, err := s.store.MarkOutboxPublished(context.Background(), event.ID, event.LeaseToken)
|
||||
s.Require().NoError(err)
|
||||
s.True(published)
|
||||
eventIDs = append(eventIDs, event.ID)
|
||||
}
|
||||
s.Require().Len(eventIDs, 2)
|
||||
_, err = s.pool.Exec(
|
||||
context.Background(),
|
||||
`UPDATE outbox_events SET published_at = clock_timestamp() - INTERVAL '2 days' WHERE id IN ($1, $2)`,
|
||||
eventIDs[0], eventIDs[1],
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
deleted, err := s.store.DeletePublishedOutboxEvents(context.Background(), time.Now().Add(-24*time.Hour), 1)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(1, deleted)
|
||||
deleted, err = s.store.DeletePublishedOutboxEvents(context.Background(), time.Now().Add(-24*time.Hour), 1)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(1, deleted)
|
||||
|
||||
var remaining int
|
||||
err = s.pool.QueryRow(
|
||||
context.Background(), `SELECT count(*) FROM outbox_events WHERE id IN ($1, $2)`, eventIDs[0], eventIDs[1],
|
||||
).Scan(&remaining)
|
||||
s.Require().NoError(err)
|
||||
s.Zero(remaining)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestFailedAttachmentReplayIsAudited() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
@@ -303,22 +351,34 @@ func (s *PostgresSuite) TestExpiredLeaseRecoveryFencesOldWorker() {
|
||||
s.Equal(2, current.Attempt)
|
||||
s.Equal(model.AttachmentJobQueued, current.Status)
|
||||
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, []model.Variant{
|
||||
{
|
||||
Name: "original", ContentType: "image/png", SizeBytes: 128,
|
||||
SHA256: strings.Repeat("a", 64), Width: 320, Height: 200,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/original.png",
|
||||
},
|
||||
{
|
||||
Name: "thumbnail", ContentType: "image/jpeg", SizeBytes: 64,
|
||||
SHA256: strings.Repeat("b", 64), Width: 256, Height: 160,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/thumbnail.jpg",
|
||||
},
|
||||
})
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, completedVariants(attachment.ID, 1))
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestCompleteAttachmentJobRejectsIncompleteOrMismatchedVariants() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
lease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
complete := completedVariants(attachment.ID, job.PipelineVersion)
|
||||
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, complete[:3])
|
||||
s.ErrorIs(err, model.ErrInvalidInput)
|
||||
|
||||
duplicate := append([]model.Variant(nil), complete...)
|
||||
duplicate[3].Name = duplicate[2].Name
|
||||
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, duplicate)
|
||||
s.ErrorIs(err, model.ErrInvalidInput)
|
||||
|
||||
mismatched := append([]model.Variant(nil), complete...)
|
||||
mismatched[3].PipelineVersion++
|
||||
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, mismatched)
|
||||
s.ErrorIs(err, model.ErrInvalidInput)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestAttachmentDeletionClaimChecksLifecycleUnderLock() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
||||
@@ -533,6 +593,29 @@ func createQueuedAttachment(
|
||||
return queued, job
|
||||
}
|
||||
|
||||
func completedVariants(attachmentID string, pipelineVersion int) []model.Variant {
|
||||
types := []struct {
|
||||
name string
|
||||
contentType string
|
||||
extension string
|
||||
}{
|
||||
{name: "original", contentType: "image/png", extension: "png"},
|
||||
{name: "thumbnail", contentType: "image/jpeg", extension: "jpg"},
|
||||
{name: "crop", contentType: "image/jpeg", extension: "jpg"},
|
||||
{name: "optimized", contentType: "image/webp", extension: "webp"},
|
||||
}
|
||||
variants := make([]model.Variant, 0, len(types))
|
||||
for index, variant := range types {
|
||||
variants = append(variants, model.Variant{
|
||||
Name: variant.name, ContentType: variant.contentType, SizeBytes: int64(128 - index*16),
|
||||
SHA256: strings.Repeat(string(rune('a'+index)), 64), Width: 320 - index*32, Height: 200 - index*20,
|
||||
ObjectKey: fmt.Sprintf("attachments/%s/v%d/%s.%s", attachmentID, pipelineVersion, variant.name, variant.extension),
|
||||
PipelineVersion: pipelineVersion,
|
||||
})
|
||||
}
|
||||
return variants
|
||||
}
|
||||
|
||||
func messageCount(t *testing.T, pool *pgxpool.Pool, chatID string) int {
|
||||
t.Helper()
|
||||
var count int
|
||||
|
||||
Reference in New Issue
Block a user