summaryrefslogtreecommitdiff
path: root/spec/workers/concerns/cronjob_queue_spec.rb
blob: 0244535051f00ed3fa5997cf8f07bcd68b0002c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CronjobQueue do
  let(:worker) do
    Class.new do
      def self.name
        'DummyWorker'
      end

      include ApplicationWorker
      include CronjobQueue # rubocop:disable Scalability/CronWorkerContext

      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
    end
  end

  before do
    stub_const("DummyWorker", worker)
    stub_const("AnotherWorker", another_worker)
  end

  it 'sets the queue name of a worker' do
    expect(worker.sidekiq_options['queue'].to_s).to eq('cronjob:dummy')
  end

  it 'disables retrying of failed jobs' do
    expect(worker.sidekiq_options['retry']).to eq(false)
  end

  it 'automatically clears project, user and namespace from the context', :aggregate_failues do
    worker_context = worker.get_worker_context.to_lazy_hash.transform_values { |v| v.try(:call) }

    expect(worker_context[:user]).to be_nil
    expect(worker_context[:root_namespace]).to be_nil
    expect(worker_context[:project]).to be_nil
  end

  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

  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

  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
end