Files
CosmicClash/server/store/allocator_sql_test.go
T
2026-09-01 18:38:06 +01:00

58 lines
2.3 KiB
Go

package store
import (
"testing"
"time"
"github.com/cosmic-clash/cosmic-clash/server/domain"
)
func TestAllocatorSQLClaimsAndAuditsCompatibleReadyServers(t *testing.T) {
for query, fragments := range map[string][]string{
RegisterReadyServerSQL: {"game_servers", "ON CONFLICT", "WHERE game_servers.state = 'READY'"},
ClaimReadyServerSQL: {"state = 'READY'", "region = $1", "protocol_version = $3", "FOR UPDATE SKIP LOCKED", "ORDER BY server_id"},
InsertAllocationSQL: {"allocations", "request_digest", "state", "ALLOCATED"},
ProviderServerClaimSQL: {"state = 'READY'", "region = $2", "protocol_version = $4", "RETURNING"},
ServerAllocationConflictSQL: {"server_id = $1", "FOR UPDATE"},
} {
for _, fragment := range fragments {
if !contains(query, fragment) {
t.Fatalf("query missing %q", fragment)
}
}
}
for _, fragment := range []string{"allocation_quotas", "ON CONFLICT (region)", "used_allocations"} {
if !contains(SetAllocationQuotaSQL, fragment) {
t.Fatalf("quota query missing %q", fragment)
}
}
}
func TestSetAllocationQuotaRejectsInvalidArgumentsWithoutDatabase(t *testing.T) {
if err := SetAllocationQuota(nil, nil, "EU", 1, time.Minute, time.Unix(1000, 0)); err == nil {
t.Fatal("nil database accepted")
}
if err := SetAllocationQuota(nil, nil, "APAC", 1, time.Minute, time.Unix(1000, 0)); err == nil {
t.Fatal("unknown region accepted")
}
if err := SetAllocationQuota(nil, nil, "EU", 0, time.Minute, time.Unix(1000, 0)); err == nil {
t.Fatal("zero limit accepted")
}
}
func TestAllocationQuotaConsumeRejectsInvalidArgumentsWithoutDatabase(t *testing.T) {
if err := (AllocationQuota{}).Consume(nil, "EU", time.Unix(1000, 0)); err == nil {
t.Fatal("nil database accepted")
}
}
func TestClaimAllocationRejectsInvalidRequestsWithoutDatabase(t *testing.T) {
_, err := ClaimAllocation(nil, nil, domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, time.Unix(1000, 0))
if err == nil {
t.Fatal("nil database accepted")
}
if _, err := ClaimAllocation(nil, nil, domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 0, Transport: "enet"}, time.Unix(1000, 0)); err != domain.ErrAllocationInput {
t.Fatalf("invalid request err=%v", err)
}
}