290 lines
9.5 KiB
Go
290 lines
9.5 KiB
Go
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")
|
|
})
|
|
|
|
}
|