コード例 #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"))
}
コード例 #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)
}
コード例 #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"))
}
コード例 #4
0
func TestMemoryLimiter(t *testing.T) {
	rf := limiter.RateLimiter(limiter.UseMemory(), limiter.BucketLimit(1000*time.Millisecond, 10))
	mr := rf()
	assertRateLimiter(mr, t)
}