| index 6ac865621..72bc8e0ea 100644 |
| --- a/internal/edge/fleetforward_policy_test.go |
| +++ b/internal/edge/fleetforward_policy_test.go |
| @@ -13,6 +13,8 @@ package edge |
| |
| import ( |
| "bufio" |
| + "context" |
| + "errors" |
| "fmt" |
| "io" |
| "net" |
| @@ -84,29 +86,46 @@ func (b *polBackend) counts() (gets, upgrades int) { |
| // polHost carries polHubRate, the box's own row polBoxRate, and both edges' |
| // limiters are on a frozen clock. |
| func policyScene(t *testing.T) (hubURL string, hub, box *Edge, app *polBackend) { |
| + t.Helper() |
| + return policySceneRates(t, polHubRate, polBoxRate) |
| +} |
| + |
| +// policySceneRates is policyScene with the two rows' rate limits given, so a |
| +// plan-throttle test can keep the per-instance limit out of its way. Both |
| +// edges' plan limiters are on the same frozen clock as their rate limiters. |
| +func policySceneRates(t *testing.T, hubRate, boxRate string) (hubURL string, hub, box *Edge, app *polBackend) { |
| t.Helper() |
| t.Setenv("CARLOS_RATE_LIMIT", "enforce") |
| app = &polBackend{closed: make(chan struct{}, 8)} |
| appAddr := startPolBackend(t, app) |
| fwd := forwardRow(polHost, "home", fwdOwner) |
| - fwd.RateLimit = polHubRate |
| + fwd.RateLimit = hubRate |
| hubURL, hub, box = forwardHubAndBox(t, []registry.Route{fwd}, |
| - []registry.Route{{Host: polHost, Kind: "instance", Status: "ready", Addr: appAddr, Account: fwdOwner, App: "shop", RateLimit: polBoxRate}}, |
| + []registry.Route{{Host: polHost, Kind: "instance", Status: "ready", Addr: appAddr, Account: fwdOwner, App: "shop", RateLimit: boxRate}}, |
| nil) |
| for name, e := range map[string]*Edge{"hub": hub, "box": box} { |
| if e.rateMode != RateEnforce { |
| t.Fatalf("the %s's rate mode is %v, want enforce", name, e.rateMode) |
| } |
| } |
| - if got := hub.current()[polHost].rate; got.Burst != 2 { |
| - t.Fatalf("the hub's forward row parsed to %+v, want burst 2 (the row's RateLimit)", got) |
| - } |
| - if got := box.current()[polHost].rate; got.Burst != 1 { |
| - t.Fatalf("the box's row parsed to %+v, want burst 1", got) |
| + for _, c := range []struct { |
| + name string |
| + e *Edge |
| + col string |
| + }{{"hub's forward row", hub, hubRate}, {"box's row", box, boxRate}} { |
| + want, err := ratelimit.Parse(c.col) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := c.e.current()[polHost].rate; got.Burst != want.Burst { |
| + t.Fatalf("the %s parsed to %+v, want burst %d (the row's RateLimit)", c.name, got, want.Burst) |
| + } |
| } |
| frozen := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC) |
| - hub.rateLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000) |
| - box.rateLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000) |
| + for _, e := range []*Edge{hub, box} { |
| + e.rateLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000) |
| + e.planLimiter = ratelimit.NewLimiter(func() time.Time { return frozen }, 1000) |
| + } |
| return hubURL, hub, box, app |
| } |
| |
| @@ -257,3 +276,90 @@ func TestForwardPlanThrottleAndUpgradeClose(t *testing.T) { |
| t.Fatalf("CloseAccountUpgrades after the handshake ended closed %d, want 0", n) |
| } |
| } |
| + |
| +// polWideRate keeps the per-instance limit out of a plan-throttle test's |
| +// way: a burst far above plans.ThrottleRule's, on both rows. |
| +const polWideRate = `{"rate":1,"burst":500}` |
| + |
| +// TestForwardPlanThrottlePlainHTTPAtTheHub (deferred minor, Task 5): a plain |
| +// request to a forward row spends the account's plan-throttle bucket at the |
| +// hub. The rule's burst is admitted and reaches the app; the next request is |
| +// the hub's 429 and never crosses the tunnel. |
| +func TestForwardPlanThrottlePlainHTTPAtTheHub(t *testing.T) { |
| + hubURL, hub, _, app := policySceneRates(t, polWideRate, polWideRate) |
| + hub.SetPlanThrottleFor(func(rt registry.Route) (plans.Throttle, bool) { |
| + if rt.Account != fwdOwner { |
| + return plans.Throttle{}, false |
| + } |
| + return plans.Throttle{Bandwidth: true, Until: time.Now().Add(time.Hour)}, true |
| + }) |
| + for i := 1; i <= plans.ThrottleRule.Burst; i++ { |
| + resp, body := fwdDo(t, hubURL, "GET", polHost, "/", "", nil) |
| + if resp.StatusCode != http.StatusOK || string(body) != "ok" { |
| + t.Fatalf("GET %d: %d %q, want the app's 200 within the plan throttle's burst of %d", i, resp.StatusCode, body, plans.ThrottleRule.Burst) |
| + } |
| + } |
| + resp, body := fwdDo(t, hubURL, "GET", polHost, "/", "", nil) |
| + wantHubRefusal(t, resp, string(body), "a GET past the plan throttle's burst") |
| + if gets, _ := app.counts(); gets != plans.ThrottleRule.Burst { |
| + t.Fatalf("the app saw %d requests, want %d: the refused one never crosses the tunnel", gets, plans.ThrottleRule.Burst) |
| + } |
| +} |
| + |
| +// TestForwardPlanThrottleExemptsTheTunnelAtTheBox (deferred minor, Task 5): |
| +// the fleet box keeps exempting what arrives over the tunnel from the plan |
| +// throttle (planThrottleOf's fromTunnel), so a throttle the box holds for |
| +// the account does not judge the hub's forwarded requests a second time. The |
| +// hub holds none here: every request past the rule's burst still reaches the |
| +// app. |
| +func TestForwardPlanThrottleExemptsTheTunnelAtTheBox(t *testing.T) { |
| + hubURL, _, box, app := policySceneRates(t, polWideRate, polWideRate) |
| + box.SetPlanThrottleFor(func(rt registry.Route) (plans.Throttle, bool) { |
| + if rt.Account != fwdOwner { |
| + return plans.Throttle{}, false |
| + } |
| + return plans.Throttle{Bandwidth: true, Until: time.Now().Add(time.Hour)}, true |
| + }) |
| + n := plans.ThrottleRule.Burst + 5 |
| + for i := 1; i <= n; i++ { |
| + resp, body := fwdDo(t, hubURL, "GET", polHost, "/", "", nil) |
| + if resp.StatusCode != http.StatusOK || string(body) != "ok" { |
| + t.Fatalf("GET %d: %d %q, want the app's 200: the box must not plan-throttle a tunneled request", i, resp.StatusCode, body) |
| + } |
| + } |
| + if gets, _ := app.counts(); gets != n { |
| + t.Fatalf("the app saw %d requests, want %d", gets, n) |
| + } |
| +} |
| + |
| +// TestForwardUpgradeCloseCancelsTheRequest (deferred minor, Task 5): |
| +// CloseAccountUpgrades does not only count a forward row's tracked upgrade, |
| +// it cancels the context of the request it tracked, which is what ends the |
| +// proxied connection. Driven directly, since no upgrade stays open across the |
| +// tunnel (#471). |
| +func TestForwardUpgradeCloseCancelsTheRequest(t *testing.T) { |
| + _, hub, _, _ := policyScene(t) |
| + hub.SetPlanGovernedFor(func(rt registry.Route) bool { return rt.Account == fwdOwner }) |
| + up := hub.current()[polHost] |
| + if !up.route.IsForward() { |
| + t.Fatalf("the hub's row for %s is %+v, want its forward row", polHost, up.route) |
| + } |
| + req, err := http.NewRequest("GET", "http://"+polHost+"/socket", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + tracked, done := hub.trackUpgrade(req, up) |
| + defer done() |
| + if tracked.Context().Err() != nil { |
| + t.Fatalf("the tracked request is done before any close: %v", tracked.Context().Err()) |
| + } |
| + if n := hub.CloseAccountUpgrades(fwdOwner); n != 1 { |
| + t.Fatalf("CloseAccountUpgrades(%q) = %d, want 1", fwdOwner, n) |
| + } |
| + if err := tracked.Context().Err(); !errors.Is(err, context.Canceled) { |
| + t.Fatalf("after CloseAccountUpgrades the tracked request's context is %v, want canceled", err) |
| + } |
| + if req.Context().Err() != nil { |
| + t.Fatal("CloseAccountUpgrades cancelled the caller's own context, not the tracked one") |
| + } |
| +} |