Esempio n. 1
0
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"))
}
Esempio n. 2
0
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)
}
Esempio n. 3
0
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"))
}