package ratelimit import ( "context" "sync" "testing" "time" ) // FakeClock moves forward only when something waits on it, by exactly the // time waited. type FakeClock struct { mu sync.Mutex now time.Time } func NewFakeClock() *FakeClock { return &FakeClock{now: time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)} } func (c *FakeClock) Now() time.Time { c.mu.Lock() defer c.mu.Unlock() return c.now } func (c *FakeClock) After(d time.Duration) <-chan time.Time { c.mu.Lock() defer c.mu.Unlock() c.now = c.now.Add(d) ch := make(chan time.Time, 1) ch <- c.now return ch } func fixedRand(v float64) Option { return WithRand(func() float64 { return v }) } func TestHostFloor(t *testing.T) { ctx := context.Background() for _, r := range []float64{0, 0.5, 0.999} { clock := NewFakeClock() l := New(nil, WithClock(clock), fixedRand(r)) if err := l.Wait(ctx, "www.amazon.de"); err != nil { t.Fatal(err) } first := clock.Now() // Another host is not held up. if err := l.Wait(ctx, "www.amazon.co.uk"); err != nil { t.Fatal(err) } if !clock.Now().Equal(first) { t.Errorf("rand %v: a request to another host waited", r) } if err := l.Wait(ctx, "WWW.AMAZON.DE"); err != nil { t.Fatal(err) } gap := clock.Now().Sub(first) if gap < HostFloor || gap > time.Duration(float64(HostFloor)*(1+Jitter)) { t.Errorf("rand %v: gap %s, want between %s and %s", r, gap, HostFloor, time.Duration(float64(HostFloor)*(1+Jitter))) } } } func TestURLFloor(t *testing.T) { clock := NewFakeClock() l := New(nil, WithClock(clock), fixedRand(0)) // shortest jitter: -20% const url = "https://www.amazon.de/dp/B0HMX2FSTF" if next := l.NextCheck(url, time.Minute); !next.Equal(clock.Now()) { t.Errorf("never checked: next %s, want now", next) } if err := l.WaitCheck(context.Background(), url); err != nil { t.Fatal(err) } last := clock.Now() // 60s - 20% would be 48s, and an interval below the floor (which the // config rejects anyway) must not get through either. for _, interval := range []time.Duration{time.Minute, time.Second} { if next := l.NextCheck(url, interval); next.Sub(last) < URLFloor { t.Errorf("interval %s: next check %s after the last, below the floor", interval, next.Sub(last)) } } if ready := l.URLReady(url); ready.Sub(last) != URLFloor { t.Errorf("URLReady %s after the last check, want %s", ready.Sub(last), URLFloor) } if next := l.NextCheck(url, 10*time.Minute); next.Sub(last) != 8*time.Minute { t.Errorf("10m interval at -20%%: next after %s, want 8m", next.Sub(last)) } } func TestBackoff(t *testing.T) { ctx := context.Background() clock := NewFakeClock() l := New(nil, WithClock(clock), fixedRand(0.5)) // no jitter for i, want := range []time.Duration{2, 4, 8, 16, 30, 30} { if got := l.Failure("www.amazon.de"); got != want*time.Minute { t.Errorf("failure %d: backoff %s, want %dm", i+1, got, want) } } start := clock.Now() if err := l.Wait(ctx, "www.amazon.de"); err != nil { t.Fatal(err) } if waited := clock.Now().Sub(start); waited != 30*time.Minute { t.Errorf("waited %s for a backed-off host, want 30m", waited) } l.Success("www.amazon.de") if got := l.Failure("www.amazon.de"); got != 2*time.Minute { t.Errorf("after a success: backoff %s, want it to start over at 2m", got) } }