mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
feat(multiplayer): expose allocator quota metrics
This commit is contained in:
@@ -3,6 +3,7 @@ package allocator
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -56,11 +57,16 @@ func (d *durableSpy) RecordProviderAllocation(_ context.Context, allocation doma
|
||||
func TestServiceDurablyRecordsProviderAllocationBeforeReturning(t *testing.T) {
|
||||
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", MatchID: "m", ServerID: "gs", 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) }}
|
||||
metrics := NewMetrics()
|
||||
service := Service{Provider: provider, Durable: durable, Metrics: metrics, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
result, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, map[string]string{"region": "EU"})
|
||||
if err != nil || result.Endpoint == "" || durable.calls != 1 || durable.allocation.ServerID != "gs" {
|
||||
t.Fatalf("result=%+v err=%v durable=%+v", result, err, durable)
|
||||
}
|
||||
var output strings.Builder
|
||||
if err := metrics.WritePrometheus(&output); err != nil || !strings.Contains(output.String(), `allocations_total{region="EU"} 1`) {
|
||||
t.Fatalf("success metric err=%v output=%s", err, output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
|
||||
@@ -76,13 +82,19 @@ func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
|
||||
func TestServiceConsumesSharedQuotaBeforeFreshProviderCall(t *testing.T) {
|
||||
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
|
||||
quota := "aSpy{err: errors.New("quota exhausted")}
|
||||
service := Service{Provider: provider, Durable: &durableSpy{}, Quota: quota, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
metrics := NewMetrics()
|
||||
service := Service{Provider: provider, Durable: &durableSpy{}, Quota: quota, Metrics: metrics, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
if _, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, nil); err == nil {
|
||||
t.Fatal("quota rejection was ignored")
|
||||
}
|
||||
if quota.calls != 1 || provider.calls != 0 {
|
||||
t.Fatalf("quota/provider calls = %d/%d, want 1/0", quota.calls, provider.calls)
|
||||
}
|
||||
var output strings.Builder
|
||||
_ = metrics.WritePrometheus(&output)
|
||||
if !strings.Contains(output.String(), `quota_denials_total{region="EU"} 1`) {
|
||||
t.Fatalf("quota denial metric missing: %s", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceConsumesSharedQuotaOnceWhenReconcilingProviderResult(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user