package transport import ( "context" "net/http" "sync" "testing" "time" ) // fakeClock drives the throttle without real waiting. type fakeClock struct { mu sync.Mutex t time.Time slept time.Duration } func (c *fakeClock) now() time.Time { c.mu.Lock() defer c.mu.Unlock() return c.t } func (c *fakeClock) sleep(_ context.Context, d time.Duration) error { c.mu.Lock() defer c.mu.Unlock() c.t = c.t.Add(d) c.slept += d return nil } func newFakeThrottle() (*throttle, *fakeClock) { clk := &fakeClock{t: time.Date(2026, 10, 14, 3, 0, 0, 0, time.UTC)} th := newThrottle(quietLogger()) th.now = clk.now return th, clk } func TestThrottleUnthrottledByDefault(t *testing.T) { th, clk := newFakeThrottle() for i := 0; i < 5; i++ { if err := th.acquire(context.Background(), clk.sleep); err != nil { t.Fatal(err) } } if clk.slept != 0 { t.Errorf("slept %s without any 429, want 0", clk.slept) } } func TestThrottleRateLimitPausesAndSpacesRequests(t *testing.T) { th, clk := newFakeThrottle() th.rateLimited(5 * time.Second) if th.interval != minInterval { t.Fatalf("interval = %s, want %s", th.interval, minInterval) } _ = th.acquire(context.Background(), clk.sleep) // waits out the pause _ = th.acquire(context.Background(), clk.sleep) // then one interval later if want := 5*time.Second + minInterval; clk.slept != want { t.Errorf("slept %s, want %s", clk.slept, want) } } func TestThrottleConcurrent429sCountOnce(t *testing.T) { th, _ := newFakeThrottle() th.rateLimited(time.Second) th.rateLimited(time.Second) th.rateLimited(2 * time.Second) if th.interval != minInterval { t.Errorf("interval = %s, want %s (one slowdown per burst)", th.interval, minInterval) } if want := th.now().Add(2 * time.Second); !th.pauseUntil.Equal(want) { t.Errorf("pauseUntil = %s, want the longest Retry-After %s", th.pauseUntil, want) } } func TestThrottleBacksOffAndRecovers(t *testing.T) { th, clk := newFakeThrottle() for i := 0; i < 20; i++ { th.rateLimited(0) _ = clk.sleep(context.Background(), time.Millisecond) // next 429 arrives after the (zero) pause } if th.interval != maxInterval { t.Fatalf("interval = %s, want capped at %s", th.interval, maxInterval) } for i := 0; i < 1000 && th.interval > 0; i++ { th.succeeded() } if th.interval != 0 { t.Errorf("interval = %s after many successes, want 0", th.interval) } } func TestThrottleRespectsCancel(t *testing.T) { th, _ := newFakeThrottle() th.rateLimited(time.Hour) ctx, cancel := context.WithCancel(context.Background()) cancel() if err := th.acquire(ctx, sleepCtx); err == nil { t.Error("acquire must return the context error") } } func TestRetryAfter(t *testing.T) { now := time.Date(2026, 10, 14, 3, 0, 0, 0, time.UTC) h := http.Header{} h.Set("Retry-After", "7") if got := retryAfter(h, 1, now); got != 7*time.Second { t.Errorf("seconds: got %s", got) } h.Set("Retry-After", now.Add(30*time.Second).Format(http.TimeFormat)) if got := retryAfter(h, 1, now); got != 30*time.Second { t.Errorf("http date: got %s", got) } if got := retryAfter(http.Header{}, 2, now); got != 4*time.Second { t.Errorf("fallback backoff: got %s", got) } }