summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/application_rate_limiter
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/application_rate_limiter')
-rw-r--r--spec/lib/gitlab/application_rate_limiter/base_strategy_spec.rb17
-rw-r--r--spec/lib/gitlab/application_rate_limiter/increment_per_action_spec.rb51
-rw-r--r--spec/lib/gitlab/application_rate_limiter/increment_per_actioned_resource_spec.rb52
3 files changed, 120 insertions, 0 deletions
diff --git a/spec/lib/gitlab/application_rate_limiter/base_strategy_spec.rb b/spec/lib/gitlab/application_rate_limiter/base_strategy_spec.rb
new file mode 100644
index 00000000000..b34ac538b24
--- /dev/null
+++ b/spec/lib/gitlab/application_rate_limiter/base_strategy_spec.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::ApplicationRateLimiter::BaseStrategy do
+ describe '#increment' do
+ it 'raises NotImplementedError' do
+ expect { subject.increment('cache_key', 0) }.to raise_error(NotImplementedError)
+ end
+ end
+
+ describe '#read' do
+ it 'raises NotImplementedError' do
+ expect { subject.read('cache_key') }.to raise_error(NotImplementedError)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/application_rate_limiter/increment_per_action_spec.rb b/spec/lib/gitlab/application_rate_limiter/increment_per_action_spec.rb
new file mode 100644
index 00000000000..b74d2360711
--- /dev/null
+++ b/spec/lib/gitlab/application_rate_limiter/increment_per_action_spec.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::ApplicationRateLimiter::IncrementPerAction, :freeze_time, :clean_gitlab_redis_rate_limiting do
+ let(:cache_key) { 'test' }
+ let(:expiry) { 60 }
+
+ subject(:counter) { described_class.new }
+
+ def increment
+ counter.increment(cache_key, expiry)
+ end
+
+ describe '#increment' do
+ it 'increments per call' do
+ expect(increment).to eq 1
+ expect(increment).to eq 2
+ expect(increment).to eq 3
+ end
+
+ it 'sets time to live (TTL) for the key' do
+ def ttl
+ Gitlab::Redis::RateLimiting.with { |r| r.ttl(cache_key) }
+ end
+
+ key_does_not_exist = -2
+
+ expect(ttl).to eq key_does_not_exist
+ expect { increment }.to change { ttl }.by(a_value > 0)
+ end
+ end
+
+ describe '#read' do
+ def read
+ counter.read(cache_key)
+ end
+
+ it 'returns 0 when there is no data' do
+ expect(read).to eq 0
+ end
+
+ it 'returns the correct value', :aggregate_failures do
+ increment
+ expect(read).to eq 1
+
+ increment
+ expect(read).to eq 2
+ end
+ end
+end
diff --git a/spec/lib/gitlab/application_rate_limiter/increment_per_actioned_resource_spec.rb b/spec/lib/gitlab/application_rate_limiter/increment_per_actioned_resource_spec.rb
new file mode 100644
index 00000000000..1f3ae2d749a
--- /dev/null
+++ b/spec/lib/gitlab/application_rate_limiter/increment_per_actioned_resource_spec.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::ApplicationRateLimiter::IncrementPerActionedResource,
+ :freeze_time, :clean_gitlab_redis_rate_limiting do
+ let(:cache_key) { 'test' }
+ let(:expiry) { 60 }
+
+ def increment(resource_key)
+ described_class.new(resource_key).increment(cache_key, expiry)
+ end
+
+ describe '#increment' do
+ it 'increments per resource', :aggregate_failures do
+ expect(increment('resource_1')).to eq(1)
+ expect(increment('resource_1')).to eq(1)
+ expect(increment('resource_2')).to eq(2)
+ expect(increment('resource_2')).to eq(2)
+ expect(increment('resource_3')).to eq(3)
+ end
+
+ it 'sets time to live (TTL) for the key' do
+ def ttl
+ Gitlab::Redis::RateLimiting.with { |r| r.ttl(cache_key) }
+ end
+
+ key_does_not_exist = -2
+
+ expect(ttl).to eq key_does_not_exist
+ expect { increment('resource_1') }.to change { ttl }.by(a_value > 0)
+ end
+ end
+
+ describe '#read' do
+ def read
+ described_class.new(nil).read(cache_key)
+ end
+
+ it 'returns 0 when there is no data' do
+ expect(read).to eq 0
+ end
+
+ it 'returns the correct value', :aggregate_failures do
+ increment 'r1'
+ expect(read).to eq 1
+
+ increment 'r2'
+ expect(read).to eq 2
+ end
+ end
+end