forked from mtusi2026/mtusi2026lab7
102 lines
3.7 KiB
Go
102 lines
3.7 KiB
Go
package tests
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
|
|
"messenger/src/backend/repositories"
|
|
)
|
|
|
|
func TestIdempotencyConcurrentStoresAndScopes(t *testing.T) {
|
|
f := newTestFixture(t)
|
|
alice, _ := createTestUser(t, f.users, "alice", "Alice")
|
|
bob, _ := createTestUser(t, f.users, "bob", "Bob")
|
|
chat := createTestChat(t, f.chats, "Chat", []string{alice.ID, bob.ID})
|
|
otherChat := createTestChat(t, f.chats, "Other", []string{alice.ID})
|
|
stores := []*repositories.MessageStore{f.messages, repositories.NewMessageStore(f.db)}
|
|
const concurrency = 24
|
|
start := make(chan struct{})
|
|
results := make(chan repositories.Message, concurrency)
|
|
errorsFound := make(chan error, concurrency)
|
|
var group sync.WaitGroup
|
|
for i := 0; i < concurrency; i++ {
|
|
group.Add(1)
|
|
go func(i int) {
|
|
defer group.Done()
|
|
<-start
|
|
message, err := stores[i%len(stores)].CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "same-key")
|
|
results <- message
|
|
errorsFound <- err
|
|
}(i)
|
|
}
|
|
close(start)
|
|
group.Wait()
|
|
close(results)
|
|
close(errorsFound)
|
|
for err := range errorsFound {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var original repositories.Message
|
|
for message := range results {
|
|
if original.ID == "" {
|
|
original = message
|
|
} else if !reflect.DeepEqual(message, original) {
|
|
t.Fatalf("replay changed the message: %v != %v", message, original)
|
|
}
|
|
}
|
|
if len(listTestMessages(t, f.messages, chat.ID)) != 1 {
|
|
t.Fatal("concurrent repeat created duplicate messages")
|
|
}
|
|
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "different", "same-key"); !errors.Is(err, repositories.ErrIdempotencyConflict) {
|
|
t.Fatalf("different body: got %v", err)
|
|
}
|
|
for _, scope := range [][2]string{{chat.ID, bob.ID}, {otherChat.ID, alice.ID}} {
|
|
message, err := f.messages.CreateIdempotent(context.Background(), scope[0], scope[1], "hello", "same-key")
|
|
if err != nil || message.ID == original.ID {
|
|
t.Fatalf("key leaked across scopes: %v", err)
|
|
}
|
|
}
|
|
if _, err := f.db.Exec(context.Background(), "UPDATE message_idempotency SET expires_at=now()-interval '1 second' WHERE message_id=$1", original.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reused, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "after expiry", "same-key")
|
|
if err != nil || reused.ID == original.ID {
|
|
t.Fatalf("expired key cannot be reused: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestIdempotencyRecordFailureRollsBackMessage(t *testing.T) {
|
|
f := newTestFixture(t)
|
|
alice, _ := createTestUser(t, f.users, "alice", "Alice")
|
|
chat := createTestChat(t, f.chats, "Chat", []string{alice.ID})
|
|
// Force a failure AFTER the message insert to verify transaction atomicity.
|
|
_, err := f.db.Exec(context.Background(), `
|
|
CREATE FUNCTION reject_replay() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN RAISE EXCEPTION 'synthetic replay failure'; END $$;
|
|
CREATE TRIGGER reject_replay BEFORE INSERT ON message_idempotency
|
|
FOR EACH ROW EXECUTE FUNCTION reject_replay()`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "retry-key"); err == nil {
|
|
t.Fatal("synthetic failure did not happen")
|
|
}
|
|
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
|
t.Fatal("failed transaction left a message behind")
|
|
}
|
|
if _, err := f.db.Exec(context.Background(), "DROP TRIGGER reject_replay ON message_idempotency"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "retry-key"); err != nil {
|
|
t.Fatalf("failure was cached instead of permitting retry: %v", err)
|
|
}
|
|
if len(listTestMessages(t, f.messages, chat.ID)) != 1 {
|
|
t.Fatal("retry did not produce exactly one message")
|
|
}
|
|
}
|