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.
This commit is contained in:
@@ -189,7 +189,7 @@ func TestUploadStagesAndQueues(t *testing.T) {
|
||||
}), mock.MatchedBy(func(job domain.Job) bool {
|
||||
return job.PipelineVersion == 1 && job.Attempt == 1 &&
|
||||
job.MaxAttempts == 5 && job.SourceKey != ""
|
||||
})).Return(domain.Attachment{
|
||||
}), domain.AdmissionLimits{}).Return(domain.Attachment{
|
||||
ID: uuid.New(), ChatID: chatID, OwnerID: ownerID,
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentQueued, PipelineVersion: 1,
|
||||
@@ -355,6 +355,89 @@ func TestCompleteQueuesPresignedObject(t *testing.T) {
|
||||
require.Len(t, store.objects, 1)
|
||||
}
|
||||
|
||||
func TestCompleteConcurrentConflictKeepsVerifiedSource(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
|
||||
|
||||
id := uuid.New()
|
||||
chatID := uuid.New()
|
||||
ownerID := uuid.New()
|
||||
content := pngBytes(t, 10, 10)
|
||||
sum := sha256.Sum256(content)
|
||||
digest := hex.EncodeToString(sum[:])
|
||||
stagingKey := "staging/presign/" + chatID.String() + "/" + id.String()
|
||||
|
||||
store.objects[stagingKey] = storedObject{
|
||||
data: content, contentType: "image/png",
|
||||
metadata: map[string]string{"sha256": digest},
|
||||
}
|
||||
|
||||
pending := domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: stagingKey,
|
||||
StagingKey: stagingKey, ContentType: "image/png",
|
||||
SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentPending, PipelineVersion: 1,
|
||||
}
|
||||
queued := domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: "attachments/" + chatID.String() + "/" + id.String(),
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentQueued, PipelineVersion: 1,
|
||||
}
|
||||
|
||||
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(pending, nil).Once()
|
||||
repo.EXPECT().CompletePresign(
|
||||
mock.Anything, ownerID, id, mock.Anything, mock.Anything,
|
||||
1, 5, mock.Anything,
|
||||
).Return(domain.Attachment{}, domain.ErrConflict).Once()
|
||||
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(queued, nil).Once()
|
||||
|
||||
got, err := service.Complete(t.Context(), ownerID, id, digest)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, domain.AttachmentQueued, got.Status)
|
||||
|
||||
verified := 0
|
||||
for key := range store.objects {
|
||||
if strings.HasPrefix(key, "staging/verified/") {
|
||||
verified++
|
||||
}
|
||||
}
|
||||
require.Equal(t, 1, verified, "loser must not delete the winner's verified source")
|
||||
}
|
||||
|
||||
func TestPresignHonoursAdmissionLimit(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||
repo.EXPECT().CreatePresign(mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(domain.Attachment{}, domain.ErrRateLimited).Once()
|
||||
|
||||
_, err := service.Presign(t.Context(), domain.PresignRequest{
|
||||
ChatID: uuid.New(), OwnerID: uuid.New(), Filename: "photo.png",
|
||||
ContentType: "image/png", SizeBytes: 10, SHA256: strings.Repeat("a", 64),
|
||||
})
|
||||
require.ErrorIs(t, err, domain.ErrRateLimited)
|
||||
}
|
||||
|
||||
func TestUploadRateLimitedByAtomicAdmissionRemovesStaging(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||
repo.EXPECT().Admission(mock.Anything, mock.Anything).Return(0, 0, int64(0), nil).Once()
|
||||
repo.EXPECT().CreateQueued(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(domain.Attachment{}, domain.ErrRateLimited).Once()
|
||||
|
||||
_, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: uuid.New(), OwnerID: uuid.New(), Filename: "photo.png",
|
||||
DeclaredContentType: "image/png", Body: bytes.NewReader(pngBytes(t, 8, 8)),
|
||||
})
|
||||
require.ErrorIs(t, err, domain.ErrRateLimited)
|
||||
require.Empty(t, store.objects, "a rejected atomic admission must not leave staging")
|
||||
}
|
||||
|
||||
func TestCompleteRejectsChecksumMismatch(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
|
||||
Reference in New Issue
Block a user