summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/sidekiq_queue_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-03 15:08:08 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-03 15:08:08 +0000
commit946771d0b016ae92b15a60bc3290a33b94191ffe (patch)
tree64862c2433989483f5fce45d5539242577a362eb /spec/lib/gitlab/sidekiq_queue_spec.rb
parentf1e2fca19a90a6992c2020cf8c2159cfb0b61bca (diff)
downloadgitlab-ce-946771d0b016ae92b15a60bc3290a33b94191ffe.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/sidekiq_queue_spec.rb')
-rw-r--r--spec/lib/gitlab/sidekiq_queue_spec.rb94
1 files changed, 94 insertions, 0 deletions
diff --git a/spec/lib/gitlab/sidekiq_queue_spec.rb b/spec/lib/gitlab/sidekiq_queue_spec.rb
new file mode 100644
index 00000000000..7a4d47563b6
--- /dev/null
+++ b/spec/lib/gitlab/sidekiq_queue_spec.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::SidekiqQueue do
+ around do |example|
+ Sidekiq::Queue.new('authorized_projects').clear
+ Sidekiq::Testing.disable!(&example)
+ Sidekiq::Queue.new('authorized_projects').clear
+ end
+
+ def add_job(user)
+ Sidekiq::Client.push(
+ 'class' => 'AuthorizedProjectsWorker',
+ 'queue' => 'authorized_projects',
+ 'args' => [user.id],
+ 'meta.user' => user.username
+ )
+ end
+
+ describe '#drop_jobs!' do
+ shared_examples 'queue processing' do
+ let(:sidekiq_queue) { described_class.new('authorized_projects') }
+ let_it_be(:sidekiq_queue_user) { create(:user) }
+
+ before do
+ add_job(create(:user))
+ add_job(sidekiq_queue_user)
+ add_job(sidekiq_queue_user)
+ end
+
+ context 'when the queue is not processed in time' do
+ before do
+ calls = 0
+
+ allow(sidekiq_queue).to receive(:job_matches?).and_wrap_original do |m, *args|
+ raise Timeout::Error if calls > 0
+
+ calls += 1
+ m.call(*args)
+ end
+ end
+
+ it 'returns a non-completion flag, the number of jobs deleted, and the remaining queue size' do
+ expect(sidekiq_queue.drop_jobs!(search_metadata, timeout: 10))
+ .to eq(completed: false,
+ deleted_jobs: timeout_deleted,
+ queue_size: 3 - timeout_deleted)
+ end
+ end
+
+ context 'when the queue is processed in time' do
+ it 'returns a completion flag, the number of jobs deleted, and the remaining queue size' do
+ expect(sidekiq_queue.drop_jobs!(search_metadata, timeout: 10))
+ .to eq(completed: true,
+ deleted_jobs: no_timeout_deleted,
+ queue_size: 3 - no_timeout_deleted)
+ end
+ end
+ end
+
+ context 'when there are no matching jobs' do
+ include_examples 'queue processing' do
+ let(:search_metadata) { { project: 1 } }
+ let(:timeout_deleted) { 0 }
+ let(:no_timeout_deleted) { 0 }
+ end
+ end
+
+ context 'when there are matching jobs' do
+ include_examples 'queue processing' do
+ let(:search_metadata) { { user: sidekiq_queue_user.username } }
+ let(:timeout_deleted) { 1 }
+ let(:no_timeout_deleted) { 2 }
+ end
+ end
+
+ context 'when there are no valid metadata keys passed' do
+ it 'raises NoMetadataError' do
+ add_job(create(:user))
+
+ expect { described_class.new('authorized_projects').drop_jobs!({ username: 'sidekiq_queue_user' }, timeout: 1) }
+ .to raise_error(described_class::NoMetadataError)
+ end
+ end
+
+ context 'when the queue does not exist' do
+ it 'raises InvalidQueueError' do
+ expect { described_class.new('foo').drop_jobs!({ user: 'sidekiq_queue_user' }, timeout: 1) }
+ .to raise_error(described_class::InvalidQueueError)
+ end
+ end
+ end
+end