mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
fix(multiplayer): recover ambiguous agones allocations
This commit is contained in:
@@ -154,3 +154,37 @@ func TestListReadyServersFailsClosedOnInvalidReadyCompatibility(t *testing.T) {
|
||||
t.Fatal("invalid Ready GameServer accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverAllocationFindsMatchingAllocatedGameServer(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || !strings.HasSuffix(r.URL.Path, "/gameservers") {
|
||||
t.Fatalf("request=%s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"gs-recovered","labels":{"cosmic-clash.io/region":"EU","cosmic-clash.io/build":"build-1","cosmic-clash.io/protocol":"1","cosmic-clash.io/transport":"enet"},"annotations":{"cosmic-clash.io/allocation-id":"allocation-1","cosmic-clash.io/match-id":"match-1"}},"status":{"state":"Allocated","address":"127.0.0.1","ports":[{"name":"default","port":31001}]}},{"metadata":{"name":"gs-other","annotations":{"cosmic-clash.io/allocation-id":"other"},"status":{"state":"Allocated"}}}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
recovered, found, err := (Client{BaseURL: server.URL, Namespace: "games", HTTP: server.Client()}).RecoverAllocation(context.Background(), request(), time.Unix(1000, 0))
|
||||
if err != nil || !found || recovered.GameServer != "gs-recovered" || recovered.Endpoint != "127.0.0.1:31001" || recovered.Allocation.ServerID != "gs-recovered" {
|
||||
t.Fatalf("recovered=%+v found=%t err=%v", recovered, found, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverAllocationRejectsMismatchedBinding(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"gs-forged","labels":{"cosmic-clash.io/region":"NA","cosmic-clash.io/build":"build-1","cosmic-clash.io/protocol":"1","cosmic-clash.io/transport":"enet"},"annotations":{"cosmic-clash.io/allocation-id":"allocation-1","cosmic-clash.io/match-id":"other-match"}},"status":{"state":"Allocated","address":"127.0.0.1","ports":[{"name":"default","port":31001}]}}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
if _, found, err := (Client{BaseURL: server.URL, Namespace: "games", HTTP: server.Client()}).RecoverAllocation(context.Background(), request(), time.Unix(1000, 0)); err == nil || found {
|
||||
t.Fatalf("mismatched recovery accepted: found=%t err=%v", found, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverAllocationRejectsDuplicateProviderMatches(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"gs-one","labels":{"cosmic-clash.io/region":"EU","cosmic-clash.io/build":"build-1","cosmic-clash.io/protocol":"1","cosmic-clash.io/transport":"enet"},"annotations":{"cosmic-clash.io/allocation-id":"allocation-1","cosmic-clash.io/match-id":"match-1"}},"status":{"state":"Allocated","address":"127.0.0.1","ports":[{"name":"default","port":31001}]}},{"metadata":{"name":"gs-two","labels":{"cosmic-clash.io/region":"EU","cosmic-clash.io/build":"build-1","cosmic-clash.io/protocol":"1","cosmic-clash.io/transport":"enet"},"annotations":{"cosmic-clash.io/allocation-id":"allocation-1","cosmic-clash.io/match-id":"match-1"}},"status":{"state":"Allocated","address":"127.0.0.1","ports":[{"name":"default","port":31002}]}}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
if _, found, err := (Client{BaseURL: server.URL, Namespace: "games", HTTP: server.Client()}).RecoverAllocation(context.Background(), request(), time.Unix(1000, 0)); err == nil || found {
|
||||
t.Fatalf("duplicate recovery accepted: found=%t err=%v", found, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user