forked from mtusi2026/mtusi2026lab2
Implement lab2 messenger API with PostgreSQL Redis and Docker
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026lab2/internal/model"
|
||||
)
|
||||
|
||||
func TestConcurrentRegistrationSameUsername(t *testing.T) {
|
||||
store, _ := newTestAuthStore(t)
|
||||
ctx := context.Background()
|
||||
|
||||
username := "race_" + uuid.NewString()[:8]
|
||||
const workers = 10
|
||||
|
||||
var wg sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
results := make(chan error, workers)
|
||||
|
||||
for i := 0; i < workers; i++ {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
|
||||
user := model.User{
|
||||
ID: uuid.New(),
|
||||
Username: username,
|
||||
DisplayName: "Race Test",
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
results <- store.CreateUser(ctx, user, "test_password_hash")
|
||||
}()
|
||||
}
|
||||
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(results)
|
||||
|
||||
successes := 0
|
||||
conflicts := 0
|
||||
|
||||
for err := range results {
|
||||
switch {
|
||||
case err == nil:
|
||||
successes++
|
||||
case errors.Is(err, ErrUsernameTaken):
|
||||
conflicts++
|
||||
default:
|
||||
t.Fatalf("unexpected registration error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if successes != 1 || conflicts != workers-1 {
|
||||
t.Fatalf(
|
||||
"expected 1 success and %d conflicts, got %d successes and %d conflicts",
|
||||
workers-1, successes, conflicts,
|
||||
)
|
||||
}
|
||||
|
||||
t.Logf("PASS: %d successful registration, %d conflicts",
|
||||
successes, conflicts)
|
||||
}
|
||||
|
||||
func TestCreateChatRollbackOnInvalidMember(t *testing.T) {
|
||||
authStore, validUserID := newTestAuthStore(t)
|
||||
ctx := context.Background()
|
||||
|
||||
chatID := uuid.New()
|
||||
|
||||
chat := model.Chat{
|
||||
ID: chatID,
|
||||
Title: "Rollback Test",
|
||||
CreatedAt: time.Now().UTC(),
|
||||
MemberIDs: []uuid.UUID{
|
||||
validUserID,
|
||||
uuid.New(),
|
||||
},
|
||||
}
|
||||
|
||||
err := NewChatStore(authStore.pool).CreateChat(ctx, chat)
|
||||
|
||||
if !errors.Is(err, ErrInvalidMember) {
|
||||
t.Fatalf("expected ErrInvalidMember, got %v", err)
|
||||
}
|
||||
|
||||
var chatCount int
|
||||
err = authStore.pool.QueryRow(
|
||||
ctx,
|
||||
"SELECT COUNT(*) FROM chats WHERE id = $1",
|
||||
chatID,
|
||||
).Scan(&chatCount)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("check chat existence: %v", err)
|
||||
}
|
||||
|
||||
if chatCount != 0 {
|
||||
t.Fatalf("rollback failed: chat still exists")
|
||||
}
|
||||
|
||||
var memberCount int
|
||||
err = authStore.pool.QueryRow(
|
||||
ctx,
|
||||
"SELECT COUNT(*) FROM chat_members WHERE chat_id = $1",
|
||||
chatID,
|
||||
).Scan(&memberCount)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("check chat members: %v", err)
|
||||
}
|
||||
|
||||
if memberCount != 0 {
|
||||
t.Fatalf("rollback failed: %d chat members still exist", memberCount)
|
||||
}
|
||||
|
||||
t.Log("PASS: transaction rolled back chat and member inserts")
|
||||
}
|
||||
Reference in New Issue
Block a user