summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/sidekiq_middleware/correlation_injector_spec.rb
blob: a138ad7c910057a8803bebe86e048dde27bd7849 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::SidekiqMiddleware::CorrelationInjector do
  class TestWorker
    include ApplicationWorker
  end

  before do |example|
    Sidekiq.client_middleware do |chain|
      chain.add described_class
    end
  end

  after do |example|
    Sidekiq.client_middleware do |chain|
      chain.remove described_class
    end

    Sidekiq::Queues.clear_all
  end

  around do |example|
    Sidekiq::Testing.fake! do
      example.run
    end
  end

  it 'injects into payload the correlation id' do
    expect_any_instance_of(described_class).to receive(:call).and_call_original

    Gitlab::CorrelationId.use_id('new-correlation-id') do
      TestWorker.perform_async(1234)
    end

    expected_job_params = {
      "class" => "TestWorker",
      "args" => [1234],
      "correlation_id" => "new-correlation-id"
    }

    expect(Sidekiq::Queues.jobs_by_worker).to a_hash_including(
      "TestWorker" => a_collection_containing_exactly(
        a_hash_including(expected_job_params)))
  end
end