func TestRedisLimiter(t *testing.T) { clearUpRedis() rf := limiter.RateLimiter(limiter.UseRedis(redisPool), limiter.BucketLimit(1000*time.Millisecond, 10)) rr := rf() assertRateLimiter(rr, t) assert.Equal(t, 100, size("rl-key1")) }
func TestFloodThreshold(t *testing.T) { clearUpRedis() rf := limiter.RateLimiter(limiter.UseRedis(redisPool), limiter.BucketLimit(1000*time.Millisecond, 10), limiter.FloodThreshold(5)) rr := rf() assertRateLimiter(rr, t) assert.Equal(t, 51, size("rl-key1")) time.Sleep(1500 * time.Millisecond) assertRateLimiter(rr, t) }
func TestMinPeriod(t *testing.T) { clearUpRedis() rf := limiter.RateLimiter(limiter.UseRedis(redisPool), limiter.MinPeriod(10*time.Millisecond)) allow := rf() assert.True(t, allow("key1")) assert.False(t, allow("key1")) assert.True(t, allow("key2")) time.Sleep(15 * time.Millisecond) assert.True(t, allow("key1")) assert.False(t, allow("key1")) assert.True(t, allow("key2")) }
func TestMemoryLimiter(t *testing.T) { rf := limiter.RateLimiter(limiter.UseMemory(), limiter.BucketLimit(1000*time.Millisecond, 10)) mr := rf() assertRateLimiter(mr, t) }