mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
feat(multiplayer): lease allocating match claims
This commit is contained in:
@@ -143,6 +143,57 @@ func TestPostgreSQLAcceptedProposalPromotesOneAtomicAllocatingMatch(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLAllocationMatchClaimLeaseAndBindFence(t *testing.T) {
|
||||
db := openIntegrationPostgres(t)
|
||||
applyIntegrationMigrations(t, db)
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
ctx := context.Background()
|
||||
for index, player := range []string{"allocation-match-a", "allocation-match-b"} {
|
||||
if _, err := db.ExecContext(ctx, `INSERT INTO identities (player_id, steam_id) VALUES ($1, $1)`, player); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, `INSERT INTO queue_tickets (ticket_id, player_id, playlist, state, client_build, protocol_version, enqueued_at, expires_at) VALUES ($1, $2, 'casual', 'ACCEPTED', 'build-1', 1, $3, $4)`, fmt.Sprintf("allocation-match-ticket-%d", index), player, now, now.Add(time.Minute)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, `INSERT INTO matches (match_id, playlist, state, region, protocol_version) VALUES ('allocation-match', 'casual', 'ALLOCATING', 'EU', 1)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for index, player := range []string{"allocation-match-a", "allocation-match-b"} {
|
||||
if _, err := db.ExecContext(ctx, `INSERT INTO match_participants (match_id, player_id, ticket_id, slot, team) VALUES ('allocation-match', $1, $2, $3, $4)`, player, fmt.Sprintf("allocation-match-ticket-%d", index), index*3, index); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
claim, found, err := ClaimAllocatingMatch(ctx, db, "enet", now)
|
||||
if err != nil || !found || claim.Request != (domain.AllocationRequest{AllocationID: "allocation-allocation-match", MatchID: "allocation-match", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}) {
|
||||
t.Fatalf("claim=%+v found=%t err=%v", claim, found, err)
|
||||
}
|
||||
if err := ReleaseAllocatedMatchClaim(ctx, db, claim.Request.MatchID, "different-allocation"); err != domain.ErrConflict {
|
||||
t.Fatalf("wrong-claim release err=%v", err)
|
||||
}
|
||||
if err := ReleaseAllocatedMatchClaim(ctx, db, claim.Request.MatchID, claim.Request.AllocationID); err != nil {
|
||||
t.Fatalf("release claim: %v", err)
|
||||
}
|
||||
reclaimed, found, err := ClaimAllocatingMatch(ctx, db, "enet", now.Add(time.Second))
|
||||
if err != nil || !found || reclaimed.Request.AllocationID != claim.Request.AllocationID {
|
||||
t.Fatalf("reclaimed=%+v found=%t err=%v", reclaimed, found, err)
|
||||
}
|
||||
server := domain.ReadyServer{ServerID: "allocation-server", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: domain.ServerReady}
|
||||
if err := RegisterReadyServer(ctx, db, server, now); err != nil {
|
||||
t.Fatalf("register allocation server: %v", err)
|
||||
}
|
||||
allocation, err := ClaimAllocation(ctx, db, reclaimed.Request, now.Add(time.Second))
|
||||
if err != nil {
|
||||
t.Fatalf("record provider allocation: %v", err)
|
||||
}
|
||||
if err := BindAllocatedMatch(ctx, db, allocation); err != nil {
|
||||
t.Fatalf("bind allocation: %v", err)
|
||||
}
|
||||
if _, found, err := ClaimAllocatingMatch(ctx, db, "enet", now.Add(2*time.Second)); err != nil || found {
|
||||
t.Fatalf("bound match re-claimed found=%t err=%v", found, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLQueueAdapterAgainstRealDatabase(t *testing.T) {
|
||||
db := openIntegrationPostgres(t)
|
||||
applyIntegrationMigrations(t, db)
|
||||
|
||||
Reference in New Issue
Block a user