mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
fix(multiplayer): recover recorded allocations
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
// lease a match before returning it and fence binding by allocation ID.
|
||||
type MatchClaimSource interface {
|
||||
ClaimAllocatingMatch(context.Context, time.Time) (domain.AllocationRequest, bool, error)
|
||||
FindProviderAllocation(context.Context, domain.AllocationRequest) (domain.Allocation, bool, error)
|
||||
BindAllocatedMatch(context.Context, domain.Allocation) error
|
||||
}
|
||||
|
||||
@@ -37,11 +38,18 @@ func (w Worker) RunOnce(ctx context.Context) (bool, error) {
|
||||
if err != nil || !found {
|
||||
return found, err
|
||||
}
|
||||
result, err := w.Service.Allocate(ctx, request, AllocationLabels(request))
|
||||
allocation, recorded, err := w.Claims.FindProviderAllocation(ctx, request)
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("allocate claimed match %s: %w", request.MatchID, err)
|
||||
return true, fmt.Errorf("recover allocation for match %s: %w", request.MatchID, err)
|
||||
}
|
||||
if err := w.Claims.BindAllocatedMatch(ctx, result.Allocation); err != nil {
|
||||
if !recorded {
|
||||
result, err := w.Service.Allocate(ctx, request, AllocationLabels(request))
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("allocate claimed match %s: %w", request.MatchID, err)
|
||||
}
|
||||
allocation = result.Allocation
|
||||
}
|
||||
if err := w.Claims.BindAllocatedMatch(ctx, allocation); err != nil {
|
||||
return true, fmt.Errorf("bind allocated match %s: %w", request.MatchID, err)
|
||||
}
|
||||
return true, nil
|
||||
|
||||
@@ -12,11 +12,17 @@ import (
|
||||
)
|
||||
|
||||
type matchClaimSpy struct {
|
||||
request domain.AllocationRequest
|
||||
found bool
|
||||
err error
|
||||
bound domain.Allocation
|
||||
bindErr error
|
||||
request domain.AllocationRequest
|
||||
found bool
|
||||
err error
|
||||
recorded domain.Allocation
|
||||
recordErr error
|
||||
bound domain.Allocation
|
||||
bindErr error
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -51,6 +57,18 @@ func TestWorkerRetainsClaimWhenProviderOutcomeIsAmbiguous(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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 TestWorkerDoesNothingWhenNoDurableMatchIsAvailable(t *testing.T) {
|
||||
claims := &matchClaimSpy{}
|
||||
worker := Worker{Claims: claims, Now: func() time.Time { return time.Unix(1_000, 0) }}
|
||||
|
||||
Reference in New Issue
Block a user