forked from mtusi2026/mtusi2026lab2
116 lines
3.0 KiB
Go
116 lines
3.0 KiB
Go
package postgres_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/database"
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/postgres"
|
|
)
|
|
|
|
func TestConcurrentUsernameCreation(t *testing.T) {
|
|
pool := openTestDatabase(t)
|
|
store := postgres.New(pool)
|
|
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 TestCreateChatRollsBackForUnknownMember(t *testing.T) {
|
|
pool := openTestDatabase(t)
|
|
store := postgres.New(pool)
|
|
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 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,
|
|
MigrationTimeout: 30 * time.Second,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("database.Open() error = %v", err)
|
|
}
|
|
t.Cleanup(pool.Close)
|
|
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
|
|
}
|