Files
mtusi2026lab5/internal/storage/postgres/pipeline_repository_test.go
T
Vladislav Kunetskiy 4bdcac3dfa lab5: асинхронная очередь, worker и идемпотентный pipeline
API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
2026-10-30 22:36:52 +03:00

136 lines
3.7 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)
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)
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)
}