Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
32 lines
852 B
Go
32 lines
852 B
Go
package adminhandler
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRateLimiterPrunesExpiredKeysAndPreservesLiveLimits(t *testing.T) {
|
|
window := time.Minute
|
|
now := time.Now()
|
|
limiter := newRateLimiter(2, window)
|
|
limiter.hits["expired"] = []time.Time{now.Add(-2 * window)}
|
|
limiter.hits["live"] = []time.Time{now.Add(-time.Second)}
|
|
limiter.lastPrune = now.Add(-2 * window)
|
|
|
|
if !limiter.allow("live") {
|
|
t.Fatal("allow rejected a live key below its limit")
|
|
}
|
|
if _, ok := limiter.hits["expired"]; ok {
|
|
t.Fatal("expired key was not pruned")
|
|
}
|
|
if got := len(limiter.hits["live"]); got != 2 {
|
|
t.Fatalf("live key hit count = %d, want 2", got)
|
|
}
|
|
if limiter.allow("live") {
|
|
t.Fatal("allow accepted a live key at its limit")
|
|
}
|
|
if got := len(limiter.hits["live"]); got != 2 {
|
|
t.Fatalf("live key hit count after rejection = %d, want 2", got)
|
|
}
|
|
}
|