mirror of
https://github.com/hanzoai/world-zap.git
synced 2026-10-30 14:55:43 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-Authored-By: Claude Opus 5 <[email protected]>
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package ratelimit
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestPlanCapacity(t *testing.T) {
|
|
cases := map[string]int{
|
|
"free": 30,
|
|
"pro": 3000,
|
|
"team": 15000,
|
|
"enterprise": 60000,
|
|
"": 30,
|
|
"unknown": 30,
|
|
}
|
|
for plan, want := range cases {
|
|
if got := PlanCapacity(plan); got != want {
|
|
t.Errorf("plan %q: got %d want %d", plan, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFreeBurstThenBlock(t *testing.T) {
|
|
l := New()
|
|
for i := range 30 {
|
|
d := l.TryAcquire("u", "free")
|
|
if !d.Allowed {
|
|
t.Fatalf("request %d denied: %+v", i, d)
|
|
}
|
|
}
|
|
d := l.TryAcquire("u", "free")
|
|
if d.Allowed {
|
|
t.Fatalf("31st request should be denied")
|
|
}
|
|
if d.RetryIn <= 0 {
|
|
t.Fatalf("expected retry hint")
|
|
}
|
|
}
|
|
|
|
func TestPerUserIsolation(t *testing.T) {
|
|
l := New()
|
|
for range 30 {
|
|
l.TryAcquire("a", "free")
|
|
}
|
|
if l.TryAcquire("a", "free").Allowed {
|
|
t.Fatalf("a should be throttled")
|
|
}
|
|
if !l.TryAcquire("b", "free").Allowed {
|
|
t.Fatalf("b should be allowed")
|
|
}
|
|
}
|
|
|
|
func TestPlanUpgradeResetsBucket(t *testing.T) {
|
|
l := New()
|
|
// burn free tokens
|
|
for range 30 {
|
|
l.TryAcquire("u", "free")
|
|
}
|
|
if l.TryAcquire("u", "free").Allowed {
|
|
t.Fatalf("free should be empty")
|
|
}
|
|
// Upgrade to pro; new bucket at pro capacity
|
|
d := l.TryAcquire("u", "pro")
|
|
if !d.Allowed {
|
|
t.Fatalf("pro upgrade should grant tokens: %+v", d)
|
|
}
|
|
}
|