test: cover allocator PostgreSQL claims

This commit is contained in:
Josh Creek
2026-09-01 09:45:02 +01:00
parent b1966a3423
commit e729570010
2 changed files with 47 additions and 2 deletions
+46 -1
View File
@@ -41,7 +41,7 @@ func openIntegrationPostgres(t *testing.T) *sql.DB {
func applyIntegrationMigrations(t *testing.T, db *sql.DB) {
t.Helper()
if _, err := db.ExecContext(context.Background(), `DROP TABLE IF EXISTS schema_migrations, assignments, audit_events, outbox, result_receipts, ranked_season_rollovers, penalties, seasons, ratings, match_participants, matches, proposal_participants, proposals, queue_tickets, idempotency_keys, sessions, identities CASCADE`); err != nil {
if _, err := db.ExecContext(context.Background(), `DROP TABLE IF EXISTS schema_migrations, assignments, audit_events, outbox, result_receipts, ranked_season_rollovers, penalties, seasons, ratings, match_participants, matches, proposal_participants, proposals, allocations, game_servers, queue_tickets, idempotency_keys, sessions, identities CASCADE`); err != nil {
t.Fatalf("reset PostgreSQL schema: %v", err)
}
if err := migrations.Apply(context.Background(), db, filepath.Join("..", "migrations")); err != nil {
@@ -49,6 +49,51 @@ func applyIntegrationMigrations(t *testing.T, db *sql.DB) {
}
}
func TestPostgreSQLAllocatorClaimReplayAndCapacityFence(t *testing.T) {
db := openIntegrationPostgres(t)
applyIntegrationMigrations(t, db)
now := time.Now().UTC().Truncate(time.Microsecond)
ctx := context.Background()
servers := []domain.ReadyServer{
{ServerID: "allocator-server-b", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: domain.ServerReady},
{ServerID: "allocator-server-a", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: domain.ServerReady},
}
for _, server := range servers {
if err := RegisterReadyServer(ctx, db, server, now); err != nil {
t.Fatal(err)
}
}
request := domain.AllocationRequest{AllocationID: "allocation-integration-1", MatchID: "match-integration-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}
allocation, err := ClaimAllocation(ctx, db, request, now)
if err != nil {
t.Fatalf("claim: %v", err)
}
if allocation.ServerID != "allocator-server-a" || allocation.State != domain.ServerAllocated {
t.Fatalf("allocation=%+v", allocation)
}
replay, err := ClaimAllocation(ctx, db, request, now.Add(time.Second))
if err != nil || replay.ServerID != allocation.ServerID || !replay.AllocatedAt.Equal(allocation.AllocatedAt) {
t.Fatalf("replay=%+v err=%v", replay, err)
}
conflict := request
conflict.MatchID = "match-integration-other"
if _, err := ClaimAllocation(ctx, db, conflict, now); err != domain.ErrConflict {
t.Fatalf("conflicting replay err=%v", err)
}
second := request
second.AllocationID = "allocation-integration-2"
second.MatchID = "match-integration-2"
if _, err := ClaimAllocation(ctx, db, second, now); err != nil {
t.Fatalf("second claim: %v", err)
}
third := second
third.AllocationID = "allocation-integration-3"
third.MatchID = "match-integration-3"
if _, err := ClaimAllocation(ctx, db, third, now); err != domain.ErrNoCapacity {
t.Fatalf("capacity err=%v", err)
}
}
func TestPostgreSQLQueueAdapterAgainstRealDatabase(t *testing.T) {
db := openIntegrationPostgres(t)
applyIntegrationMigrations(t, db)