package web import ( "testing" "time" ) func TestIPLimiterWindow(t *testing.T) { limiter := newIPLimiter(2, time.Hour) if !limiter.allow("a") || !limiter.allow("a") { t.Fatal("first two hits for one key should be allowed") } if limiter.allow("a") { t.Error("third hit within the window was allowed, want denied") } if !limiter.allow("b") { t.Error("a different key should have its own budget") } } func TestIPLimiterExpiry(t *testing.T) { limiter := newIPLimiter(1, time.Millisecond) if !limiter.allow("a") { t.Fatal("first hit should be allowed") } time.Sleep(5 * time.Millisecond) if !limiter.allow("a") { t.Error("hit after the window should be allowed") } }