forked from mtusi2026/mtusi2026lab3
feat: add shared idempotency and replica identity
This commit is contained in:
@@ -12,8 +12,9 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/postgres"
|
||||
"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) {
|
||||
@@ -69,6 +70,140 @@ func TestCreateChatRollsBackForUnknownMember(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageIdempotencyReplayAndConflict(t *testing.T) {
|
||||
pool := openTestDatabase(t)
|
||||
store := postgres.New(pool)
|
||||
owner, chatID := createOwnerAndChat(t, store)
|
||||
key := uuid.NewString()
|
||||
|
||||
first, replay, err := store.CreateMessage(context.Background(), chatID, owner.ID, "same body", key, 24*time.Hour)
|
||||
if err != nil || replay {
|
||||
t.Fatalf("first CreateMessage() = (%v, %t, %v)", first, replay, err)
|
||||
}
|
||||
second, replay, err := store.CreateMessage(context.Background(), chatID, owner.ID, "same body", key, 24*time.Hour)
|
||||
if err != nil || !replay {
|
||||
t.Fatalf("replay CreateMessage() = (%v, %t, %v)", second, replay, err)
|
||||
}
|
||||
if first != second {
|
||||
t.Fatalf("replay message = %v, want %v", second, first)
|
||||
}
|
||||
if _, _, err := store.CreateMessage(context.Background(), chatID, owner.ID, "different body", key, 24*time.Hour); !errors.Is(err, postgres.ErrIdempotencyConflict) {
|
||||
t.Fatalf("conflicting CreateMessage() error = %v, want ErrIdempotencyConflict", err)
|
||||
}
|
||||
if count := messageCount(t, pool, chatID); count != 1 {
|
||||
t.Fatalf("message count = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentMessageIdempotency(t *testing.T) {
|
||||
pool := openTestDatabase(t)
|
||||
stores := []*postgres.Store{postgres.New(pool), postgres.New(pool)}
|
||||
owner, chatID := createOwnerAndChat(t, stores[0])
|
||||
key := uuid.NewString()
|
||||
|
||||
const workers = 8
|
||||
type result struct {
|
||||
message model.Message
|
||||
replay bool
|
||||
err error
|
||||
}
|
||||
results := make(chan result, workers)
|
||||
var group sync.WaitGroup
|
||||
for worker := range workers {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
message, replay, err := stores[worker%len(stores)].CreateMessage(
|
||||
context.Background(), chatID, owner.ID, "concurrent body", key, 24*time.Hour,
|
||||
)
|
||||
results <- result{message: message, replay: replay, err: err}
|
||||
}()
|
||||
}
|
||||
group.Wait()
|
||||
close(results)
|
||||
|
||||
var messageID string
|
||||
var created int
|
||||
for result := range results {
|
||||
if result.err != nil {
|
||||
t.Fatalf("CreateMessage() error = %v", result.err)
|
||||
}
|
||||
if messageID == "" {
|
||||
messageID = result.message.ID
|
||||
}
|
||||
if result.message.ID != messageID {
|
||||
t.Fatalf("message ID = %s, want %s", result.message.ID, messageID)
|
||||
}
|
||||
if !result.replay {
|
||||
created++
|
||||
}
|
||||
}
|
||||
if created != 1 {
|
||||
t.Fatalf("new message results = %d, want 1", created)
|
||||
}
|
||||
if count := messageCount(t, pool, chatID); count != 1 {
|
||||
t.Fatalf("message count = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageIdempotencyScopeAndExpiry(t *testing.T) {
|
||||
pool := openTestDatabase(t)
|
||||
store := postgres.New(pool)
|
||||
owner, chatID := createOwnerAndChat(t, store)
|
||||
second, err := store.CreateUser(context.Background(), uniqueUsername(), "Second", "password-hash")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser() error = %v", err)
|
||||
}
|
||||
chat, err := store.CreateChat(context.Background(), "Shared", []string{owner.ID, second.ID}, owner.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChat() error = %v", err)
|
||||
}
|
||||
key := uuid.NewString()
|
||||
|
||||
if _, _, err := store.CreateMessage(context.Background(), chat.ID, owner.ID, "owner", key, 24*time.Hour); err != nil {
|
||||
t.Fatalf("owner CreateMessage() error = %v", err)
|
||||
}
|
||||
if _, _, err := store.CreateMessage(context.Background(), chat.ID, second.ID, "second", key, 24*time.Hour); err != nil {
|
||||
t.Fatalf("second user CreateMessage() error = %v", err)
|
||||
}
|
||||
if _, _, err := store.CreateMessage(context.Background(), chatID, owner.ID, "other chat", key, 24*time.Hour); err != nil {
|
||||
t.Fatalf("other chat CreateMessage() error = %v", err)
|
||||
}
|
||||
if _, err := 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, chat.ID, key); err != nil {
|
||||
t.Fatalf("expire idempotency key: %v", err)
|
||||
}
|
||||
message, replay, err := store.CreateMessage(context.Background(), chat.ID, owner.ID, "after expiry", key, 24*time.Hour)
|
||||
if err != nil || replay || message.Text != "after expiry" {
|
||||
t.Fatalf("expired CreateMessage() = (%v, %t, %v)", message, replay, err)
|
||||
}
|
||||
}
|
||||
|
||||
func createOwnerAndChat(t *testing.T, store *postgres.Store) (model.User, string) {
|
||||
t.Helper()
|
||||
owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser() error = %v", err)
|
||||
}
|
||||
chat, err := store.CreateChat(context.Background(), "Chat", []string{owner.ID}, owner.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChat() error = %v", err)
|
||||
}
|
||||
return owner, chat.ID
|
||||
}
|
||||
|
||||
func messageCount(t *testing.T, pool *pgxpool.Pool, chatID string) int {
|
||||
t.Helper()
|
||||
var count int
|
||||
if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM messages WHERE chat_id = $1", chatID).Scan(&count); err != nil {
|
||||
t.Fatalf("count messages: %v", err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func openTestDatabase(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
url := testSecret(t, "TEST_DATABASE_URL", "TEST_DATABASE_URL_FILE")
|
||||
@@ -76,11 +211,14 @@ func openTestDatabase(t *testing.T) *pgxpool.Pool {
|
||||
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)
|
||||
}
|
||||
if err := database.Migrate(context.Background(), pool, 30*time.Second); err != nil {
|
||||
pool.Close()
|
||||
t.Fatalf("database.Migrate() error = %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
return pool
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user