fix: address review comments
CI / verify (pull_request) Successful in 4m21s

This commit is contained in:
2026-10-02 17:23:58 +03:00
parent 18d51db435
commit 0062313c0b
15 changed files with 698 additions and 395 deletions
+72 -15
View File
@@ -11,15 +11,37 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/suite"
"gitea.privcloud.tech/dizi/mtusi2026lab3/internal/database"
"gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab3/internal/postgres"
)
func TestConcurrentUsernameCreation(t *testing.T) {
pool := openTestDatabase(t)
store := postgres.New(pool)
type PostgresSuite struct {
suite.Suite
pool *pgxpool.Pool
store *postgres.Store
}
func TestPostgresSuite(t *testing.T) {
suite.Run(t, new(PostgresSuite))
}
func (s *PostgresSuite) SetupSuite() {
s.pool = openTestDatabase(s.T())
s.store = postgres.New(s.pool)
}
func (s *PostgresSuite) TearDownSuite() {
if s.pool != nil {
s.pool.Close()
}
}
func (s *PostgresSuite) TestConcurrentUsernameCreation() {
t := s.T()
store := s.store
username := "race_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16]
const workers = 8
@@ -52,9 +74,10 @@ func TestConcurrentUsernameCreation(t *testing.T) {
}
}
func TestCreateChatRollsBackForUnknownMember(t *testing.T) {
pool := openTestDatabase(t)
store := postgres.New(pool)
func (s *PostgresSuite) TestCreateChatRollsBackForUnknownMember() {
t := s.T()
pool := s.pool
store := s.store
owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash")
if err != nil {
t.Fatalf("CreateUser() error = %v", err)
@@ -70,9 +93,10 @@ func TestCreateChatRollsBackForUnknownMember(t *testing.T) {
}
}
func TestMessageIdempotencyReplayAndConflict(t *testing.T) {
pool := openTestDatabase(t)
store := postgres.New(pool)
func (s *PostgresSuite) TestMessageIdempotencyReplayAndConflict() {
t := s.T()
pool := s.pool
store := s.store
owner, chatID := createOwnerAndChat(t, store)
key := uuid.NewString()
@@ -95,8 +119,9 @@ func TestMessageIdempotencyReplayAndConflict(t *testing.T) {
}
}
func TestConcurrentMessageIdempotency(t *testing.T) {
pool := openTestDatabase(t)
func (s *PostgresSuite) TestConcurrentMessageIdempotency() {
t := s.T()
pool := s.pool
stores := []*postgres.Store{postgres.New(pool), postgres.New(pool)}
owner, chatID := createOwnerAndChat(t, stores[0])
key := uuid.NewString()
@@ -146,9 +171,10 @@ func TestConcurrentMessageIdempotency(t *testing.T) {
}
}
func TestMessageIdempotencyScopeAndExpiry(t *testing.T) {
pool := openTestDatabase(t)
store := postgres.New(pool)
func (s *PostgresSuite) TestMessageIdempotencyScopeAndExpiry() {
t := s.T()
pool := s.pool
store := s.store
owner, chatID := createOwnerAndChat(t, store)
second, err := store.CreateUser(context.Background(), uniqueUsername(), "Second", "password-hash")
if err != nil {
@@ -182,6 +208,38 @@ func TestMessageIdempotencyScopeAndExpiry(t *testing.T) {
}
}
func (s *PostgresSuite) TestExpiredIdempotencyRecordsAreCleanedOnNewMessages() {
t := s.T()
owner, chatID := createOwnerAndChat(t, s.store)
expiredKey := uuid.NewString()
if _, _, err := s.store.CreateMessage(context.Background(), chatID, owner.ID, "expired", expiredKey, 24*time.Hour); err != nil {
t.Fatalf("create expired message: %v", err)
}
if _, err := s.pool.Exec(context.Background(), `
UPDATE message_idempotency
SET created_at = clock_timestamp() - INTERVAL '2 days',
expires_at = clock_timestamp() - INTERVAL '1 day'
WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chatID, expiredKey); err != nil {
t.Fatalf("expire idempotency key: %v", err)
}
if _, _, err := s.store.CreateMessage(
context.Background(), chatID, owner.ID, "new message", uuid.NewString(), 24*time.Hour,
); err != nil {
t.Fatalf("create new message: %v", err)
}
var remaining int
if err := s.pool.QueryRow(context.Background(), `
SELECT count(*)
FROM message_idempotency
WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chatID, expiredKey).Scan(&remaining); err != nil {
t.Fatalf("count expired idempotency records: %v", err)
}
if remaining != 0 {
t.Fatalf("expired idempotency records = %d, want 0", remaining)
}
}
func createOwnerAndChat(t *testing.T, store *postgres.Store) (model.User, string) {
t.Helper()
owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash")
@@ -219,7 +277,6 @@ func openTestDatabase(t *testing.T) *pgxpool.Pool {
pool.Close()
t.Fatalf("database.Migrate() error = %v", err)
}
t.Cleanup(pool.Close)
return pool
}