2020-04-08 14:13:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::SidekiqQueue, :clean_gitlab_redis_queues do
|
2020-04-08 14:13:33 +05:30
|
|
|
around do |example|
|
2021-11-11 11:23:49 +05:30
|
|
|
Sidekiq::Queue.new('default').clear
|
2020-04-08 14:13:33 +05:30
|
|
|
Sidekiq::Testing.disable!(&example)
|
2021-11-11 11:23:49 +05:30
|
|
|
Sidekiq::Queue.new('default').clear
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def add_job(args, user:, klass: 'AuthorizedProjectsWorker')
|
2020-04-08 14:13:33 +05:30
|
|
|
Sidekiq::Client.push(
|
2021-11-11 11:23:49 +05:30
|
|
|
'class' => klass,
|
|
|
|
'queue' => 'default',
|
2020-04-08 14:13:33 +05:30
|
|
|
'args' => args,
|
|
|
|
'meta.user' => user.username
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#drop_jobs!' do
|
|
|
|
shared_examples 'queue processing' do
|
2021-11-11 11:23:49 +05:30
|
|
|
let(:sidekiq_queue) { described_class.new('default') }
|
2020-04-08 14:13:33 +05:30
|
|
|
let_it_be(:sidekiq_queue_user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
2021-11-11 11:23:49 +05:30
|
|
|
add_job([1], user: create(:user))
|
|
|
|
add_job([2], user: sidekiq_queue_user, klass: 'MergeWorker')
|
|
|
|
add_job([3], user: sidekiq_queue_user)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the queue is not processed in time' do
|
|
|
|
before do
|
2021-09-30 23:02:18 +05:30
|
|
|
allow(sidekiq_queue).to receive(:monotonic_time).and_return(1, 2, 12)
|
2020-04-08 14:13:33 +05:30
|
|
|
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
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when there are jobs matching the class name' do
|
|
|
|
include_examples 'queue processing' do
|
|
|
|
let(:search_metadata) { { user: sidekiq_queue_user.username, worker_class: 'AuthorizedProjectsWorker' } }
|
|
|
|
let(:timeout_deleted) { 1 }
|
|
|
|
let(:no_timeout_deleted) { 1 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'when there are no valid metadata keys passed' do
|
|
|
|
it 'raises NoMetadataError' do
|
2021-11-11 11:23:49 +05:30
|
|
|
add_job([1], user: create(:user))
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect { described_class.new('default').drop_jobs!({ username: 'sidekiq_queue_user' }, timeout: 1) }
|
2020-04-08 14:13:33 +05:30
|
|
|
.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
|