FIX: Should flush rate limit keys before testing it

This commit is contained in:
Robin Ward 2015-01-29 11:44:51 -05:00
parent 3a24df6956
commit 6e862e0e59
3 changed files with 14 additions and 7 deletions

View File

@ -63,9 +63,9 @@ class DiscourseRedis
@redis.del k
end
def keys
def keys(pattern=nil)
len = DiscourseRedis.namespace.length + 1
@redis.keys("#{DiscourseRedis.namespace}:*").map{
@redis.keys("#{DiscourseRedis.namespace}:#{pattern || '*'}").map{
|k| k[len..-1]
}
end

View File

@ -4,6 +4,8 @@ require_dependency 'rate_limiter/on_create_record'
# A redis backed rate limiter.
class RateLimiter
KEY_PREFIX = "l-rate-limit:"
def self.disable
@disabled = true
end
@ -17,9 +19,13 @@ class RateLimiter
@disabled || Rails.env.test?
end
def self.clear_all!
$redis.keys("#{KEY_PREFIX}:*").each { |k| $redis.del(k) }
end
def initialize(user, key, max, secs)
@user = user
@key = "l-rate-limit:#{@user && @user.id}:#{key}"
@key = "#{KEY_PREFIX}:#{@user && @user.id}:#{key}"
@max = max
@secs = secs
end
@ -64,10 +70,10 @@ class RateLimiter
end
def is_under_limit?
# number of events in buffer less than max allowed? OR
($redis.llen(@key) < @max) ||
# age bigger than silding window size?
(age_of_oldest > @secs)
# number of events in buffer less than max allowed? OR
($redis.llen(@key) < @max) ||
# age bigger than silding window size?
(age_of_oldest > @secs)
end
def rate_unlimited?

View File

@ -1245,6 +1245,7 @@ describe Topic do
SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)
RateLimiter.stubs(:rate_limit_create_topic).returns(100)
RateLimiter.stubs(:disabled?).returns(false)
RateLimiter.clear_all!
start = Time.now.tomorrow.beginning_of_day