Files
lincodex/backend/integration_test/redis_ratelimit_test.go
T

88 lines
3.5 KiB
Go

//go:build integration
package integration_test
import (
"errors"
"testing"
"time"
"lincodex/backend/internal/port/outbound"
)
func TestRedisRateLimiter(t *testing.T) {
env := bootstrap(t, redisService)
ctx := t.Context()
limiter := env.Redis
t.Run("admits up to the limit and then reports the wait", func(t *testing.T) {
rule := outbound.RateLimitRule{Window: 10 * time.Second, Limit: 2}
for attempt := 1; attempt <= 2; attempt++ {
decision, err := limiter.Take(ctx, "limit-basic", rule)
if err != nil || !decision.Allowed || decision.RetryAfter != 0 {
t.Fatalf("take %d = %+v, %v, want allowed", attempt, decision, err)
}
}
decision, err := limiter.Take(ctx, "limit-basic", rule)
if err != nil || decision.Allowed || decision.RetryAfter <= 0 || decision.RetryAfter > rule.Window {
t.Fatalf("third take = %+v, %v, want refused with a wait inside the window", decision, err)
}
})
t.Run("a refused take counts nothing in any rule", func(t *testing.T) {
interval := outbound.RateLimitRule{Window: 300 * time.Millisecond, Limit: 1}
daily := outbound.RateLimitRule{Window: time.Hour, Limit: 3}
if decision, err := limiter.Take(ctx, "limit-atomic", interval, daily); err != nil || !decision.Allowed {
t.Fatalf("first take = %+v, %v", decision, err)
}
// Refused by the interval: the daily counter must stay at 1.
for range 5 {
if decision, err := limiter.Take(ctx, "limit-atomic", interval, daily); err != nil || decision.Allowed {
t.Fatalf("take inside the interval = %+v, %v, want refused", decision, err)
}
}
time.Sleep(350 * time.Millisecond)
for attempt := 2; attempt <= 3; attempt++ {
decision, err := limiter.Take(ctx, "limit-atomic", interval, daily)
if err != nil || !decision.Allowed {
t.Fatalf("take %d after the interval = %+v, %v, want allowed", attempt, decision, err)
}
time.Sleep(350 * time.Millisecond)
}
decision, err := limiter.Take(ctx, "limit-atomic", interval, daily)
if err != nil || decision.Allowed || decision.RetryAfter < 50*time.Minute {
t.Fatalf("take over the daily limit = %+v, %v, want refused until the daily window ends", decision, err)
}
})
t.Run("keys are independent and windows reset", func(t *testing.T) {
rule := outbound.RateLimitRule{Window: 200 * time.Millisecond, Limit: 1}
for _, key := range []string{"limit-a", "limit-b"} {
if decision, err := limiter.Take(ctx, key, rule); err != nil || !decision.Allowed {
t.Fatalf("first take of %s = %+v, %v", key, decision, err)
}
}
time.Sleep(250 * time.Millisecond)
if decision, err := limiter.Take(ctx, "limit-a", rule); err != nil || !decision.Allowed {
t.Fatalf("take after the window = %+v, %v, want allowed", decision, err)
}
})
t.Run("rejects an invalid rule", func(t *testing.T) {
for name, rules := range map[string][]outbound.RateLimitRule{
"no rule": nil,
"zero limit": {{Window: time.Second, Limit: 0}},
"zero window": {{Window: 0, Limit: 1}},
"tiny window": {{Window: time.Microsecond, Limit: 1}},
"second fails": {{Window: time.Second, Limit: 1}, {Window: time.Second, Limit: -1}},
} {
if _, err := limiter.Take(ctx, "limit-invalid", rules...); !errors.Is(err, outbound.ErrInvalidRateLimitRule) {
t.Fatalf("Take(%s) error = %v, want ErrInvalidRateLimitRule", name, err)
}
}
if _, err := limiter.Take(ctx, "", outbound.RateLimitRule{Window: time.Second, Limit: 1}); !errors.Is(err, outbound.ErrInvalidRateLimitRule) {
t.Fatalf("Take with an empty key error = %v", err)
}
})
}