forked from mtusi2026/mtusi2026lab7
733 lines
26 KiB
Go
733 lines
26 KiB
Go
package postgres_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/postgres"
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/reconcile"
|
|
)
|
|
|
|
type PostgresSuite struct {
|
|
suite.Suite
|
|
pool *pgxpool.Pool
|
|
store *postgres.Store
|
|
}
|
|
|
|
func TestPostgresSuite(t *testing.T) {
|
|
suite.Run(t, new(PostgresSuite))
|
|
}
|
|
|
|
func (s *PostgresSuite) SetupSuite() {
|
|
s.pool = openTestDatabase(s.T())
|
|
s.store = postgres.New(s.pool)
|
|
}
|
|
|
|
func (s *PostgresSuite) TearDownSuite() {
|
|
if s.pool != nil {
|
|
s.pool.Close()
|
|
}
|
|
}
|
|
|
|
func (s *PostgresSuite) TestConcurrentUsernameCreation() {
|
|
t := s.T()
|
|
store := s.store
|
|
username := "race_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16]
|
|
|
|
const workers = 8
|
|
results := make(chan error, workers)
|
|
var group sync.WaitGroup
|
|
for range workers {
|
|
group.Add(1)
|
|
go func() {
|
|
defer group.Done()
|
|
_, err := store.CreateUser(context.Background(), username, "Race user", "password-hash")
|
|
results <- err
|
|
}()
|
|
}
|
|
group.Wait()
|
|
close(results)
|
|
|
|
var created, conflicts int
|
|
for err := range results {
|
|
switch {
|
|
case err == nil:
|
|
created++
|
|
case errors.Is(err, postgres.ErrConflict):
|
|
conflicts++
|
|
default:
|
|
t.Fatalf("CreateUser() error = %v", err)
|
|
}
|
|
}
|
|
if created != 1 || conflicts != workers-1 {
|
|
t.Fatalf("created = %d, conflicts = %d", created, conflicts)
|
|
}
|
|
}
|
|
|
|
func (s *PostgresSuite) TestPasswordHashUpdateUsesCompareAndSwap() {
|
|
username := uniqueUsername()
|
|
user, err := s.store.CreateUser(context.Background(), username, "Password user", "old-hash")
|
|
s.Require().NoError(err)
|
|
|
|
updated, err := s.store.UpdatePasswordHash(context.Background(), user.ID, "other-hash", "new-hash")
|
|
s.Require().NoError(err)
|
|
s.False(updated)
|
|
|
|
updated, err = s.store.UpdatePasswordHash(context.Background(), user.ID, "old-hash", "new-hash")
|
|
s.Require().NoError(err)
|
|
s.True(updated)
|
|
updated, err = s.store.UpdatePasswordHash(context.Background(), user.ID, "old-hash", "stale-hash")
|
|
s.Require().NoError(err)
|
|
s.False(updated)
|
|
|
|
credentials, err := s.store.UserByUsername(context.Background(), username)
|
|
s.Require().NoError(err)
|
|
s.Equal("new-hash", credentials.PasswordHash)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestCreateChatRollsBackForUnknownMember() {
|
|
t := s.T()
|
|
pool := s.pool
|
|
store := s.store
|
|
owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash")
|
|
if err != nil {
|
|
t.Fatalf("CreateUser() error = %v", err)
|
|
}
|
|
|
|
before := chatCount(t, pool)
|
|
_, err = store.CreateChat(context.Background(), "Invalid chat", []string{owner.ID, uuid.NewString()}, owner.ID)
|
|
if !errors.Is(err, postgres.ErrInvalidMembers) {
|
|
t.Fatalf("CreateChat() error = %v, want ErrInvalidMembers", err)
|
|
}
|
|
if after := chatCount(t, pool); after != before {
|
|
t.Fatalf("chat count changed from %d to %d", before, after)
|
|
}
|
|
}
|
|
|
|
func (s *PostgresSuite) TestMessageIdempotencyReplayAndConflict() {
|
|
t := s.T()
|
|
pool := s.pool
|
|
store := s.store
|
|
owner, chatID := createOwnerAndChat(t, store)
|
|
key := uuid.NewString()
|
|
|
|
first, replay, err := store.CreateMessage(context.Background(), chatID, owner.ID, "same body", nil, key, 24*time.Hour)
|
|
if err != nil || replay {
|
|
t.Fatalf("first CreateMessage() = (%v, %t, %v)", first, replay, err)
|
|
}
|
|
second, replay, err := store.CreateMessage(context.Background(), chatID, owner.ID, "same body", []string{}, key, 24*time.Hour)
|
|
if err != nil || !replay {
|
|
t.Fatalf("replay CreateMessage() = (%v, %t, %v)", second, replay, err)
|
|
}
|
|
s.Equal(first, second)
|
|
if _, _, err := store.CreateMessage(context.Background(), chatID, owner.ID, "different body", nil, key, 24*time.Hour); !errors.Is(err, postgres.ErrIdempotencyConflict) {
|
|
t.Fatalf("conflicting CreateMessage() error = %v, want ErrIdempotencyConflict", err)
|
|
}
|
|
if count := messageCount(t, pool, chatID); count != 1 {
|
|
t.Fatalf("message count = %d, want 1", count)
|
|
}
|
|
}
|
|
|
|
func (s *PostgresSuite) TestAttachmentAccessAndMessageLinking() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
outsider, err := s.store.CreateUser(context.Background(), uniqueUsername(), "Outsider", "password-hash")
|
|
s.Require().NoError(err)
|
|
attachment := model.Attachment{
|
|
ID: uuid.NewString(), ChatID: chatID, OwnerID: owner.ID, Filename: "photo.png",
|
|
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("a", 64),
|
|
ObjectKey: "attachments/" + uuid.NewString() + "/original",
|
|
}
|
|
pending, err := s.store.CreatePendingAttachment(context.Background(), attachment)
|
|
s.Require().NoError(err)
|
|
created, err := s.store.MarkAttachmentReady(context.Background(), pending.ID, pending.ObjectKey)
|
|
s.Require().NoError(err)
|
|
s.Equal("ready", created.Status)
|
|
s.Empty(created.Variants)
|
|
s.Nil(created.ErrorCode)
|
|
|
|
visible, err := s.store.AttachmentForUser(context.Background(), created.ID, owner.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(created, visible)
|
|
_, err = s.store.AttachmentForUser(context.Background(), created.ID, outsider.ID)
|
|
s.ErrorIs(err, postgres.ErrNotFound)
|
|
|
|
message, replay, err := s.store.CreateMessage(
|
|
context.Background(), chatID, owner.ID, "", []string{created.ID}, uuid.NewString(), 24*time.Hour,
|
|
)
|
|
s.Require().NoError(err)
|
|
s.False(replay)
|
|
s.Equal([]string{created.ID}, message.AttachmentIDs)
|
|
|
|
messages, _, err := s.store.ListMessages(context.Background(), chatID, owner.ID, nil, 10)
|
|
s.Require().NoError(err)
|
|
s.Len(messages, 1)
|
|
s.Equal([]string{created.ID}, messages[0].AttachmentIDs)
|
|
referenced, err := s.store.AttachmentReferenced(context.Background(), created.ID)
|
|
s.Require().NoError(err)
|
|
s.True(referenced)
|
|
deleted, err := s.store.ClaimAttachmentForDeletion(context.Background(), created, time.Now().Add(time.Hour))
|
|
s.Require().NoError(err)
|
|
s.False(deleted)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
|
s.Equal(model.AttachmentQueued, attachment.Status)
|
|
|
|
events, err := s.store.ClaimOutboxEvents(context.Background(), 10, time.Minute)
|
|
s.Require().NoError(err)
|
|
s.Require().NotEmpty(events)
|
|
var event model.OutboxEvent
|
|
for _, candidate := range events {
|
|
if candidate.JobID == job.ID {
|
|
event = candidate
|
|
break
|
|
}
|
|
}
|
|
s.Require().NotEmpty(event.ID)
|
|
var message model.AttachmentJobMessage
|
|
s.Require().NoError(json.Unmarshal(event.Payload, &message))
|
|
s.Equal(job.ID, message.JobID)
|
|
s.Equal(attachment.ID, message.AttachmentID)
|
|
s.Equal(1, message.Attempt)
|
|
published, err := s.store.MarkOutboxPublished(context.Background(), event.ID, event.LeaseToken)
|
|
s.Require().NoError(err)
|
|
s.True(published)
|
|
|
|
_, replay, err := s.store.CreateMessage(
|
|
context.Background(), chatID, owner.ID, "queued attachment",
|
|
[]string{attachment.ID}, uuid.NewString(), 24*time.Hour,
|
|
)
|
|
s.Require().NoError(err)
|
|
s.False(replay)
|
|
|
|
_, err = s.store.ClaimAttachmentJob(
|
|
context.Background(), job.ID, uuid.NewString(), job.PipelineVersion, job.Attempt, "worker-invalid", time.Minute,
|
|
)
|
|
s.ErrorIs(err, model.ErrInvalidInput)
|
|
|
|
lease, err := s.store.ClaimAttachmentJob(
|
|
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
|
)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.AttachmentProcessing, lease.Attachment.Status)
|
|
s.NotEmpty(lease.Attachment.StagingKey)
|
|
|
|
variants := completedVariants(attachment.ID, 1)
|
|
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.JobMutationApplied, result)
|
|
result, err = s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.JobMutationAlreadyComplete, result)
|
|
|
|
metadata, variant, err := s.store.AttachmentVariantForUser(
|
|
context.Background(), attachment.ID, owner.ID, "thumbnail",
|
|
)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.AttachmentReady, metadata.Status)
|
|
s.Len(metadata.Variants, 3)
|
|
s.Equal(variants[1].ObjectKey, variant.ObjectKey)
|
|
listedKeys := make(map[string]struct{})
|
|
var after string
|
|
for {
|
|
listed, next, err := s.store.ListAttachments(context.Background(), after, 1000)
|
|
s.Require().NoError(err)
|
|
for _, candidate := range listed {
|
|
if candidate.ID == attachment.ID {
|
|
listedKeys[candidate.ObjectKey] = struct{}{}
|
|
}
|
|
}
|
|
if next == "" {
|
|
break
|
|
}
|
|
after = next
|
|
}
|
|
for _, candidate := range variants {
|
|
s.Contains(listedKeys, candidate.ObjectKey)
|
|
}
|
|
|
|
v2 := model.AttachmentJob{ID: uuid.NewString(), PipelineVersion: 2}
|
|
queuedV2, err := s.store.QueueAttachmentPipelineVersion(
|
|
context.Background(), attachment.ID, v2, "", "",
|
|
)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.AttachmentQueued, queuedV2.Status)
|
|
s.Equal(2, queuedV2.PipelineVersion)
|
|
result, err = s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.JobMutationStale, result)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestPublishedOutboxRetentionIsBatched() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
_, firstJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
|
_, secondJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
|
events, err := s.store.ClaimOutboxEvents(context.Background(), 1000, time.Minute)
|
|
s.Require().NoError(err)
|
|
|
|
eventIDs := make([]string, 0, 2)
|
|
for _, event := range events {
|
|
if event.JobID != firstJob.ID && event.JobID != secondJob.ID {
|
|
continue
|
|
}
|
|
published, err := s.store.MarkOutboxPublished(context.Background(), event.ID, event.LeaseToken)
|
|
s.Require().NoError(err)
|
|
s.True(published)
|
|
eventIDs = append(eventIDs, event.ID)
|
|
}
|
|
s.Require().Len(eventIDs, 2)
|
|
_, err = s.pool.Exec(
|
|
context.Background(),
|
|
`UPDATE outbox_events SET published_at = clock_timestamp() - INTERVAL '2 days' WHERE id IN ($1, $2)`,
|
|
eventIDs[0], eventIDs[1],
|
|
)
|
|
s.Require().NoError(err)
|
|
|
|
deleted, err := s.store.DeletePublishedOutboxEvents(context.Background(), time.Now().Add(-24*time.Hour), 1)
|
|
s.Require().NoError(err)
|
|
s.Equal(1, deleted)
|
|
deleted, err = s.store.DeletePublishedOutboxEvents(context.Background(), time.Now().Add(-24*time.Hour), 1)
|
|
s.Require().NoError(err)
|
|
s.Equal(1, deleted)
|
|
|
|
var remaining int
|
|
err = s.pool.QueryRow(
|
|
context.Background(), `SELECT count(*) FROM outbox_events WHERE id IN ($1, $2)`, eventIDs[0], eventIDs[1],
|
|
).Scan(&remaining)
|
|
s.Require().NoError(err)
|
|
s.Zero(remaining)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestFailedAttachmentReplayIsAudited() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
|
lease, err := s.store.ClaimAttachmentJob(
|
|
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
|
)
|
|
s.Require().NoError(err)
|
|
result, err := s.store.FailAttachmentJob(context.Background(), lease, "invalid_image")
|
|
s.Require().NoError(err)
|
|
s.Equal(model.JobMutationApplied, result)
|
|
|
|
_, _, err = s.store.CreateMessage(
|
|
context.Background(), chatID, owner.ID, "failed attachment",
|
|
[]string{attachment.ID}, uuid.NewString(), 24*time.Hour,
|
|
)
|
|
s.ErrorIs(err, postgres.ErrConflict)
|
|
metadata, err := s.store.AttachmentForUser(context.Background(), attachment.ID, owner.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.AttachmentFailed, metadata.Status)
|
|
s.Require().NotNil(metadata.ErrorCode)
|
|
s.Equal("invalid_image", *metadata.ErrorCode)
|
|
|
|
replayed, err := s.store.ReplayFailedAttachment(
|
|
context.Background(), attachment.ID, "operator@example", "input repaired",
|
|
)
|
|
s.Require().NoError(err)
|
|
s.Equal(2, replayed.Attempt)
|
|
s.Equal(model.AttachmentJobQueued, replayed.Status)
|
|
|
|
var auditCount int
|
|
err = s.pool.QueryRow(
|
|
context.Background(), `SELECT count(*) FROM attachment_replay_audit WHERE job_id = $1`, job.ID,
|
|
).Scan(&auditCount)
|
|
s.Require().NoError(err)
|
|
s.Equal(1, auditCount)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestExpiredLeaseRecoveryFencesOldWorker() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
|
lease, err := s.store.ClaimAttachmentJob(
|
|
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
|
)
|
|
s.Require().NoError(err)
|
|
_, err = s.pool.Exec(
|
|
context.Background(),
|
|
`UPDATE attachment_jobs SET lease_until = clock_timestamp() - INTERVAL '1 second' WHERE id = $1`,
|
|
job.ID,
|
|
)
|
|
s.Require().NoError(err)
|
|
|
|
recovered, err := s.store.RecoverExpiredAttachmentJobs(context.Background(), 10, 5, time.Now())
|
|
s.Require().NoError(err)
|
|
s.Equal(1, recovered.Requeued)
|
|
s.Zero(recovered.Failed)
|
|
current, err := s.store.AttachmentJob(context.Background(), job.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(2, current.Attempt)
|
|
s.Equal(model.AttachmentJobQueued, current.Status)
|
|
|
|
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, completedVariants(attachment.ID, 1))
|
|
s.Require().NoError(err)
|
|
s.Equal(model.JobMutationStale, result)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestCompleteAttachmentJobRejectsIncompleteOrMismatchedVariants() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
|
lease, err := s.store.ClaimAttachmentJob(
|
|
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
|
)
|
|
s.Require().NoError(err)
|
|
|
|
complete := completedVariants(attachment.ID, job.PipelineVersion)
|
|
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, complete[:3])
|
|
s.ErrorIs(err, model.ErrInvalidInput)
|
|
|
|
duplicate := append([]model.Variant(nil), complete...)
|
|
duplicate[3].Name = duplicate[2].Name
|
|
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, duplicate)
|
|
s.ErrorIs(err, model.ErrInvalidInput)
|
|
|
|
mismatched := append([]model.Variant(nil), complete...)
|
|
mismatched[3].PipelineVersion++
|
|
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, mismatched)
|
|
s.ErrorIs(err, model.ErrInvalidInput)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestAttachmentDeletionClaimChecksLifecycleUnderLock() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
|
ID: uuid.NewString(), ChatID: chatID, OwnerID: owner.ID, Filename: "pending.png",
|
|
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("c", 64),
|
|
ObjectKey: "attachments/" + uuid.NewString() + "/original",
|
|
})
|
|
s.Require().NoError(err)
|
|
|
|
claimed, err := s.store.ClaimAttachmentForDeletion(context.Background(), pending, time.Now().Add(time.Hour))
|
|
s.Require().NoError(err)
|
|
s.True(claimed)
|
|
deleted, err := s.store.DeleteAttachmentIfDeleting(context.Background(), pending.ID)
|
|
s.Require().NoError(err)
|
|
s.True(deleted)
|
|
|
|
pending, err = s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
|
ID: uuid.NewString(), ChatID: chatID, OwnerID: owner.ID, Filename: "completed.png",
|
|
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("d", 64),
|
|
ObjectKey: "attachments/" + uuid.NewString() + "/original",
|
|
})
|
|
s.Require().NoError(err)
|
|
ready, err := s.store.MarkAttachmentReady(context.Background(), pending.ID, pending.ObjectKey)
|
|
s.Require().NoError(err)
|
|
|
|
claimed, err = s.store.ClaimAttachmentForDeletion(context.Background(), pending, time.Now().Add(time.Hour))
|
|
s.Require().NoError(err)
|
|
s.False(claimed)
|
|
visible, err := s.store.AttachmentForUser(context.Background(), ready.ID, owner.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.AttachmentReady, visible.Status)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestReconciliationDryRunPreservesDeletingAttachment() {
|
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
|
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
|
ID: uuid.NewString(), ChatID: chatID, OwnerID: owner.ID, Filename: "deleting.png",
|
|
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("e", 64),
|
|
ObjectKey: "attachments/" + uuid.NewString() + "/original",
|
|
})
|
|
s.Require().NoError(err)
|
|
claimed, err := s.store.ClaimAttachmentForDeletion(context.Background(), pending, time.Now().Add(time.Hour))
|
|
s.Require().NoError(err)
|
|
s.True(claimed)
|
|
|
|
reconciler, err := reconcile.New(s.store, emptyObjectStore{}, reconcile.Config{
|
|
Prefix: "attachments/", PageSize: 1000, GraceTime: time.Minute,
|
|
UnreferencedRetention: time.Hour,
|
|
})
|
|
s.Require().NoError(err)
|
|
report, err := reconciler.Run(context.Background())
|
|
s.Require().NoError(err)
|
|
s.Zero(report.DeletedUnreferencedRows)
|
|
|
|
var status string
|
|
err = s.pool.QueryRow(context.Background(), "SELECT status FROM attachments WHERE id = $1", pending.ID).Scan(&status)
|
|
s.Require().NoError(err)
|
|
s.Equal(model.AttachmentDeleting, status)
|
|
}
|
|
|
|
type emptyObjectStore struct{}
|
|
|
|
func (emptyObjectStore) List(context.Context, string, string, int) ([]model.StoredObject, string, error) {
|
|
return nil, "", nil
|
|
}
|
|
|
|
func (emptyObjectStore) Delete(context.Context, string) error {
|
|
return errors.New("unexpected object deletion")
|
|
}
|
|
|
|
func (s *PostgresSuite) TestMessageRejectsAttachmentFromAnotherChat() {
|
|
owner, firstChatID := createOwnerAndChat(s.T(), s.store)
|
|
secondChat, err := s.store.CreateChat(context.Background(), "Second", []string{owner.ID}, owner.ID)
|
|
s.Require().NoError(err)
|
|
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
|
ID: uuid.NewString(), ChatID: firstChatID, OwnerID: owner.ID, Filename: "photo.png",
|
|
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("b", 64),
|
|
ObjectKey: "attachments/" + uuid.NewString() + "/original",
|
|
})
|
|
s.Require().NoError(err)
|
|
attachment, err := s.store.MarkAttachmentReady(context.Background(), pending.ID, pending.ObjectKey)
|
|
s.Require().NoError(err)
|
|
_, _, err = s.store.CreateMessage(
|
|
context.Background(), secondChat.ID, owner.ID, "wrong chat", []string{attachment.ID}, uuid.NewString(), 24*time.Hour,
|
|
)
|
|
s.ErrorIs(err, postgres.ErrNotFound)
|
|
}
|
|
|
|
func (s *PostgresSuite) TestConcurrentMessageIdempotency() {
|
|
t := s.T()
|
|
pool := s.pool
|
|
stores := []*postgres.Store{postgres.New(pool), postgres.New(pool)}
|
|
owner, chatID := createOwnerAndChat(t, stores[0])
|
|
key := uuid.NewString()
|
|
|
|
const workers = 8
|
|
type result struct {
|
|
message model.Message
|
|
replay bool
|
|
err error
|
|
}
|
|
results := make(chan result, workers)
|
|
var group sync.WaitGroup
|
|
for worker := range workers {
|
|
group.Add(1)
|
|
go func() {
|
|
defer group.Done()
|
|
message, replay, err := stores[worker%len(stores)].CreateMessage(
|
|
context.Background(), chatID, owner.ID, "concurrent body", nil, key, 24*time.Hour,
|
|
)
|
|
results <- result{message: message, replay: replay, err: err}
|
|
}()
|
|
}
|
|
group.Wait()
|
|
close(results)
|
|
|
|
var messageID string
|
|
var created int
|
|
for result := range results {
|
|
if result.err != nil {
|
|
t.Fatalf("CreateMessage() error = %v", result.err)
|
|
}
|
|
if messageID == "" {
|
|
messageID = result.message.ID
|
|
}
|
|
if result.message.ID != messageID {
|
|
t.Fatalf("message ID = %s, want %s", result.message.ID, messageID)
|
|
}
|
|
if !result.replay {
|
|
created++
|
|
}
|
|
}
|
|
if created != 1 {
|
|
t.Fatalf("new message results = %d, want 1", created)
|
|
}
|
|
if count := messageCount(t, pool, chatID); count != 1 {
|
|
t.Fatalf("message count = %d, want 1", count)
|
|
}
|
|
}
|
|
|
|
func (s *PostgresSuite) TestMessageIdempotencyScopeAndExpiry() {
|
|
t := s.T()
|
|
pool := s.pool
|
|
store := s.store
|
|
owner, chatID := createOwnerAndChat(t, store)
|
|
second, err := store.CreateUser(context.Background(), uniqueUsername(), "Second", "password-hash")
|
|
if err != nil {
|
|
t.Fatalf("CreateUser() error = %v", err)
|
|
}
|
|
chat, err := store.CreateChat(context.Background(), "Shared", []string{owner.ID, second.ID}, owner.ID)
|
|
if err != nil {
|
|
t.Fatalf("CreateChat() error = %v", err)
|
|
}
|
|
key := uuid.NewString()
|
|
|
|
if _, _, err := store.CreateMessage(context.Background(), chat.ID, owner.ID, "owner", nil, key, 24*time.Hour); err != nil {
|
|
t.Fatalf("owner CreateMessage() error = %v", err)
|
|
}
|
|
if _, _, err := store.CreateMessage(context.Background(), chat.ID, second.ID, "second", nil, key, 24*time.Hour); err != nil {
|
|
t.Fatalf("second user CreateMessage() error = %v", err)
|
|
}
|
|
if _, _, err := store.CreateMessage(context.Background(), chatID, owner.ID, "other chat", nil, key, 24*time.Hour); err != nil {
|
|
t.Fatalf("other chat CreateMessage() error = %v", err)
|
|
}
|
|
if _, err := pool.Exec(context.Background(), `
|
|
UPDATE message_idempotency
|
|
SET created_at = clock_timestamp() - INTERVAL '2 days',
|
|
expires_at = clock_timestamp() - INTERVAL '1 day'
|
|
WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chat.ID, key); err != nil {
|
|
t.Fatalf("expire idempotency key: %v", err)
|
|
}
|
|
message, replay, err := store.CreateMessage(context.Background(), chat.ID, owner.ID, "after expiry", nil, key, 24*time.Hour)
|
|
if err != nil || replay || message.Text != "after expiry" {
|
|
t.Fatalf("expired CreateMessage() = (%v, %t, %v)", message, replay, err)
|
|
}
|
|
}
|
|
|
|
func (s *PostgresSuite) TestExpiredIdempotencyRecordsAreCleanedOnNewMessages() {
|
|
t := s.T()
|
|
owner, chatID := createOwnerAndChat(t, s.store)
|
|
expiredKey := uuid.NewString()
|
|
if _, _, err := s.store.CreateMessage(context.Background(), chatID, owner.ID, "expired", nil, expiredKey, 24*time.Hour); err != nil {
|
|
t.Fatalf("create expired message: %v", err)
|
|
}
|
|
if _, err := s.pool.Exec(context.Background(), `
|
|
UPDATE message_idempotency
|
|
SET created_at = clock_timestamp() - INTERVAL '2 days',
|
|
expires_at = clock_timestamp() - INTERVAL '1 day'
|
|
WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chatID, expiredKey); err != nil {
|
|
t.Fatalf("expire idempotency key: %v", err)
|
|
}
|
|
if _, _, err := s.store.CreateMessage(
|
|
context.Background(), chatID, owner.ID, "new message", nil, uuid.NewString(), 24*time.Hour,
|
|
); err != nil {
|
|
t.Fatalf("create new message: %v", err)
|
|
}
|
|
|
|
var remaining int
|
|
if err := s.pool.QueryRow(context.Background(), `
|
|
SELECT count(*)
|
|
FROM message_idempotency
|
|
WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chatID, expiredKey).Scan(&remaining); err != nil {
|
|
t.Fatalf("count expired idempotency records: %v", err)
|
|
}
|
|
if remaining != 0 {
|
|
t.Fatalf("expired idempotency records = %d, want 0", remaining)
|
|
}
|
|
}
|
|
|
|
func createOwnerAndChat(t *testing.T, store *postgres.Store) (model.User, string) {
|
|
t.Helper()
|
|
owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash")
|
|
if err != nil {
|
|
t.Fatalf("CreateUser() error = %v", err)
|
|
}
|
|
chat, err := store.CreateChat(context.Background(), "Chat", []string{owner.ID}, owner.ID)
|
|
if err != nil {
|
|
t.Fatalf("CreateChat() error = %v", err)
|
|
}
|
|
return owner, chat.ID
|
|
}
|
|
|
|
func createQueuedAttachment(
|
|
t *testing.T,
|
|
store *postgres.Store,
|
|
ownerID string,
|
|
chatID string,
|
|
) (model.Attachment, model.AttachmentJob) {
|
|
t.Helper()
|
|
attachmentID := uuid.NewString()
|
|
stagingKey := "staging/" + attachmentID + "/original"
|
|
pending, err := store.CreatePendingAttachment(context.Background(), model.Attachment{
|
|
ID: attachmentID, ChatID: chatID, OwnerID: ownerID, Filename: "photo.png",
|
|
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("a", 64),
|
|
ObjectKey: stagingKey,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreatePendingAttachment() error = %v", err)
|
|
}
|
|
job := model.AttachmentJob{
|
|
ID: uuid.NewString(), AttachmentID: attachmentID, PipelineVersion: 1, Attempt: 1,
|
|
}
|
|
queued, err := store.QueueAttachmentWithJobAndOutbox(
|
|
context.Background(), pending.ID, stagingKey, job, "", "",
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("QueueAttachmentWithJobAndOutbox() error = %v", err)
|
|
}
|
|
return queued, job
|
|
}
|
|
|
|
func completedVariants(attachmentID string, pipelineVersion int) []model.Variant {
|
|
types := []struct {
|
|
name string
|
|
contentType string
|
|
extension string
|
|
}{
|
|
{name: "original", contentType: "image/png", extension: "png"},
|
|
{name: "thumbnail", contentType: "image/jpeg", extension: "jpg"},
|
|
{name: "crop", contentType: "image/jpeg", extension: "jpg"},
|
|
{name: "optimized", contentType: "image/webp", extension: "webp"},
|
|
}
|
|
variants := make([]model.Variant, 0, len(types))
|
|
for index, variant := range types {
|
|
variants = append(variants, model.Variant{
|
|
Name: variant.name, ContentType: variant.contentType, SizeBytes: int64(128 - index*16),
|
|
SHA256: strings.Repeat(string(rune('a'+index)), 64), Width: 320 - index*32, Height: 200 - index*20,
|
|
ObjectKey: fmt.Sprintf("attachments/%s/v%d/%s.%s", attachmentID, pipelineVersion, variant.name, variant.extension),
|
|
PipelineVersion: pipelineVersion,
|
|
})
|
|
}
|
|
return variants
|
|
}
|
|
|
|
func messageCount(t *testing.T, pool *pgxpool.Pool, chatID string) int {
|
|
t.Helper()
|
|
var count int
|
|
if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM messages WHERE chat_id = $1", chatID).Scan(&count); err != nil {
|
|
t.Fatalf("count messages: %v", err)
|
|
}
|
|
return count
|
|
}
|
|
|
|
func openTestDatabase(t *testing.T) *pgxpool.Pool {
|
|
t.Helper()
|
|
url := testSecret(t, "TEST_DATABASE_URL", "TEST_DATABASE_URL_FILE")
|
|
pool, err := database.Open(context.Background(), database.Config{
|
|
URL: url, MinConnections: 0, MaxConnections: 10,
|
|
MaxConnectionAge: time.Minute, MaxConnectionIdle: time.Minute,
|
|
HealthCheckPeriod: time.Minute, ConnectTimeout: 5 * time.Second,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("database.Open() error = %v", err)
|
|
}
|
|
if err := database.Migrate(context.Background(), pool, 30*time.Second); err != nil {
|
|
pool.Close()
|
|
t.Fatalf("database.Migrate() error = %v", err)
|
|
}
|
|
return pool
|
|
}
|
|
|
|
func testSecret(t *testing.T, valueName, fileName string) string {
|
|
t.Helper()
|
|
if value := strings.TrimSpace(os.Getenv(valueName)); value != "" {
|
|
return value
|
|
}
|
|
path := strings.TrimSpace(os.Getenv(fileName))
|
|
if path == "" {
|
|
t.Skipf("set %s or %s to run integration tests", valueName, fileName)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("read %s: %v", fileName, err)
|
|
}
|
|
return strings.TrimSpace(string(data))
|
|
}
|
|
|
|
func uniqueUsername() string {
|
|
return "u_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:20]
|
|
}
|
|
|
|
func chatCount(t *testing.T, pool *pgxpool.Pool) int {
|
|
t.Helper()
|
|
var count int
|
|
if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM chats").Scan(&count); err != nil {
|
|
t.Fatalf("count chats: %v", err)
|
|
}
|
|
return count
|
|
}
|