| new file mode 100644 |
| index 000000000..2201dda2b |
| --- /dev/null |
| +++ b/internal/host/plans_throttle_test.go |
| @@ -0,0 +1,92 @@ |
| +package host |
| + |
| +// Serial: these write the package-level plans cache. |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/plans" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// putThrottledLimits writes sqid's record with a throttle. |
| +func putThrottledLimits(t *testing.T, s *plansCountingStore, sqid string, th *plans.Throttle) { |
| + t.Helper() |
| + ctx := context.Background() |
| + _, etag, err := plans.GetLimits(ctx, s, sqid) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r := &plans.Record{Account: sqid, CatalogID: "v1@0123456789ab", Plan: "free", Revision: 1, Throttle: th} |
| + if _, err := plans.PutLimits(ctx, s, r, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +var ( |
| + throttledRoute = registry.Route{Host: "a.example", Kind: "instance", Account: "bab", App: "web"} |
| + otherRoute = registry.Route{Host: "b.example", Kind: "instance", Account: "bdk", App: "shop"} |
| +) |
| + |
| +func TestPlansPassKeepsThrottle(t *testing.T) { |
| + s, reg := plansWorld(t) |
| + putMarker(t, s, true) |
| + putThrottledLimits(t, s, "bab", &plans.Throttle{Bandwidth: true, Until: time.Now().Add(24 * time.Hour)}) |
| + putAccountLimits(t, s, "bdk", 90) |
| + plansPass(context.Background(), s, reg, plansNoEnv) |
| + if th, ok := planThrottleFor(throttledRoute); !ok || !th.Bandwidth { |
| + t.Fatalf("bab throttle = %+v, %v", th, ok) |
| + } |
| + if _, ok := planThrottleFor(otherRoute); ok { |
| + t.Fatal("an unthrottled account is throttled") |
| + } |
| +} |
| + |
| +func TestExpiredThrottleIsIgnored(t *testing.T) { |
| + s, reg := plansWorld(t) |
| + putMarker(t, s, true) |
| + putThrottledLimits(t, s, "bab", &plans.Throttle{Bandwidth: true, Awake: true, Until: time.Now().Add(-time.Minute)}) |
| + plansPass(context.Background(), s, reg, plansNoEnv) |
| + if _, ok := planThrottleFor(throttledRoute); ok { |
| + t.Fatal("a throttle past Until still applies") |
| + } |
| + if d := planMaxRunFor(throttledRoute); d != 0 { |
| + t.Fatalf("an expired awake throttle gives a ceiling of %v", d) |
| + } |
| +} |
| + |
| +func TestPlanMaxRunFor(t *testing.T) { |
| + t.Cleanup(func() { planLimits.Store(nil) }) |
| + until := time.Now().Add(time.Hour) |
| + planLimits.Store(&planLimitsSnapshot{byAccount: map[string]plans.Limits{}, throttle: map[string]plans.Throttle{ |
| + "bab": {Awake: true, Until: until}, "bdk": {Bandwidth: true, Until: until}, |
| + }}) |
| + if d := planMaxRunFor(throttledRoute); d != 15*time.Minute { |
| + t.Fatalf("awake-throttled instance: %v, want 15m", d) |
| + } |
| + if d := planMaxRunFor(otherRoute); d != 0 { |
| + t.Fatalf("bandwidth-only throttle: %v, want 0", d) |
| + } |
| + worker := throttledRoute |
| + worker.Kind = "worker" |
| + if d := planMaxRunFor(worker); d != 0 { |
| + t.Fatalf("worker route: %v, want 0", d) |
| + } |
| +} |
| + |
| +func TestThrottleOffWithPlanLimitsOff(t *testing.T) { |
| + s, reg := plansWorld(t) |
| + putMarker(t, s, true) |
| + putThrottledLimits(t, s, "bab", &plans.Throttle{Bandwidth: true, Until: time.Now().Add(time.Hour)}) |
| + plansPass(context.Background(), s, reg, func(k string) string { |
| + if k == "CARLOS_PLAN_LIMITS" { |
| + return "off" |
| + } |
| + return "" |
| + }) |
| + if _, ok := planThrottleFor(throttledRoute); ok { |
| + t.Fatal("throttled with CARLOS_PLAN_LIMITS=off") |
| + } |
| +} |