feat: gate assignment publication on allocation

This commit is contained in:
Josh Creek
2026-08-31 21:24:19 +01:00
parent 88b5ffedb2
commit 726fe1ce2e
4 changed files with 71 additions and 7 deletions
+35
View File
@@ -83,3 +83,38 @@ func TestAllocatorConcurrentClaimsCannotDoubleAllocateOneServer(t *testing.T) {
t.Fatalf("concurrent claims succeeded %d times", wins)
}
}
func TestAllocatorPublishesOnlyVerifiedAssignmentAndReplaysIdentically(t *testing.T) {
a, err := NewAllocator([]ReadyServer{{ServerID: "server-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: ServerReady}})
if err != nil {
t.Fatal(err)
}
allocation, err := a.Allocate(AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}, time.Unix(1000, 0))
if err != nil {
t.Fatal(err)
}
manifest := AllocationManifest{AllocationID: allocation.AllocationID, MatchID: allocation.MatchID, ServerID: allocation.ServerID, Region: allocation.Region, Build: allocation.Build, Protocol: allocation.Protocol, Transport: allocation.Transport, RosterDigest: "roster-1"}
digest := ManifestDigest(manifest)
verify := func(payload, signature []byte) bool {
return string(payload) == string(manifestBytes(manifest)) && string(signature) == string(digest[:])
}
if _, err := a.PublishAssignment("unknown", manifest, "127.0.0.1:30001", digest[:], verify); !errors.Is(err, ErrAllocationNotFound) {
t.Fatalf("unknown allocation error = %v", err)
}
bad := manifest
bad.Build = "build-2"
if _, err := a.PublishAssignment(allocation.AllocationID, bad, "127.0.0.1:30001", digest[:], verify); !errors.Is(err, ErrManifestRejected) {
t.Fatalf("tampered assignment error = %v", err)
}
first, err := a.PublishAssignment(allocation.AllocationID, manifest, "127.0.0.1:30001", digest[:], verify)
if err != nil {
t.Fatal(err)
}
replay, err := a.PublishAssignment(allocation.AllocationID, manifest, "127.0.0.1:30001", digest[:], verify)
if err != nil || replay != first {
t.Fatalf("assignment replay = %+v err=%v", replay, err)
}
if _, err := a.PublishAssignment(allocation.AllocationID, manifest, "127.0.0.1:30002", digest[:], verify); !errors.Is(err, ErrConflict) {
t.Fatalf("endpoint mutation error = %v", err)
}
}