package allocator import ( "context" "errors" "reflect" "testing" "time" "github.com/cosmic-clash/cosmic-clash/server/agones" "github.com/cosmic-clash/cosmic-clash/server/domain" ) type matchClaimSpy struct { request domain.AllocationRequest found bool err error recorded domain.Allocation recordErr error bound domain.Allocation bindErr error } type recoverableProviderSpy struct { providerSpy recovered agones.AllocatedServer found bool recoverErr error } func (p *recoverableProviderSpy) RecoverAllocation(_ context.Context, _ domain.AllocationRequest, _ time.Time) (agones.AllocatedServer, bool, error) { return p.recovered, p.found, p.recoverErr } func (s *matchClaimSpy) FindProviderAllocation(_ context.Context, _ domain.AllocationRequest) (domain.Allocation, bool, error) { return s.recorded, s.recorded.AllocationID != "", s.recordErr } func (s *matchClaimSpy) ClaimAllocatingMatch(_ context.Context, _ time.Time) (domain.AllocationRequest, bool, error) { return s.request, s.found, s.err } func (s *matchClaimSpy) BindAllocatedMatch(_ context.Context, allocation domain.Allocation) error { s.bound = allocation return s.bindErr } func TestWorkerClaimsAllocatesAndBindsDurably(t *testing.T) { request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"} claims := &matchClaimSpy{request: request, found: true} provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-1", Region: request.Region, Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}} durable := &durableSpy{} worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }} processed, err := worker.RunOnce(context.Background()) if err != nil || !processed || provider.calls != 1 || durable.calls != 1 || claims.bound.ServerID != "server-1" { t.Fatalf("processed=%t err=%v provider=%d durable=%d bound=%+v", processed, err, provider.calls, durable.calls, claims.bound) } } func TestWorkerRetainsClaimWhenProviderOutcomeIsAmbiguous(t *testing.T) { request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"} claims := &matchClaimSpy{request: request, found: true} provider := &providerSpy{err: errors.New("provider timeout")} worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: &durableSpy{}, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }} processed, err := worker.RunOnce(context.Background()) if err == nil || !processed || claims.bound != (domain.Allocation{}) { t.Fatalf("processed=%t err=%v bound=%+v", processed, err, claims.bound) } } func TestWorkerRecoversDurableProviderAllocationWithoutCallingProvider(t *testing.T) { request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"} recorded := domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-1", Region: request.Region, Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated} claims := &matchClaimSpy{request: request, found: true, recorded: recorded} provider := &providerSpy{} worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: &durableSpy{}, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }} processed, err := worker.RunOnce(context.Background()) if err != nil || !processed || provider.calls != 0 || claims.bound != recorded { t.Fatalf("processed=%t err=%v provider=%d bound=%+v", processed, err, provider.calls, claims.bound) } } func TestWorkerRecoversProviderAllocationBeforeIssuingSecondAllocation(t *testing.T) { request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"} recovered := agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-recovered", Region: request.Region, Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated}, Endpoint: "127.0.0.1:31001"} claims := &matchClaimSpy{request: request, found: true} provider := &recoverableProviderSpy{recovered: recovered, found: true} durable := &durableSpy{} worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }} processed, err := worker.RunOnce(context.Background()) if err != nil || !processed || provider.calls != 0 || durable.calls != 1 || claims.bound.ServerID != "server-recovered" { t.Fatalf("processed=%t err=%v provider_calls=%d durable_calls=%d bound=%+v", processed, err, provider.calls, durable.calls, claims.bound) } } func TestWorkerBindsCanonicalRecordedRecovery(t *testing.T) { now := time.Unix(1_000, 0) request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"} providerAllocation := domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-recovered", Region: request.Region, Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated} canonical := providerAllocation canonical.AllocatedAt = now claims := &matchClaimSpy{request: request, found: true} provider := &recoverableProviderSpy{recovered: agones.AllocatedServer{Allocation: providerAllocation, Endpoint: "127.0.0.1:31001"}, found: true} worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: &durableSpy{result: canonical}, Now: func() time.Time { return now }}, Now: func() time.Time { return now }} processed, err := worker.RunOnce(context.Background()) if err != nil || !processed || claims.bound != canonical { t.Fatalf("processed=%t err=%v bound=%+v, want %+v", processed, err, claims.bound, canonical) } } func TestWorkerRejectsRecoveredAllocationForDifferentCompatibility(t *testing.T) { request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"} claims := &matchClaimSpy{request: request, found: true} provider := &recoverableProviderSpy{recovered: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-1", Region: "NA", Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated}, Endpoint: "127.0.0.1:31001"}, found: true} durable := &durableSpy{} worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }} processed, err := worker.RunOnce(context.Background()) if err == nil || !processed || durable.calls != 0 || claims.bound != (domain.Allocation{}) { t.Fatalf("processed=%t err=%v durable_calls=%d bound=%+v", processed, err, durable.calls, claims.bound) } } func TestWorkerRejectsRecoveredAllocationForDifferentArena(t *testing.T) { request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", ArenaPath: "res://scenes/arena_01.tscn", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"} claims := &matchClaimSpy{request: request, found: true} provider := &recoverableProviderSpy{recovered: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-1", ArenaPath: "res://scenes/arena_02.tscn", Region: request.Region, Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated}, Endpoint: "127.0.0.1:31001"}, found: true} durable := &durableSpy{} worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }} processed, err := worker.RunOnce(context.Background()) if err == nil || !processed || durable.calls != 0 || claims.bound != (domain.Allocation{}) { t.Fatalf("processed=%t err=%v durable_calls=%d bound=%+v", processed, err, durable.calls, claims.bound) } } func TestWorkerDoesNothingWhenNoDurableMatchIsAvailable(t *testing.T) { claims := &matchClaimSpy{} worker := Worker{Claims: claims, Now: func() time.Time { return time.Unix(1_000, 0) }} processed, err := worker.RunOnce(context.Background()) if err != nil || processed { t.Fatalf("processed=%t err=%v", processed, err) } } func TestAllocationLabelsMirrorFleetCompatibilityTuple(t *testing.T) { got := AllocationLabels(domain.AllocationRequest{Region: "NA", Build: "build-4", Protocol: 12, Transport: "steam_sdr"}) want := map[string]string{"cosmic-clash.io/region": "NA", "cosmic-clash.io/build": "build-4", "cosmic-clash.io/protocol": "12", "cosmic-clash.io/transport": "steam_sdr"} if !reflect.DeepEqual(got, want) { t.Fatalf("labels=%v want=%v", got, want) } } func TestAllocationLabelsCarryPlaylistWhenKnown(t *testing.T) { labels := AllocationLabels(domain.AllocationRequest{Playlist: domain.Ranked, Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}) if labels["cosmic-clash.io/playlist"] != string(domain.Ranked) { t.Fatalf("playlist label = %q, want %q", labels["cosmic-clash.io/playlist"], domain.Ranked) } }