mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
feat: gate allocation on accepted proposals
This commit is contained in:
@@ -29,6 +29,36 @@ type Service struct {
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
// AllocateAcceptedProposal is the hand-off from proposal consensus to server
|
||||
// allocation. Keeping this check beside the provider call prevents a caller
|
||||
// from allocating capacity for an OPEN/DECLINED proposal or for a request
|
||||
// whose playlist does not match the proposal that produced it.
|
||||
func (s Service) AllocateAcceptedProposal(ctx context.Context, proposal domain.Proposal, request domain.AllocationRequest, playlist domain.Playlist, labels map[string]string) (agones.AllocatedServer, error) {
|
||||
if proposal.State != domain.Accepted || proposal.Playlist != playlist || len(proposal.Participants) == 0 {
|
||||
return agones.AllocatedServer{}, domain.ErrAllocationInput
|
||||
}
|
||||
if proposal.Playlist == domain.Ranked && len(proposal.Participants) != 6 {
|
||||
return agones.AllocatedServer{}, domain.ErrAllocationInput
|
||||
}
|
||||
if proposal.Playlist == domain.Casual && (len(proposal.Participants) < 2 || len(proposal.Participants) > 6) {
|
||||
return agones.AllocatedServer{}, domain.ErrAllocationInput
|
||||
}
|
||||
seen := make(map[string]struct{}, len(proposal.Participants))
|
||||
for _, participant := range proposal.Participants {
|
||||
if participant.PlayerID == "" || participant.Response != domain.AcceptedResponse {
|
||||
return agones.AllocatedServer{}, domain.ErrAllocationInput
|
||||
}
|
||||
if _, exists := seen[participant.PlayerID]; exists {
|
||||
return agones.AllocatedServer{}, domain.ErrAllocationInput
|
||||
}
|
||||
seen[participant.PlayerID] = struct{}{}
|
||||
}
|
||||
if request.MatchID == "" {
|
||||
return agones.AllocatedServer{}, domain.ErrAllocationInput
|
||||
}
|
||||
return s.Allocate(ctx, request, labels)
|
||||
}
|
||||
|
||||
func (s Service) PublishRoster(ctx context.Context, assignment domain.Assignment, roster []domain.SignedJoinAuthorisation, verify func([]byte, []byte) bool) error {
|
||||
if s.Roster == nil {
|
||||
return errNotConfigured
|
||||
|
||||
@@ -63,6 +63,44 @@ func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceAllocatesOnlyUnanimouslyAcceptedMatchingProposal(t *testing.T) {
|
||||
proposal := domain.Proposal{
|
||||
ProposalID: "proposal-1", Playlist: domain.Casual, State: domain.Accepted,
|
||||
Participants: []domain.ProposalParticipant{
|
||||
{PlayerID: "player-a", Response: domain.AcceptedResponse},
|
||||
{PlayerID: "player-b", Response: domain.AcceptedResponse},
|
||||
},
|
||||
}
|
||||
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", MatchID: "m", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
|
||||
durable := &durableSpy{}
|
||||
service := Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
request := domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}
|
||||
if _, err := service.AllocateAcceptedProposal(context.Background(), proposal, request, domain.Casual, map[string]string{"region": "EU"}); err != nil {
|
||||
t.Fatalf("accepted proposal was rejected: %v", err)
|
||||
}
|
||||
if provider.calls != 1 || durable.calls != 1 {
|
||||
t.Fatalf("provider/durable calls = %d/%d", provider.calls, durable.calls)
|
||||
}
|
||||
|
||||
for name, mutate := range map[string]func(*domain.Proposal){
|
||||
"open": func(p *domain.Proposal) { p.State = domain.Open },
|
||||
"wrong-playlist": func(p *domain.Proposal) { p.Playlist = domain.Ranked },
|
||||
"pending": func(p *domain.Proposal) { p.Participants[0].Response = domain.Pending },
|
||||
"duplicate": func(p *domain.Proposal) { p.Participants[1].PlayerID = p.Participants[0].PlayerID },
|
||||
} {
|
||||
invalid := proposal
|
||||
invalid.Participants = append([]domain.ProposalParticipant(nil), proposal.Participants...)
|
||||
mutate(&invalid)
|
||||
before := provider.calls
|
||||
if _, err := service.AllocateAcceptedProposal(context.Background(), invalid, request, domain.Casual, map[string]string{"region": "EU"}); err == nil {
|
||||
t.Fatalf("%s proposal was accepted", name)
|
||||
}
|
||||
if provider.calls != before {
|
||||
t.Fatalf("%s proposal reached provider", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestServicePublishesRosterOnlyForAllocatedAssignment(t *testing.T) {
|
||||
roster := &rosterSpy{}
|
||||
service := Service{Roster: roster}
|
||||
|
||||
Reference in New Issue
Block a user