package track1 import ( "testing" "time" "github.com/stretchr/testify/assert" ) func TestInMemoryRateLimiter_Allow(t *testing.T) { config := RateLimitConfig{ RequestsPerSecond: 10, RequestsPerMinute: 100, BurstSize: 20, } limiter := NewInMemoryRateLimiter(config) key := "test-key" // Should allow first 100 requests for i := 0; i < 100; i++ { assert.True(t, limiter.Allow(key), "Request %d should be allowed", i) } // 101st request should be denied assert.False(t, limiter.Allow(key), "Request 101 should be denied") } func TestInMemoryRateLimiter_Reset(t *testing.T) { config := RateLimitConfig{ RequestsPerMinute: 10, } limiter := NewInMemoryRateLimiter(config) key := "test-key" // Exhaust limit for i := 0; i < 10; i++ { limiter.Allow(key) } assert.False(t, limiter.Allow(key)) // Wait for reset (1 minute) time.Sleep(61 * time.Second) // Should allow again after reset assert.True(t, limiter.Allow(key)) } func TestInMemoryRateLimiter_DifferentKeys(t *testing.T) { config := RateLimitConfig{ RequestsPerMinute: 10, } limiter := NewInMemoryRateLimiter(config) key1 := "key1" key2 := "key2" // Exhaust limit for key1 for i := 0; i < 10; i++ { limiter.Allow(key1) } assert.False(t, limiter.Allow(key1)) // key2 should still have full limit for i := 0; i < 10; i++ { assert.True(t, limiter.Allow(key2), "Request %d for key2 should be allowed", i) } } func TestInMemoryRateLimiter_Cleanup(t *testing.T) { config := RateLimitConfig{ RequestsPerMinute: 10, } limiter := NewInMemoryRateLimiter(config) key := "test-key" limiter.Allow(key) // Cleanup should remove old entries limiter.Cleanup() // Entry should still exist if not old enough // This test verifies cleanup doesn't break functionality assert.NotNil(t, limiter) }