229 lines
6.4 KiB
Go
229 lines
6.4 KiB
Go
package store_test
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"messenger/internal/chat"
|
|
)
|
|
|
|
const keyTTL = time.Hour
|
|
|
|
// expire makes every stored key older than keyTTL.
|
|
func expire(t *testing.T) {
|
|
t.Helper()
|
|
if _, err := db.Pool.Exec(context.Background(),
|
|
`UPDATE message_idempotency_keys SET created_at = now() - interval '2 hours'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func (f fixture) appendOnce(t *testing.T, m chat.Message, key string) chat.Message {
|
|
t.Helper()
|
|
got, err := f.messages.AppendOnce(f.ctx, m, key, keyTTL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
func (f fixture) count(t *testing.T, chatID uuid.UUID) int {
|
|
t.Helper()
|
|
got, err := f.messages.ListByChat(f.ctx, chatID, nil, 1000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return len(got)
|
|
}
|
|
|
|
func keyTarget(t *testing.T, key string) uuid.UUID {
|
|
t.Helper()
|
|
var id uuid.UUID
|
|
if err := db.Pool.QueryRow(context.Background(),
|
|
`SELECT message_id FROM message_idempotency_keys WHERE key = $1`, key).Scan(&id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestAppendOnceInsertsThenReplays(t *testing.T) {
|
|
f := newFixture(t)
|
|
alice := f.mustUser(t)
|
|
c := f.mustChat(t, alice)
|
|
first := f.newMessage(t, c.ID, alice)
|
|
if got := f.appendOnce(t, first, "k1"); got.ID != first.ID {
|
|
t.Fatalf("first append returned %v, want the new message %v", got.ID, first.ID)
|
|
}
|
|
retry := f.newMessage(t, c.ID, alice)
|
|
got := f.appendOnce(t, retry, "k1")
|
|
if got.ID != first.ID || !got.CreatedAt.Equal(first.CreatedAt) || got.Text != first.Text || got.SenderID != alice {
|
|
t.Fatalf("replay returned %+v, want the stored %+v", got, first)
|
|
}
|
|
if n := f.count(t, c.ID); n != 1 {
|
|
t.Fatalf("history has %d messages, want 1", n)
|
|
}
|
|
}
|
|
|
|
func TestAppendOnceTakesOverExpiredKey(t *testing.T) {
|
|
f := newFixture(t)
|
|
alice := f.mustUser(t)
|
|
c := f.mustChat(t, alice)
|
|
f.appendOnce(t, f.newMessage(t, c.ID, alice), "k1")
|
|
expire(t)
|
|
next := f.newMessage(t, c.ID, alice)
|
|
if got := f.appendOnce(t, next, "k1"); got.ID != next.ID {
|
|
t.Fatalf("expired key returned %v, want the new message %v", got.ID, next.ID)
|
|
}
|
|
if n := f.count(t, c.ID); n != 2 {
|
|
t.Fatalf("history has %d messages, want 2", n)
|
|
}
|
|
if target := keyTarget(t, "k1"); target != next.ID {
|
|
t.Fatalf("key points to %v, want %v", target, next.ID)
|
|
}
|
|
}
|
|
|
|
// A cleaner batch holds the expired key row; the request waits for it and,
|
|
// once the row is gone, inserts its message under the now free key.
|
|
func TestAppendOnceWaitsForCleanerThenInserts(t *testing.T) {
|
|
f := newFixture(t)
|
|
alice := f.mustUser(t)
|
|
c := f.mustChat(t, alice)
|
|
f.appendOnce(t, f.newMessage(t, c.ID, alice), "k1")
|
|
expire(t)
|
|
|
|
tx, err := db.Pool.Begin(f.ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer tx.Rollback(f.ctx)
|
|
if _, err := tx.Exec(f.ctx, `SELECT 1 FROM message_idempotency_keys WHERE key = 'k1' FOR UPDATE`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
next := f.newMessage(t, c.ID, alice)
|
|
type result struct {
|
|
m chat.Message
|
|
err error
|
|
}
|
|
done := make(chan result, 1)
|
|
go func() {
|
|
m, err := f.messages.AppendOnce(f.ctx, next, "k1", keyTTL)
|
|
done <- result{m, err}
|
|
}()
|
|
select {
|
|
case r := <-done:
|
|
t.Fatalf("AppendOnce did not wait for the locked key: %+v", r)
|
|
case <-time.After(300 * time.Millisecond):
|
|
}
|
|
if _, err := tx.Exec(f.ctx, `DELETE FROM message_idempotency_keys WHERE key = 'k1'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := tx.Commit(f.ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case r := <-done:
|
|
if r.err != nil || r.m.ID != next.ID {
|
|
t.Fatalf("after the cleaner: %+v, %v; want the new message %v", r.m, r.err, next.ID)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("AppendOnce did not finish after the cleaner committed")
|
|
}
|
|
}
|
|
|
|
// A request holding a key row must not make the cleaner wait: it skips the
|
|
// row and deletes it on a later run.
|
|
func TestCleanerSkipsRowLockedByRequest(t *testing.T) {
|
|
f := newFixture(t)
|
|
alice := f.mustUser(t)
|
|
c := f.mustChat(t, alice)
|
|
f.appendOnce(t, f.newMessage(t, c.ID, alice), "k1")
|
|
expire(t)
|
|
|
|
tx, err := db.Pool.Begin(f.ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := tx.Exec(f.ctx, `SELECT 1 FROM message_idempotency_keys WHERE key = 'k1' FOR UPDATE`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start := time.Now()
|
|
n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 1000)
|
|
if err != nil || n != 0 {
|
|
t.Fatalf("cleaner over a locked row: deleted %d, %v; want 0", n, err)
|
|
}
|
|
if waited := time.Since(start); waited > time.Second {
|
|
t.Fatalf("cleaner waited %v for the locked row", waited)
|
|
}
|
|
if err := tx.Rollback(f.ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 1000); err != nil || n != 1 {
|
|
t.Fatalf("cleaner after release: deleted %d, %v; want 1", n, err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteExpiredKeysOnlyExpiredAndUpToLimit(t *testing.T) {
|
|
f := newFixture(t)
|
|
alice := f.mustUser(t)
|
|
c := f.mustChat(t, alice)
|
|
for _, k := range []string{"a", "b", "c"} {
|
|
f.appendOnce(t, f.newMessage(t, c.ID, alice), k)
|
|
}
|
|
expire(t)
|
|
f.appendOnce(t, f.newMessage(t, c.ID, alice), "fresh")
|
|
|
|
if n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 2); err != nil || n != 2 {
|
|
t.Fatalf("first batch deleted %d, %v; want 2", n, err)
|
|
}
|
|
if n, err := f.messages.DeleteExpiredKeys(f.ctx, keyTTL, 2); err != nil || n != 1 {
|
|
t.Fatalf("second batch deleted %d, %v; want 1", n, err)
|
|
}
|
|
var left int
|
|
if err := db.Pool.QueryRow(f.ctx, `SELECT count(*) FROM message_idempotency_keys`).Scan(&left); err != nil || left != 1 {
|
|
t.Fatalf("%d keys left, %v; want only the fresh one", left, err)
|
|
}
|
|
if n := f.count(t, c.ID); n != 4 {
|
|
t.Fatalf("cleanup removed messages: %d left, want 4", n)
|
|
}
|
|
}
|
|
|
|
// Every request brings its own new message under one key; exactly one is
|
|
// stored and all of them get it back. This also proves the key row and the
|
|
// message it references are written by one statement.
|
|
func TestConcurrentAppendOnceSameKeyStoresOneMessage(t *testing.T) {
|
|
f := newFixture(t)
|
|
alice := f.mustUser(t)
|
|
c := f.mustChat(t, alice)
|
|
const requests = 20
|
|
ids := make(chan uuid.UUID, requests)
|
|
var wg sync.WaitGroup
|
|
for range requests {
|
|
m := f.newMessage(t, c.ID, alice)
|
|
wg.Go(func() {
|
|
got, err := f.messages.AppendOnce(f.ctx, m, "same", keyTTL)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
ids <- got.ID
|
|
})
|
|
}
|
|
wg.Wait()
|
|
close(ids)
|
|
seen := map[uuid.UUID]int{}
|
|
for id := range ids {
|
|
seen[id]++
|
|
}
|
|
if len(seen) != 1 {
|
|
t.Fatalf("requests got %d different messages, want 1: %v", len(seen), seen)
|
|
}
|
|
if n := f.count(t, c.ID); n != 1 {
|
|
t.Fatalf("history has %d messages, want 1", n)
|
|
}
|
|
}
|