Files
gitlab-foss/spec/lib/gitlab/sidekiq_middleware/concurrency_limit/client_spec.rb
2024-10-21 06:12:31 +00:00

65 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::SidekiqMiddleware::ConcurrencyLimit::Client, :clean_gitlab_redis_queues, feature_category: :global_search do
let(:worker_class) do
Class.new do
def self.name
'TestConcurrencyLimitWorker'
end
include ApplicationWorker
concurrency_limit -> { 5 }
def perform(*)
self.class.work
end
def self.work; end
end
end
before do
stub_const('TestConcurrencyLimitWorker', worker_class)
end
describe '#call' do
context 'when feature flag is disabled' do
before do
stub_feature_flags(sidekiq_concurrency_limit_middleware: false)
end
it 'schedules the job' do
expect(Gitlab::SidekiqMiddleware::ConcurrencyLimit::ConcurrencyLimitService).not_to receive(:add_to_queue!)
TestConcurrencyLimitWorker.perform_async('foo')
expect(TestConcurrencyLimitWorker.jobs.size).to eq(1)
end
end
context 'when there are jobs in the queue' do
before do
allow(::Gitlab::SidekiqMiddleware::ConcurrencyLimit::ConcurrencyLimitService).to receive(:has_jobs_in_queue?)
.and_return(true)
end
it 'defers the job' do
expect(Gitlab::SidekiqMiddleware::ConcurrencyLimit::ConcurrencyLimitService).to receive(:add_to_queue!).once
TestConcurrencyLimitWorker.perform_async('foo')
expect(TestConcurrencyLimitWorker.jobs.size).to eq(0)
end
it 'does not defer scheduled jobs' do
expect(Gitlab::SidekiqMiddleware::ConcurrencyLimit::ConcurrencyLimitService).not_to receive(:add_to_queue!)
TestConcurrencyLimitWorker.perform_in(10, 'foo')
end
end
end
end