mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 20:13:43 +00:00
test: cover PostgreSQL result and outbox flow
This commit is contained in:
@@ -4,6 +4,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -243,6 +244,47 @@ func TestPostgreSQLProposalCreationRollsBackPartialClaims(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLResultCompletionAndOutboxAreAtomicAndReplayable(t *testing.T) {
|
||||
db := openIntegrationPostgres(t)
|
||||
applyIntegrationMigrations(t, db)
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
ctx := context.Background()
|
||||
if _, err := db.ExecContext(ctx, `INSERT INTO matches (match_id, playlist, state, region, protocol_version, server_id) VALUES ('result-match', 'casual', 'RESULT_PENDING', 'NA', 1, 'result-server')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload := []byte(`{"match_id":"result-match","team0_score":2,"team1_score":1}`)
|
||||
digest := sha256.Sum256(payload)
|
||||
receipt := domain.ResultReceipt{ResultID: "result-receipt", MatchID: "result-match", ResultNonce: "result-nonce-123456", PayloadDigest: digest, IntegrityState: domain.IntegrityCertified, ReceivedAt: now}
|
||||
if err := CompleteResult(ctx, db, receipt, "result-server", "result-event", payload, now); err != nil {
|
||||
t.Fatalf("complete result: %v", err)
|
||||
}
|
||||
var state string
|
||||
if err := db.QueryRow(`SELECT state FROM matches WHERE match_id = 'result-match'`).Scan(&state); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if state != "COMPLETED" {
|
||||
t.Fatalf("result match state = %s", state)
|
||||
}
|
||||
events, err := ReadUnpublishedOutbox(ctx, db, 10)
|
||||
if err != nil || len(events) != 1 || events[0].EventID != "result-event" {
|
||||
t.Fatalf("unpublished result events = %+v, err = %v", events, err)
|
||||
}
|
||||
if err := MarkOutboxPublished(ctx, db, events[0].EventID, now.Add(time.Second)); err != nil {
|
||||
t.Fatalf("ack result event: %v", err)
|
||||
}
|
||||
if remaining, err := ReadUnpublishedOutbox(ctx, db, 10); err != nil || len(remaining) != 0 {
|
||||
t.Fatalf("outbox after ack = %+v, err = %v", remaining, err)
|
||||
}
|
||||
if err := CompleteResult(ctx, db, receipt, "result-server", "result-event-retry", payload, now.Add(time.Second)); err != nil {
|
||||
t.Fatalf("identical completed result replay: %v", err)
|
||||
}
|
||||
conflict := receipt
|
||||
conflict.ResultID = "different-result"
|
||||
if err := CompleteResult(ctx, db, conflict, "result-server", "different-event", []byte(`{"conflict":true}`), now.Add(2*time.Second)); err == nil {
|
||||
t.Fatal("conflicting completed result was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLMigrationsAreForwardExecutable(t *testing.T) {
|
||||
db := openIntegrationPostgres(t)
|
||||
applyIntegrationMigrations(t, db)
|
||||
|
||||
Reference in New Issue
Block a user