Разрешите отправку сообщения во время публикации вложения

This commit is contained in:
2026-11-20 10:54:34 +03:00
parent c6a5e9ee47
commit bdc072aab7
3 changed files with 90 additions and 9 deletions
+78
View File
@@ -16,6 +16,7 @@ import (
"messenger/internal/chat"
"messenger/internal/chat/store"
"messenger/internal/pipeline"
"messenger/internal/platform/apperr"
"messenger/internal/platform/ident"
)
@@ -79,6 +80,83 @@ func TestAppendOnceRejectsUnknownAttachmentWithoutClaim(t *testing.T) {
}
}
func TestAppendOnceAcceptsQueuedAttachmentDuringPublish(t *testing.T) {
f := newFixture(t)
alice := f.mustUser(t)
c := f.mustChat(t, alice)
id := f.attachment(t, c.ID, alice, "queued")
m := attachedMessage(t, c.ID, alice, id)
found, err := pipeline.NewStore(db.Pool).PublishOne(f.ctx, func(ctx context.Context, j pipeline.Job) error {
if j.AttachmentID != id {
t.Fatalf("publisher selected %s, want %s", j.AttachmentID, id)
}
// The callback holds the publisher transaction open until AppendOnce returns.
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
_, err := f.messages.AppendOnce(ctx, m, "during-publish", keyTTL)
return err
})
if err != nil || !found {
t.Fatalf("linking queued attachment while awaiting broker confirm: found=%v, err=%v", found, err)
}
assertCounts(t, 1, 1, 1)
}
func TestAppendOnceSeesFailureCommittedWhileValidating(t *testing.T) {
f := newFixture(t)
alice := f.mustUser(t)
c := f.mustChat(t, alice)
id := f.attachment(t, c.ID, alice, "queued")
if _, err := db.Pool.Exec(f.ctx, `UPDATE attachment_jobs SET state='processing',
lease_token=gen_random_uuid(),lease_until=now()+interval '1 hour' WHERE attachment_id=$1`, id); err != nil {
t.Fatal(err)
}
tx, err := db.Pool.Begin(f.ctx)
if err != nil {
t.Fatal(err)
}
defer tx.Rollback(f.ctx)
// Fail and lease recovery both take this lock before deciding the terminal state.
if _, err := tx.Exec(f.ctx, `SELECT job_id FROM attachment_jobs WHERE attachment_id=$1 FOR UPDATE`, id); err != nil {
t.Fatal(err)
}
if _, err := tx.Exec(f.ctx, `UPDATE attachment_jobs SET state='failed',error_code='retry_exhausted',
lease_token=NULL,lease_until=NULL WHERE attachment_id=$1`, id); err != nil {
t.Fatal(err)
}
m := attachedMessage(t, c.ID, alice, id)
ctx, cancel := context.WithTimeout(f.ctx, 5*time.Second)
defer cancel()
done := make(chan error, 1)
go func() {
_, err := f.messages.AppendOnce(ctx, m, "failing", keyTTL)
done <- err
}()
for {
var waiting bool
err := db.Pool.QueryRow(ctx, `SELECT EXISTS (
SELECT FROM pg_stat_activity WHERE $1 = ANY(pg_blocking_pids(pid)))`, tx.Conn().PgConn().PID()).Scan(&waiting)
if err != nil {
t.Fatal(err)
}
if waiting {
break
}
select {
case err := <-done:
t.Fatalf("message finished before the terminal transition: %v", err)
case <-time.After(10 * time.Millisecond):
}
}
if err := tx.Commit(ctx); err != nil {
t.Fatal(err)
}
if err := <-done; err == nil || apperr.KindOf(err) != apperr.KindConflict {
t.Fatalf("message with concurrently failed attachment: %v; want conflict", err)
}
assertCounts(t, 0, 0, 0)
}
func TestAppendOnceAttachmentValidationIsAtomic(t *testing.T) {
for _, status := range []string{"foreign", "failed", "mixed"} {
t.Run(status, func(t *testing.T) {
+6 -4
View File
@@ -18,6 +18,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/internal/attachment"
"messenger/internal/chat"
"messenger/internal/platform/apperr"
"messenger/internal/platform/postgres"
@@ -209,12 +210,13 @@ func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids [
return apperr.NotFound("attachment not found")
}
// A separate statement sees the latest committed transition after a lock wait.
// Every referenced attachment must have a current job.
rows, err = tx.Query(ctx, `SELECT status FROM attachment_states WHERE id=ANY($1::uuid[]) ORDER BY id FOR SHARE`, ids)
// Terminal failures take FOR UPDATE on the job or file. KEY SHARE orders
// validation with those transitions while allowing an outbox publish in flight.
rows, err = tx.Query(ctx, `SELECT status FROM attachment_states WHERE id=ANY($1::uuid[]) ORDER BY id FOR KEY SHARE`, ids)
if err != nil {
return err
}
states, err := pgx.CollectRows(rows, pgx.RowTo[string])
states, err := pgx.CollectRows(rows, pgx.RowTo[attachment.FileStatus])
if err != nil {
return err
}
@@ -223,7 +225,7 @@ func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids [
}
for _, state := range states {
switch state {
case "queued", "processing", "ready":
case attachment.FileQueued, attachment.FileProcessing, attachment.FileReady:
default:
return apperr.Conflict("attachment processing failed")
}