Files
mtusi2026lab5/internal/storage/postgres/pipeline_repository_test.go
T
Vladislav Kunetskiy 9d7a0acb5a lab5: исправления по ревью
Concurrent complete no longer deletes the shared verified source; admission is atomic and covers pending presign rows; relay reconciles queued jobs lost to dead-lettering; failed staging is protected until failed_retention; timestamps normalized to UTC; reprocess prefers the durable original and keeps job->attachment lock order; heartbeat errors no longer burn attempts; confirm timeout enforced; pinned images and sanitized health responses.
2026-10-30 23:13:44 +03:00

207 lines
6.2 KiB
Go

//go:build integration
package postgres_test
import (
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/suite"
"mtusi-messenger/internal/domain"
)
type PipelineSuite struct {
BaseSuite
}
func TestPipelineSuite(t *testing.T) {
suite.Run(t, new(PipelineSuite))
}
func (s *PipelineSuite) attachmentDraft(chat domain.Chat, owner domain.User, version int) (domain.AttachmentDraft, domain.Job) {
s.T().Helper()
id, err := uuid.NewV7()
s.Require().NoError(err)
jobID, err := uuid.NewV7()
s.Require().NoError(err)
draft := domain.AttachmentDraft{
ID: id,
ChatID: chat.ID,
OwnerID: owner.ID,
ObjectKey: domain.OriginalObjectKey(chat.ID, id),
StagingKey: domain.StagingUploadKey(chat.ID, id),
Filename: "photo.png",
ContentType: "image/png",
SizeBytes: 10,
SHA256: strings.Repeat("a", 64),
Status: domain.AttachmentQueued,
PipelineVersion: version,
}
job := domain.Job{
ID: jobID,
AttachmentID: id,
PipelineVersion: version,
SourceKey: draft.StagingKey,
Attempt: 1,
MaxAttempts: 5,
CreatedAt: time.Now().UTC(),
}
return draft, job
}
func (s *PipelineSuite) TestCreateClaimCompleteAndIdempotentRedelivery() {
alice := s.createUser("alice")
bob := s.createUser("bob")
chat := s.createChat(alice, bob)
draft, job := s.attachmentDraft(chat, alice, 1)
repo := s.store.Pipeline()
created, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
s.Equal(domain.AttachmentQueued, created.Status)
s.Equal(1, created.PipelineVersion)
message := domain.NewJobMessage(job)
claim, err := repo.ClaimJob(s.T().Context(), message, "lease-1", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
s.Equal(domain.AttachmentProcessing, claim.Attachment.Status)
duplicate, err := repo.ClaimJob(s.T().Context(), message, "lease-2", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimBusyLease, duplicate.Outcome)
variants := []domain.Variant{
{
Name: domain.VariantThumbnail, ContentType: "image/jpeg", SizeBytes: 11,
Width: 256, Height: 160, SHA256: strings.Repeat("b", 64),
ObjectKey: domain.VariantObjectKey(chat.ID, draft.ID, 1, domain.VariantThumbnail),
},
}
ok, err := repo.CompleteJob(s.T().Context(), job.ID, 1, "lease-1", variants)
s.Require().NoError(err)
s.True(ok)
ready, err := repo.GetForMember(s.T().Context(), bob.ID, draft.ID)
s.Require().NoError(err)
s.Equal(domain.AttachmentReady, ready.Status)
stored, err := repo.ListVariants(s.T().Context(), draft.ID, 1)
s.Require().NoError(err)
s.Require().Len(stored, 1)
s.Equal(domain.VariantThumbnail, stored[0].Name)
// A redelivery after ready is a no-op, even with the same attempt.
again, err := repo.ClaimJob(s.T().Context(), message, "lease-3", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAlreadyReady, again.Outcome)
}
func (s *PipelineSuite) TestLeaseExpiryIsBoundedByMaxAttempts() {
alice := s.createUser("alice")
chat := s.createChat(alice)
draft, job := s.attachmentDraft(chat, alice, 1)
job.MaxAttempts = 1
repo := s.store.Pipeline()
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
claim, err := repo.ClaimJob(s.T().Context(), domain.NewJobMessage(job), "lease", time.Millisecond)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
time.Sleep(10 * time.Millisecond)
reaped, err := repo.ReapExpiredLeases(s.T().Context(), 10)
s.Require().NoError(err)
s.Equal(1, reaped)
dead, err := repo.GetJob(s.T().Context(), job.ID)
s.Require().NoError(err)
s.Equal(domain.JobFailed, dead.Status)
s.NotNil(dead.LastErrorCode)
s.Equal("retry_exhausted", *dead.LastErrorCode)
}
func (s *PipelineSuite) TestAdmissionCountsPendingPresignAndIsAtomic() {
alice := s.createUser("alice")
chat := s.createChat(alice)
repo := s.store.Pipeline()
draft, _ := s.attachmentDraft(chat, alice, 1)
draft.ObjectKey = "staging/presign/" + chat.ID.String() + "/" + draft.ID.String()
draft.StagingKey = draft.ObjectKey
draft.Status = domain.AttachmentPending
limits := domain.AdmissionLimits{PerUser: 1, Total: 10}
_, err := repo.CreatePresign(s.T().Context(), draft, limits)
s.Require().NoError(err)
second, _ := s.attachmentDraft(chat, alice, 1)
second.ObjectKey = "staging/presign/" + chat.ID.String() + "/" + second.ID.String()
second.StagingKey = second.ObjectKey
second.Status = domain.AttachmentPending
_, err = repo.CreatePresign(s.T().Context(), second, limits)
s.Require().ErrorIs(err, domain.ErrRateLimited)
}
func (s *PipelineSuite) TestReconcileQueuedJobsRepublishesLostIntent() {
alice := s.createUser("alice")
chat := s.createChat(alice)
repo := s.store.Pipeline()
draft, job := s.attachmentDraft(chat, alice, 1)
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
claimed, err := repo.ClaimOutbox(s.T().Context(), "relay", time.Minute, 10)
s.Require().NoError(err)
s.Require().Len(claimed, 1)
s.Require().NoError(repo.MarkPublished(s.T().Context(), []int64{claimed[0].ID}))
reconciled, err := repo.ReconcileQueuedJobs(s.T().Context(), 0, 10)
s.Require().NoError(err)
s.Equal(1, reconciled)
pending, err := repo.ClaimOutbox(s.T().Context(), "relay", time.Minute, 10)
s.Require().NoError(err)
s.Require().Len(pending, 1)
s.Equal("reconcile", pending[0].Kind)
s.Equal(job.ID, pending[0].JobID)
}
func (s *PipelineSuite) TestFailedStagingStaysReferencedUntilRetention() {
alice := s.createUser("alice")
chat := s.createChat(alice)
repo := s.store.Pipeline()
draft, job := s.attachmentDraft(chat, alice, 1)
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
claim, err := repo.ClaimJob(s.T().Context(), domain.NewJobMessage(job), "lease", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
ok, err := repo.FailJob(s.T().Context(), job.ID, 1, "lease", "invalid_image")
s.Require().NoError(err)
s.True(ok)
keys, err := repo.ListReferencedKeys(s.T().Context(), "", 100)
s.Require().NoError(err)
s.Contains(keys, draft.StagingKey)
}