Add spec for ActionRateLimiter
This commit is contained in:
parent
ef78f67f4a
commit
8b939bfab7
|
@ -16,12 +16,12 @@ module Gitlab
|
|||
value = 0
|
||||
|
||||
Gitlab::Redis::Cache.with do |redis|
|
||||
cache_key = "action_rate_limiter:#{action}:#{key}"
|
||||
cache_key = "action_rate_limiter:#{action.to_s}:#{key}"
|
||||
value = redis.incr(cache_key)
|
||||
redis.expire(cache_key, expiry_time) if value == 1
|
||||
end
|
||||
|
||||
value.to_i
|
||||
value
|
||||
end
|
||||
|
||||
def throttled?(key, threshold_value)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Gitlab::ActionRateLimiter do
|
||||
let(:redis) { double('redis') }
|
||||
let(:key) { 'user:1' }
|
||||
let(:cache_key) { "action_rate_limiter:test_action:#{key}" }
|
||||
|
||||
subject { described_class.new(action: :test_action, expiry_time: 100) }
|
||||
|
||||
before do
|
||||
allow(Gitlab::Redis::Cache).to receive(:with).and_yield(redis)
|
||||
end
|
||||
|
||||
it 'increases the throttle count and sets the expire time' do
|
||||
expect(redis).to receive(:incr).with(cache_key).and_return(1)
|
||||
expect(redis).to receive(:expire).with(cache_key, 100)
|
||||
|
||||
expect(subject.throttled?(key, 1)).to be false
|
||||
end
|
||||
|
||||
it 'returns true if the key is throttled' do
|
||||
expect(redis).to receive(:incr).with(cache_key).and_return(2)
|
||||
expect(redis).not_to receive(:expire)
|
||||
|
||||
expect(subject.throttled?(key, 1)).to be true
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue