debian-mirror-gitlab/spec/workers/concerns/cronjob_queue_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.1 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe CronjobQueue do
2016-11-03 12:29:30 +05:30
let(:worker) do
Class.new do
2018-03-17 18:26:18 +05:30
def self.name
'DummyWorker'
end
include ApplicationWorker
2020-03-13 15:44:24 +05:30
include CronjobQueue # rubocop:disable Scalability/CronWorkerContext
2022-07-23 23:45:48 +05:30
def perform
AnotherWorker.perform_async('identifier')
end
end
end
let(:another_worker) do
Class.new do
def self.name
'AnotherWorker'
end
include ApplicationWorker
# To keep track of the context that was active for certain arguments
cattr_accessor(:contexts) { {} }
def perform(identifier, *args)
self.class.contexts.merge!(identifier => Gitlab::ApplicationContext.current)
end
2016-11-03 12:29:30 +05:30
end
end
2020-04-22 19:07:51 +05:30
before do
stub_const("DummyWorker", worker)
2022-07-23 23:45:48 +05:30
stub_const("AnotherWorker", another_worker)
2020-04-22 19:07:51 +05:30
end
2022-11-25 23:54:43 +05:30
it 'sets the queue name of a worker' do
expect(worker.sidekiq_options['queue'].to_s).to eq('cronjob:dummy')
end
2016-11-03 12:29:30 +05:30
it 'disables retrying of failed jobs' do
expect(worker.sidekiq_options['retry']).to eq(false)
end
2020-03-13 15:44:24 +05:30
it 'automatically clears project, user and namespace from the context', :aggregate_failues do
2022-07-23 23:45:48 +05:30
worker_context = worker.get_worker_context.to_lazy_hash.transform_values { |v| v.try(:call) }
2020-03-13 15:44:24 +05:30
expect(worker_context[:user]).to be_nil
expect(worker_context[:root_namespace]).to be_nil
expect(worker_context[:project]).to be_nil
end
2020-04-22 19:07:51 +05:30
it 'gets scheduled with caller_id set to Cronjob' do
worker.perform_async
job = worker.jobs.last
expect(job).to include('meta.caller_id' => 'Cronjob')
end
2022-07-23 23:45:48 +05:30
it 'gets root_caller_id from the cronjob' do
Sidekiq::Testing.inline! do
worker.perform_async
end
expect(AnotherWorker.contexts['identifier']).to include('meta.root_caller_id' => 'Cronjob')
end
2020-04-22 19:07:51 +05:30
it 'does not set the caller_id if there was already one in the context' do
Gitlab::ApplicationContext.with_context(caller_id: 'already set') do
worker.perform_async
end
job = worker.jobs.last
expect(job).to include('meta.caller_id' => 'already set')
end
2016-11-03 12:29:30 +05:30
end