2 Commits
Author SHA1 Message Date
kk10 81cea31c81 test(chat): add create chat rollback test 2026-10-05 23:06:18 +03:00
kk10 66808c6c80 test(auth): add concurrent registration race test 2026-10-05 22:43:09 +03:00
3 changed files with 135 additions and 0 deletions
+3
View File
@@ -77,6 +77,9 @@ func (s *ChatService) Create(
ID: id,
Title: title,
})
if err != nil {
return model.Chat{}, err
}
err = q.AddChatMembers(ctx, db.AddChatMembersParams{
ChatID: row.ID,
+55
View File
@@ -0,0 +1,55 @@
//go:build contract
package studenttests_test
import (
"context"
"encoding/json"
"net/http"
"os"
"strings"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
func TestCreateChatRollback(t *testing.T) {
data, err := os.ReadFile("../secrets/test_database_url.txt")
if err != nil {
t.Fatalf("read secrets/test_database_url: %v", err)
}
ctx := context.Background()
conn, err := pgx.Connect(ctx, strings.TrimSpace(string(data)))
if err != nil {
t.Fatalf("connect to database: %v", err)
}
defer conn.Close(ctx)
creator := createUser(t)
title := "rollback-" + uuid.NewString()
body, err := json.Marshal(map[string]any{
"title": title,
"member_ids": []uuid.UUID{creator.ID, uuid.New()},
})
if err != nil {
t.Fatalf("marshal body: %v", err)
}
resp := doRequest(t, http.MethodPost, "/api/v1/chats", creator.Token, body)
resp.Body.Close()
if resp.StatusCode < 400 || resp.StatusCode >= 500 {
t.Fatalf("expected 4xx, got %d", resp.StatusCode)
}
var n int
err = conn.QueryRow(ctx, "SELECT count(*) FROM chats WHERE title = $1", title).Scan(&n)
if err != nil {
t.Fatalf("count chats: %v", err)
}
if n != 0 {
t.Fatalf("expected no chat rows after failed creation, found %d", n)
}
}
+77
View File
@@ -0,0 +1,77 @@
//go:build race
package studenttests_test
import (
"bytes"
"encoding/json"
"net/http"
"strings"
"sync"
"testing"
"github.com/google/uuid"
)
func TestConcurrentRegistration(t *testing.T) {
const workers = 20
username := "u_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:12]
body, err := json.Marshal(map[string]string{
"username": username,
"display_name": "Racer",
"password": testPassword,
})
if err != nil {
t.Fatalf("marshal body: %v", err)
}
start := make(chan struct{})
statuses := make(chan int, workers)
var wg sync.WaitGroup
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
<-start
req, err := http.NewRequest(http.MethodPost, *baseURL+"/api/v1/users", bytes.NewReader(body))
if err != nil {
t.Errorf("build request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
t.Errorf("request failed: %v", err)
return
}
defer resp.Body.Close()
statuses <- resp.StatusCode
}()
}
close(start)
wg.Wait()
close(statuses)
created, conflict := 0, 0
for s := range statuses {
switch s {
case http.StatusCreated:
created++
case http.StatusConflict:
conflict++
default:
t.Errorf("unexpected status %d", s)
}
}
if created != 1 || conflict != workers-1 {
t.Fatalf("expected 1 created and %d conflict, got %d created and %d conflict", workers-1, created, conflict)
}
}