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.
This commit is contained in:
@@ -269,3 +269,75 @@ func (_c *MockRepository_ListVisible_Call) RunAndReturn(run func(ctx context.Con
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// Replay provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) Replay(ctx context.Context, draft domain.MessageDraft) (domain.Message, bool, error) {
|
||||
ret := _mock.Called(ctx, draft)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Replay")
|
||||
}
|
||||
|
||||
var r0 domain.Message
|
||||
var r1 bool
|
||||
var r2 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.MessageDraft) (domain.Message, bool, error)); ok {
|
||||
return returnFunc(ctx, draft)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.MessageDraft) domain.Message); ok {
|
||||
r0 = returnFunc(ctx, draft)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Message)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, domain.MessageDraft) bool); ok {
|
||||
r1 = returnFunc(ctx, draft)
|
||||
} else {
|
||||
r1 = ret.Get(1).(bool)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(2).(func(context.Context, domain.MessageDraft) error); ok {
|
||||
r2 = returnFunc(ctx, draft)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
// MockRepository_Replay_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Replay'
|
||||
type MockRepository_Replay_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Replay is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - draft domain.MessageDraft
|
||||
func (_e *MockRepository_Expecter) Replay(ctx any, draft any) *MockRepository_Replay_Call {
|
||||
return &MockRepository_Replay_Call{Call: _e.mock.On("Replay", ctx, draft)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Replay_Call) Run(run func(ctx context.Context, draft domain.MessageDraft)) *MockRepository_Replay_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 domain.MessageDraft
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(domain.MessageDraft)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Replay_Call) Return(message domain.Message, b bool, err error) *MockRepository_Replay_Call {
|
||||
_c.Call.Return(message, b, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Replay_Call) RunAndReturn(run func(ctx context.Context, draft domain.MessageDraft) (domain.Message, bool, error)) *MockRepository_Replay_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
@@ -11,6 +11,11 @@ import (
|
||||
|
||||
type Repository interface {
|
||||
IsChatMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error)
|
||||
// Replay returns the stored message for a live Idempotency-Key. It must be
|
||||
// checked before attachment-state validation: a message that was accepted
|
||||
// while the attachment was queued keeps replaying as 201 even after the
|
||||
// attachment later fails.
|
||||
Replay(ctx context.Context, draft domain.MessageDraft) (domain.Message, bool, error)
|
||||
Create(ctx context.Context, draft domain.MessageDraft) (domain.Message, error)
|
||||
ListVisible(
|
||||
ctx context.Context,
|
||||
@@ -50,6 +55,15 @@ func (s *Service) Send(ctx context.Context, draft domain.MessageDraft) (domain.M
|
||||
return domain.Message{}, fmt.Errorf("%w: chat is not visible", domain.ErrNotFound)
|
||||
}
|
||||
|
||||
replayed, found, err := s.messages.Replay(ctx, draft)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("replay message: %w", err)
|
||||
}
|
||||
|
||||
if found {
|
||||
return replayed, nil
|
||||
}
|
||||
|
||||
if len(draft.AttachmentIDs) > 0 {
|
||||
if s.attachments == nil {
|
||||
return domain.Message{}, fmt.Errorf("attachment checker is not configured: %w", domain.ErrUnavailable)
|
||||
@@ -65,7 +79,10 @@ func (s *Service) Send(ctx context.Context, draft domain.MessageDraft) (domain.M
|
||||
}
|
||||
|
||||
for _, state := range states {
|
||||
if state.Status != domain.AttachmentReady {
|
||||
switch state.Status {
|
||||
case domain.AttachmentQueued, domain.AttachmentProcessing, domain.AttachmentReady:
|
||||
// Accepted work is linkable by contract.
|
||||
default:
|
||||
return domain.Message{}, fmt.Errorf(
|
||||
"%w: attachment %s is %s",
|
||||
domain.ErrConflict, state.ID, state.Status,
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
@@ -38,6 +39,8 @@ func (s *ServiceSuite) SetupTest() {
|
||||
s.caller = uuid.New()
|
||||
s.chatID = uuid.New()
|
||||
s.now = time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC)
|
||||
s.messages.EXPECT().Replay(mock.Anything, mock.Anything).
|
||||
Return(domain.Message{}, false, nil).Maybe()
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) draft() domain.MessageDraft {
|
||||
@@ -167,6 +170,40 @@ func (s *ServiceSuite) TestSendAcceptsReadyAttachment() {
|
||||
s.Equal([]uuid.UUID{attachmentID}, got.AttachmentIDs)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendReplaysBeforeAttachmentValidation() {
|
||||
draft := s.draft()
|
||||
draft.AttachmentIDs = []uuid.UUID{uuid.New()}
|
||||
stored := domain.Message{
|
||||
ID: uuid.New(), ChatID: draft.ChatID, SenderID: draft.SenderID,
|
||||
Text: draft.Text, AttachmentIDs: draft.AttachmentIDs, CreatedAt: s.now,
|
||||
}
|
||||
|
||||
// The stored key is authoritative even if the attachment has since failed;
|
||||
// ListStates must not be consulted on a replay.
|
||||
s.messages.ExpectedCalls = nil
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(true, nil).Once()
|
||||
s.messages.EXPECT().Replay(s.T().Context(), draft).Return(stored, true, nil).Once()
|
||||
|
||||
got, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(stored, got)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendRejectsFailedAttachment() {
|
||||
attachmentID := uuid.New()
|
||||
draft := s.draft()
|
||||
draft.AttachmentIDs = []uuid.UUID{attachmentID}
|
||||
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(true, nil).Once()
|
||||
s.attachments.EXPECT().
|
||||
ListStates(s.T().Context(), draft.ChatID, []uuid.UUID{attachmentID}).
|
||||
Return([]domain.AttachmentState{{ID: attachmentID, Status: domain.AttachmentFailed}}, nil).
|
||||
Once()
|
||||
|
||||
_, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrConflict)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendAllowsAttachmentOnlyMessage() {
|
||||
attachmentID := uuid.New()
|
||||
draft := s.draft()
|
||||
|
||||
Reference in New Issue
Block a user