lab4 done
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
|
||||
)
|
||||
|
||||
func (s *Storage) CreateChat(ctx context.Context, title string, members []string) (domain.Chat, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return domain.Chat{}, dbError(err)
|
||||
}
|
||||
defer tx.Rollback(context.WithoutCancel(ctx))
|
||||
chat := domain.Chat{ID: uuid.NewString(), Title: title, MemberIDs: append([]string(nil), members...)}
|
||||
err = tx.QueryRow(ctx, `INSERT INTO chats(id,title) VALUES($1,$2) RETURNING created_at`, chat.ID, title).Scan(&chat.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.Chat{}, dbError(err)
|
||||
}
|
||||
for i, member := range members {
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id,ordinal) VALUES($1,$2,$3)`, chat.ID, member, i); err != nil {
|
||||
return domain.Chat{}, dbError(err)
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return domain.Chat{}, dbError(err)
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
const chatColumns = `c.id::text,c.title,c.created_at,ARRAY(SELECT m.user_id::text FROM chat_members m WHERE m.chat_id=c.id ORDER BY m.ordinal)`
|
||||
|
||||
func (s *Storage) ChatForUser(ctx context.Context, chatID, userID string) (domain.Chat, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
var chat domain.Chat
|
||||
err := s.pool.QueryRow(ctx, `SELECT `+chatColumns+` FROM chats c
|
||||
WHERE c.id=$1 AND EXISTS(SELECT 1 FROM chat_members m WHERE m.chat_id=c.id AND m.user_id=$2)`, chatID, userID).
|
||||
Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return chat, chatusecase.ErrChatNotFound
|
||||
}
|
||||
return chat, dbError(err)
|
||||
}
|
||||
|
||||
func (s *Storage) ListChats(ctx context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
query := `SELECT ` + chatColumns + ` FROM chats c JOIN chat_members member ON member.chat_id=c.id
|
||||
WHERE member.user_id=$1`
|
||||
args := []any{userID, limit}
|
||||
if after != nil {
|
||||
query += ` AND (c.created_at,c.id)>($3,$4)`
|
||||
args = append(args, after.CreatedAt, after.ID)
|
||||
}
|
||||
query += ` ORDER BY c.created_at,c.id LIMIT $2`
|
||||
rows, err := s.pool.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, dbError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
chats := make([]domain.Chat, 0)
|
||||
for rows.Next() {
|
||||
var chat domain.Chat
|
||||
if err = rows.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs); err != nil {
|
||||
return nil, dbError(err)
|
||||
}
|
||||
chats = append(chats, chat)
|
||||
}
|
||||
return chats, dbError(rows.Err())
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type fakeObject struct {
|
||||
data []byte
|
||||
modified time.Time
|
||||
}
|
||||
type fakeS3 struct {
|
||||
mu sync.Mutex
|
||||
objects map[string]fakeObject
|
||||
failPUT bool
|
||||
}
|
||||
|
||||
func (f *fakeS3) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
key := strings.TrimPrefix(r.URL.Path, "/attachments/")
|
||||
if r.Method == "GET" && r.URL.Path == "/attachments/" || r.Method == "GET" && r.URL.Path == "/attachments" {
|
||||
type item struct {
|
||||
Key string
|
||||
LastModified string
|
||||
ETag string
|
||||
Size int
|
||||
}
|
||||
type listing struct {
|
||||
XMLName xml.Name `xml:"ListBucketResult"`
|
||||
Xmlns string `xml:"xmlns,attr"`
|
||||
Name string
|
||||
Prefix string
|
||||
MaxKeys int
|
||||
IsTruncated bool
|
||||
Contents []item
|
||||
}
|
||||
l := listing{Xmlns: "http://s3.amazonaws.com/doc/2006-03-01/", Name: "attachments", Prefix: r.URL.Query().Get("prefix"), MaxKeys: 1000}
|
||||
for k, v := range f.objects {
|
||||
if strings.HasPrefix(k, l.Prefix) {
|
||||
l.Contents = append(l.Contents, item{Key: k, LastModified: v.modified.UTC().Format("2006-01-02T15:04:05.000Z"), ETag: `"abc"`, Size: len(v.data)})
|
||||
}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
xml.NewEncoder(w).Encode(l)
|
||||
return
|
||||
}
|
||||
if r.Method == "PUT" {
|
||||
if f.failPUT {
|
||||
w.WriteHeader(503)
|
||||
fmt.Fprint(w, `<Error><Code>ServiceUnavailable</Code></Error>`)
|
||||
return
|
||||
}
|
||||
var data []byte
|
||||
var err error
|
||||
if strings.Contains(r.Header.Get("Content-Encoding"), "aws-chunked") || strings.HasPrefix(r.Header.Get("X-Amz-Content-Sha256"), "STREAMING-") {
|
||||
reader := bufio.NewReader(r.Body)
|
||||
for {
|
||||
line, e := reader.ReadString('\n')
|
||||
if e != nil {
|
||||
err = e
|
||||
break
|
||||
}
|
||||
size, e := strconv.ParseInt(strings.Split(strings.TrimSpace(line), ";")[0], 16, 64)
|
||||
if e != nil {
|
||||
err = e
|
||||
break
|
||||
}
|
||||
if size == 0 {
|
||||
break
|
||||
}
|
||||
chunk := make([]byte, size)
|
||||
if _, e = io.ReadFull(reader, chunk); e != nil {
|
||||
err = e
|
||||
break
|
||||
}
|
||||
data = append(data, chunk...)
|
||||
if _, e = reader.Discard(2); e != nil {
|
||||
err = e
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data, err = io.ReadAll(r.Body)
|
||||
}
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
f.objects[key] = fakeObject{data: data, modified: time.Now().UTC()}
|
||||
w.Header().Set("ETag", `"900150983cd24fb0d6963f7d28e17f72"`)
|
||||
w.WriteHeader(200)
|
||||
return
|
||||
}
|
||||
obj, ok := f.objects[key]
|
||||
if !ok {
|
||||
w.WriteHeader(404)
|
||||
if r.Method != "HEAD" {
|
||||
fmt.Fprint(w, `<Error><Code>NoSuchKey</Code></Error>`)
|
||||
}
|
||||
return
|
||||
}
|
||||
if r.Method == "DELETE" {
|
||||
delete(f.objects, key)
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Last-Modified", obj.modified.UTC().Format(http.TimeFormat))
|
||||
w.Header().Set("ETag", `"abc"`)
|
||||
w.Header().Set("Content-Length", fmt.Sprint(len(obj.data)))
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
if r.Method == "GET" {
|
||||
w.Write(obj.data)
|
||||
}
|
||||
}
|
||||
func lab4Backend(t *testing.T, pool *pgxpool.Pool, repo *postgres.Storage, user domain.User, chat domain.Chat) {
|
||||
ctx := context.Background()
|
||||
fake := &fakeS3{objects: map[string]fakeObject{}}
|
||||
server := httptest.NewServer(fake)
|
||||
defer server.Close()
|
||||
client, err := minio.New(strings.TrimPrefix(server.URL, "http://"), &minio.Options{Creds: credentials.NewStaticV4("test-key", "test-secret", ""), Region: "us-east-1", MaxRetries: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := attachment.New(pool, &attachment.Store{Client: client, Bucket: "attachments"})
|
||||
var imageBytes bytes.Buffer
|
||||
if err = png.Encode(&imageBytes, image.NewRGBA(image.Rect(0, 0, 10, 8))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
upload := func(name string) attachment.Attachment {
|
||||
t.Helper()
|
||||
a, e := service.Upload(ctx, chat.ID, user.ID, name, "image/png", bytes.NewReader(imageBytes.Bytes()))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return a
|
||||
}
|
||||
first := upload("first.png")
|
||||
second := upload("second.png")
|
||||
requireCode := func(err error, want domain.ErrorCode) {
|
||||
t.Helper()
|
||||
e, ok := domain.AsApplicationError(err)
|
||||
if !ok || e.Code != want {
|
||||
t.Fatalf("expected %s, got %v", want, err)
|
||||
}
|
||||
}
|
||||
outside, err := repo.CreateUser(ctx, "attachment_outsider", "Outside", "hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.Get(ctx, first.ID, outside.ID)
|
||||
requireCode(err, domain.ErrorNotFound)
|
||||
obj, err := service.Content(ctx, first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := io.ReadAll(obj)
|
||||
obj.Close()
|
||||
if err != nil || !bytes.Equal(data, imageBytes.Bytes()) {
|
||||
t.Fatalf("original mismatch: %v", err)
|
||||
}
|
||||
otherChat, err := repo.CreateChat(ctx, "Other", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = repo.CreateMessageWithAttachments(ctx, otherChat.ID, user.ID, "", "cross-chat", []string{first.ID})
|
||||
requireCode(err, domain.ErrorNotFound)
|
||||
_, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "unknown", []string{uuid.NewString()})
|
||||
requireCode(err, domain.ErrorNotFound)
|
||||
sent, err := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{first.ID, second.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replay, err := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{first.ID, second.ID})
|
||||
if err != nil || sent.ID != replay.ID {
|
||||
t.Fatalf("replay failed: %v", err)
|
||||
}
|
||||
_, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{second.ID, first.ID})
|
||||
requireCode(err, domain.ErrorConflict)
|
||||
textOnly, err := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "text", "empty-equivalent", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replay, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "text", "empty-equivalent", []string{})
|
||||
if err != nil || textOnly.ID != replay.ID {
|
||||
t.Fatalf("absent/empty differs: %v", err)
|
||||
}
|
||||
|
||||
const callers = 12
|
||||
outcomes := make(chan domain.Message, callers)
|
||||
failures := make(chan error, callers)
|
||||
var concurrent sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
for i := 0; i < callers; i++ {
|
||||
concurrent.Add(1)
|
||||
go func() {
|
||||
defer concurrent.Done()
|
||||
<-start
|
||||
m, e := repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "concurrent-attachments", []string{first.ID, second.ID})
|
||||
if e != nil {
|
||||
failures <- e
|
||||
} else {
|
||||
outcomes <- m
|
||||
}
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
concurrent.Wait()
|
||||
close(outcomes)
|
||||
close(failures)
|
||||
for e := range failures {
|
||||
t.Fatal(e)
|
||||
}
|
||||
identity := ""
|
||||
for m := range outcomes {
|
||||
if identity == "" {
|
||||
identity = m.ID
|
||||
}
|
||||
if m.ID != identity {
|
||||
t.Fatal("duplicate concurrent attachment message")
|
||||
}
|
||||
}
|
||||
var effects int
|
||||
if err = pool.QueryRow(ctx, `SELECT count(*) FROM messages WHERE chat_id=$1 AND idempotency_key='concurrent-attachments'`, chat.ID).Scan(&effects); err != nil || effects != 1 {
|
||||
t.Fatalf("concurrent effects=%d %v", effects, err)
|
||||
}
|
||||
t.Log("ACL, original bytes, cross-chat attachment rejection, ordered/concurrent idempotency and absent=[] passed")
|
||||
|
||||
old := time.Now().Add(-3 * time.Minute)
|
||||
orphan := "original/" + uuid.NewString()
|
||||
fresh := "original/" + uuid.NewString()
|
||||
locked := "original/" + uuid.NewString()
|
||||
fake.mu.Lock()
|
||||
delete(fake.objects, first.Key)
|
||||
v := fake.objects[second.Key]
|
||||
v.modified = old
|
||||
fake.objects[second.Key] = v
|
||||
fake.objects[orphan] = fakeObject{data: []byte("orphan"), modified: old}
|
||||
fake.objects[fresh] = fakeObject{data: []byte("fresh"), modified: time.Now()}
|
||||
fake.objects[locked] = fakeObject{data: []byte("locked"), modified: old}
|
||||
fake.mu.Unlock()
|
||||
if _, err = pool.Exec(ctx, `UPDATE attachments SET created_at=$1 WHERE id=ANY($2::uuid[])`, old, []string{first.ID, second.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hold, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer hold.Rollback(ctx)
|
||||
if _, err = hold.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, locked); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := service.Reconcile(ctx, false, 90*time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dry.Orphans != 1 || dry.Missing != 1 || dry.ReferencedMissing != 1 || dry.LockedSkipped != 1 || dry.FreshSkipped < 1 || dry.Deleted != 0 || dry.MarkedFailed != 0 {
|
||||
t.Fatalf("bad dry-run %+v", dry)
|
||||
}
|
||||
var status string
|
||||
if err = pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id=$1`, first.ID).Scan(&status); err != nil || status != "ready" {
|
||||
t.Fatalf("dry-run mutated metadata %s %v", status, err)
|
||||
}
|
||||
applied, err := service.Reconcile(ctx, true, 90*time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if applied.Deleted != 1 || applied.MarkedFailed != 1 {
|
||||
t.Fatalf("bad apply %+v", applied)
|
||||
}
|
||||
fake.mu.Lock()
|
||||
_, hasOrphan := fake.objects[orphan]
|
||||
_, hasFresh := fake.objects[fresh]
|
||||
_, hasLinked := fake.objects[second.Key]
|
||||
_, hasLocked := fake.objects[locked]
|
||||
fake.mu.Unlock()
|
||||
if hasOrphan || !hasFresh || !hasLinked || !hasLocked {
|
||||
t.Fatal("unsafe object cleanup")
|
||||
}
|
||||
_, err = service.Get(ctx, first.ID, user.ID)
|
||||
requireCode(err, domain.ErrorConflict)
|
||||
_, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "failed-attachment", []string{first.ID})
|
||||
requireCode(err, domain.ErrorConflict)
|
||||
replay, err = repo.CreateMessageWithAttachments(ctx, chat.ID, user.ID, "", "ordered", []string{first.ID, second.ID})
|
||||
if err != nil || replay.ID != sent.ID {
|
||||
t.Fatalf("historical replay changed after missing: %v", err)
|
||||
}
|
||||
repeated, err := service.Reconcile(ctx, true, 90*time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if repeated.Deleted != 0 || repeated.MarkedFailed != 0 || repeated.Orphans != 0 || repeated.Missing != 0 {
|
||||
t.Fatalf("not repeatable %+v", repeated)
|
||||
}
|
||||
t.Logf("dry-run=%+v apply=%+v repeat=%+v; fresh, locked and referenced objects retained", dry, applied, repeated)
|
||||
}
|
||||
|
||||
func TestS3WireFixture(t *testing.T) {
|
||||
fake := &fakeS3{objects: map[string]fakeObject{}}
|
||||
server := httptest.NewServer(fake)
|
||||
defer server.Close()
|
||||
client, err := minio.New(strings.TrimPrefix(server.URL, "http://"), &minio.Options{Creds: credentials.NewStaticV4("test-key", "test-secret", ""), Region: "us-east-1", MaxRetries: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload := []byte("exact object bytes")
|
||||
_, err = client.PutObject(context.Background(), "attachments", "original/test", bytes.NewReader(payload), int64(len(payload)), minio.PutObjectOptions{ContentType: "image/png", DisableMultipart: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obj, err := client.GetObject(context.Background(), "attachments", "original/test", minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer obj.Close()
|
||||
got, err := io.ReadAll(obj)
|
||||
if err != nil || !bytes.Equal(got, payload) {
|
||||
t.Fatalf("wire fixture did not store original: %q %v", got, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func (s *Storage) CreateMessage(ctx context.Context, chatID, senderID, text, key string) (domain.Message, error) {
|
||||
return s.CreateMessageWithAttachments(ctx, chatID, senderID, text, key, nil)
|
||||
}
|
||||
|
||||
func (s *Storage) CreateMessageWithAttachments(ctx context.Context, chatID, senderID, text, key string, ids []string) (domain.Message, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
if ids == nil {
|
||||
ids = []string{}
|
||||
}
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbError(err)
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
// Serialize this idempotency scope before validating attachments. A replay remains a
|
||||
// replay even when reconciliation has since marked an original as missing.
|
||||
if key != "" {
|
||||
_, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, senderID+":"+chatID+":"+key)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbError(err)
|
||||
}
|
||||
old := domain.Message{ChatID: chatID, SenderID: senderID}
|
||||
err = tx.QueryRow(ctx, `SELECT id::text,text,attachment_ids::text[],created_at FROM messages WHERE sender_id=$1 AND chat_id=$2 AND idempotency_key=$3`, senderID, chatID, key).Scan(&old.ID, &old.Text, &old.AttachmentIDs, &old.CreatedAt)
|
||||
if err == nil {
|
||||
if old.Text != text || !slices.Equal(old.AttachmentIDs, ids) {
|
||||
return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Idempotency-Key уже использован с другим телом")
|
||||
}
|
||||
return old, nil
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.Message{}, dbError(err)
|
||||
}
|
||||
}
|
||||
// Lock each metadata row against reconciler updates until the link is committed.
|
||||
for _, id := range ids {
|
||||
var status string
|
||||
err = tx.QueryRow(ctx, `SELECT status FROM attachments WHERE id=$1 AND chat_id=$2 FOR SHARE`, id, chatID).Scan(&status)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.Message{}, domain.NewApplicationError(domain.ErrorNotFound, "Объект не найден")
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Message{}, dbError(err)
|
||||
}
|
||||
if status != "ready" {
|
||||
return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Вложение не готово")
|
||||
}
|
||||
}
|
||||
m := domain.Message{ID: uuid.NewString(), ChatID: chatID, SenderID: senderID, Text: text, AttachmentIDs: ids}
|
||||
var storedKey *string
|
||||
if key != "" {
|
||||
storedKey = &key
|
||||
}
|
||||
err = tx.QueryRow(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,idempotency_key,attachment_ids) VALUES($1,$2,$3,$4,$5,$6) RETURNING created_at`, m.ID, chatID, senderID, text, storedKey, ids).Scan(&m.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.Message{}, dbError(err)
|
||||
}
|
||||
return m, dbError(tx.Commit(ctx))
|
||||
}
|
||||
|
||||
func (s *Storage) ListMessages(ctx context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
query := `SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.attachment_ids::text[],m.created_at FROM messages m WHERE m.chat_id=$1`
|
||||
args := []any{chatID, limit}
|
||||
if after != nil {
|
||||
query += ` AND (m.created_at,m.id)>($3,$4)`
|
||||
args = append(args, after.CreatedAt, after.ID)
|
||||
}
|
||||
query += ` ORDER BY m.created_at,m.id LIMIT $2`
|
||||
rows, err := s.pool.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, dbError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
result := []domain.Message{}
|
||||
for rows.Next() {
|
||||
var m domain.Message
|
||||
if err = rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.AttachmentIDs, &m.CreatedAt); err != nil {
|
||||
return nil, dbError(err)
|
||||
}
|
||||
result = append(result, m)
|
||||
}
|
||||
return result, dbError(rows.Err())
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
pool *pgxpool.Pool
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
var _ chatusecase.Repository = (*Storage)(nil)
|
||||
|
||||
func New(pool *pgxpool.Pool, timeout time.Duration) *Storage {
|
||||
return &Storage{pool: pool, timeout: timeout}
|
||||
}
|
||||
|
||||
func (s *Storage) Ready(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
return dbError(s.pool.Ping(ctx))
|
||||
}
|
||||
|
||||
func dbError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
switch pgErr.Code {
|
||||
case "23505":
|
||||
return chatusecase.ErrUsernameTaken
|
||||
case "23503":
|
||||
return chatusecase.ErrUserNotFound
|
||||
}
|
||||
log.Printf("postgres operation failed: sqlstate=%s", pgErr.Code)
|
||||
} else if !errors.Is(err, pgx.ErrNoRows) {
|
||||
log.Print("postgres operation failed")
|
||||
}
|
||||
return domain.NewApplicationError(domain.ErrorUnavailable, "Хранилище недоступно")
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config"
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres"
|
||||
chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
|
||||
)
|
||||
|
||||
func TestIntegration(t *testing.T) {
|
||||
if os.Getenv("INTEGRATION_TESTS") != "1" {
|
||||
t.Skip("run make test-integration against the Compose stack")
|
||||
}
|
||||
ctx := context.Background()
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pc, err := pgxpool.ParseConfig("sslmode=disable")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pc.ConnConfig.Host = cfg.PostgresHost
|
||||
pc.ConnConfig.Port = cfg.PostgresPort
|
||||
pc.ConnConfig.User = cfg.PostgresUser
|
||||
pc.ConnConfig.Database = cfg.PostgresDatabase
|
||||
pc.ConnConfig.Password = cfg.PostgresPassword
|
||||
pc.ConnConfig.RuntimeParams["timezone"] = "UTC"
|
||||
pc.MaxConns = 4
|
||||
admin, err := pgxpool.NewWithConfig(ctx, pc.Copy())
|
||||
if err != nil {
|
||||
t.Fatal("connect failed")
|
||||
}
|
||||
defer admin.Close()
|
||||
// Identifier is generated by the test, never from an API request.
|
||||
schema := "test_" + uuid.New().String()[:8]
|
||||
if _, err = admin.Exec(ctx, `CREATE SCHEMA `+schema); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer admin.Exec(ctx, `DROP SCHEMA `+schema+` CASCADE`)
|
||||
pc.ConnConfig.RuntimeParams["search_path"] = schema
|
||||
pool, err := pgxpool.NewWithConfig(ctx, pc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
runGoose := func(t *testing.T, command string) {
|
||||
t.Helper()
|
||||
migrationDir := os.Getenv("GOOSE_MIGRATION_DIR")
|
||||
if migrationDir == "" {
|
||||
migrationDir, err = filepath.Abs("../../../../migrations")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
connection := url.URL{Scheme: "postgres", User: url.User(cfg.PostgresUser), Host: net.JoinHostPort(cfg.PostgresHost, strconv.Itoa(int(cfg.PostgresPort))), Path: cfg.PostgresDatabase}
|
||||
connection.RawQuery = url.Values{"sslmode": {"disable"}, "search_path": {schema}, "connect_timeout": {"5"}}.Encode()
|
||||
commandCtx, cancel := context.WithTimeout(ctx, 45*time.Second)
|
||||
defer cancel()
|
||||
commandExec := exec.CommandContext(commandCtx, "goose", "-env", "/dev/null", "-dir", migrationDir, "-timeout", "30s", command)
|
||||
commandExec.Env = append(os.Environ(), "GOOSE_DRIVER=postgres", "GOOSE_DBSTRING="+connection.String(), "PGPASSWORD="+cfg.PostgresPassword)
|
||||
output, err := commandExec.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("goose %s: %v: %s", command, err, strings.ReplaceAll(string(output), cfg.PostgresPassword, "[redacted]"))
|
||||
}
|
||||
t.Logf("goose %s: %s", command, output)
|
||||
}
|
||||
runGoose(t, "up")
|
||||
runGoose(t, "up")
|
||||
var versions int
|
||||
if err = pool.QueryRow(ctx, `SELECT count(*) FROM goose_db_version WHERE version_id=1 AND is_applied`).Scan(&versions); err != nil || versions != 1 {
|
||||
t.Fatalf("migration versions=%d error=%v", versions, err)
|
||||
}
|
||||
repo := postgres.New(pool, 2*time.Second)
|
||||
t.Run("registration_race", func(t *testing.T) {
|
||||
const count = 12
|
||||
results := make(chan error, count)
|
||||
var wg sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
for i := 0; i < count; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
_, err := repo.CreateUser(ctx, "same_username", "Same", "test-hash")
|
||||
results <- err
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(results)
|
||||
successes, conflicts := 0, 0
|
||||
for err := range results {
|
||||
if err == nil {
|
||||
successes++
|
||||
} else if errors.Is(err, chatusecase.ErrUsernameTaken) {
|
||||
conflicts++
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if successes != 1 || conflicts != count-1 {
|
||||
t.Fatalf("successes=%d conflicts=%d", successes, conflicts)
|
||||
}
|
||||
t.Logf("12 concurrent INSERTs: %d success, %d unique conflicts", successes, conflicts)
|
||||
})
|
||||
user, err := repo.CreateUser(ctx, "history_owner", "Owner", "test-hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Run("rollback", func(t *testing.T) {
|
||||
_, err := repo.CreateChat(ctx, "must rollback", []string{user.ID, uuid.NewString()})
|
||||
if !errors.Is(err, chatusecase.ErrUserNotFound) {
|
||||
t.Fatalf("unexpected error %v", err)
|
||||
}
|
||||
var chats, members int
|
||||
if err = pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM chats),(SELECT count(*) FROM chat_members)`).Scan(&chats, &members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 0 || members != 0 {
|
||||
t.Fatalf("partial transaction: chats=%d members=%d", chats, members)
|
||||
}
|
||||
t.Log("failed second member INSERT left 0 chats and 0 members")
|
||||
})
|
||||
chat, err := repo.CreateChat(ctx, "History", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Run("lab4_attachments_and_reconciliation", func(t *testing.T) {
|
||||
isolated, e := repo.CreateChat(ctx, "Lab4", []string{user.ID})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
lab4Backend(t, pool, repo, user, isolated)
|
||||
})
|
||||
_, err = pool.Exec(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at)
|
||||
SELECT md5(i::text)::uuid,$1,$2,'synthetic message '||i,
|
||||
timestamptz '2026-01-01 00:00:00+00' + (i / 10) * interval '1 millisecond'
|
||||
FROM generate_series(1,10000) i`, chat.ID, user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = pool.Exec(ctx, `ANALYZE messages`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Run("keyset_and_explain", func(t *testing.T) {
|
||||
seen := make(map[string]bool)
|
||||
var after *domain.Position
|
||||
for {
|
||||
page, err := repo.ListMessages(ctx, chat.ID, after, 97)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page) == 0 {
|
||||
break
|
||||
}
|
||||
for _, message := range page {
|
||||
if seen[message.ID] {
|
||||
t.Fatal("duplicate on page boundary")
|
||||
}
|
||||
seen[message.ID] = true
|
||||
}
|
||||
last := page[len(page)-1]
|
||||
after = &domain.Position{CreatedAt: last.CreatedAt, ID: last.ID}
|
||||
}
|
||||
if len(seen) != 10000 {
|
||||
t.Fatalf("history count=%d", len(seen))
|
||||
}
|
||||
rows, err := pool.Query(ctx, `EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.created_at FROM messages m
|
||||
WHERE m.chat_id=$1 AND (m.created_at,m.id)>($2,$3) ORDER BY m.created_at,m.id LIMIT $4`,
|
||||
chat.ID, time.Date(2026, 1, 1, 0, 0, 0, 900000000, time.UTC), "00000000-0000-0000-0000-000000000000", 51)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
t.Log("EXPLAIN on 10,000 messages, cursor near the end (9,000):")
|
||||
for rows.Next() {
|
||||
var line string
|
||||
if err = rows.Scan(&line); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(line)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("keyset traversal returned 10,000 distinct messages, including equal timestamps")
|
||||
})
|
||||
t.Run("idempotency_race", func(t *testing.T) {
|
||||
isolated, err := repo.CreateChat(ctx, "Idempotency", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const count = 16
|
||||
results := make(chan domain.Message, count)
|
||||
failures := make(chan error, count)
|
||||
start := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < count; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
message, err := repo.CreateMessage(ctx, isolated.ID, user.ID, "exactly once", "shared-key")
|
||||
if err != nil {
|
||||
failures <- err
|
||||
return
|
||||
}
|
||||
results <- message
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(results)
|
||||
close(failures)
|
||||
for err := range failures {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var first domain.Message
|
||||
for message := range results {
|
||||
if first.ID == "" {
|
||||
first = message
|
||||
}
|
||||
if message.ID != first.ID || !message.CreatedAt.Equal(first.CreatedAt) || message.Text != first.Text {
|
||||
t.Fatal("concurrent replay changed result")
|
||||
}
|
||||
}
|
||||
var rows int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM messages WHERE chat_id=$1`, isolated.ID).Scan(&rows); err != nil || rows != 1 {
|
||||
t.Fatalf("expected one durable effect, got %d: %v", rows, err)
|
||||
}
|
||||
_, err = repo.CreateMessage(ctx, isolated.ID, user.ID, "different", "shared-key")
|
||||
applicationError, ok := domain.AsApplicationError(err)
|
||||
if !ok || applicationError.Code != domain.ErrorConflict {
|
||||
t.Fatalf("expected conflict, got %v", err)
|
||||
}
|
||||
t.Log("16 concurrent PostgreSQL requests: one effect, identical replay, changed body conflicts")
|
||||
})
|
||||
t.Run("goose_legacy_and_down", func(t *testing.T) {
|
||||
// This is the disposable test schema, never the application's public schema.
|
||||
const digestSQL = `SELECT md5(string_agg(ROW(id,chat_id,sender_id,text,created_at)::text,',' ORDER BY id)) FROM messages`
|
||||
var before, after string
|
||||
if err = pool.QueryRow(ctx, digestSQL).Scan(&before); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = pool.Exec(ctx, `DROP TABLE goose_db_version;
|
||||
CREATE TABLE schema_migrations(version text PRIMARY KEY,applied_at timestamptz NOT NULL DEFAULT now());
|
||||
INSERT INTO schema_migrations(version) VALUES('001_initial.sql')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runGoose(t, "up")
|
||||
runGoose(t, "up")
|
||||
if err = pool.QueryRow(ctx, digestSQL).Scan(&after); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if before != after {
|
||||
t.Fatal("legacy migration changed message data")
|
||||
}
|
||||
t.Log("adopted legacy schema: all existing messages are unchanged")
|
||||
runGoose(t, "reset")
|
||||
var tables int
|
||||
if err = pool.QueryRow(ctx, `SELECT count(*) FROM information_schema.tables WHERE table_schema=$1 AND table_name IN ('users','chats','chat_members','messages','schema_migrations')`, schema).Scan(&tables); err != nil || tables != 0 {
|
||||
t.Fatalf("down left %d application tables: %v", tables, err)
|
||||
}
|
||||
runGoose(t, "up")
|
||||
if err = pool.QueryRow(ctx, `SELECT count(*) FROM messages`).Scan(&tables); err != nil || tables != 0 {
|
||||
t.Fatalf("up after down failed: %v", err)
|
||||
}
|
||||
t.Log("down and up succeeded in isolated schema")
|
||||
})
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
)
|
||||
|
||||
func (s *Storage) CreateUser(ctx context.Context, username, displayName, hash string) (domain.User, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
var user domain.User
|
||||
err := s.pool.QueryRow(ctx, `INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,$3,$4)
|
||||
RETURNING id::text,username,display_name,created_at`, uuid.NewString(), username, displayName, hash).
|
||||
Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
||||
return user, dbError(err)
|
||||
}
|
||||
|
||||
func (s *Storage) User(ctx context.Context, id string) (domain.User, bool, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
var user domain.User
|
||||
err := s.pool.QueryRow(ctx, `SELECT id::text,username,display_name,created_at FROM users WHERE id=$1`, id).
|
||||
Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return user, false, nil
|
||||
}
|
||||
return user, err == nil, dbError(err)
|
||||
}
|
||||
|
||||
func (s *Storage) UserByUsername(ctx context.Context, username string) (domain.User, string, bool, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
var user domain.User
|
||||
var hash string
|
||||
err := s.pool.QueryRow(ctx, `SELECT id::text,username,display_name,created_at,password_hash FROM users WHERE username=$1`, username).
|
||||
Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return user, "", false, nil
|
||||
}
|
||||
return user, hash, err == nil, dbError(err)
|
||||
}
|
||||
Reference in New Issue
Block a user