summaryrefslogtreecommitdiff
path: root/spec/initializers/forbid_sidekiq_in_transactions_spec.rb
blob: a89ac73f6fadce9350ba839ec2a5585f19951a6c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Sidekiq::Worker' do
  shared_examples_for 'a forbiddable operation within a transaction' do
    it 'allows the operation outside of a transaction' do
      expect { operation }.not_to raise_error
    end

    it 'forbids the operation within a transaction' do
      ApplicationRecord.transaction do
        expect { operation }.to raise_error(Sidekiq::Worker::EnqueueFromTransactionError)
      end
    end

    it 'allows the oepration within a transaction if skipped' do
      Sidekiq::Worker.skipping_transaction_check do
        ApplicationRecord.transaction do
          expect { operation }.not_to raise_error
        end
      end
    end

    it 'forbids the operation if it is within a Ci::ApplicationRecord transaction' do
      Ci::Pipeline.transaction do
        expect { operation }.to raise_error(Sidekiq::Worker::EnqueueFromTransactionError)
      end
    end
  end

  context 'for sidekiq workers' do
    let(:worker_class) do
      Class.new do
        include Sidekiq::Worker

        def perform
        end
      end
    end

    let(:operation) { worker_class.perform_async }

    it_behaves_like 'a forbiddable operation within a transaction'
  end

  context 'for mailers' do
    let(:mailer_class) do
      Class.new(ApplicationMailer) do
        def test_mail
        end
      end
    end

    let(:operation) { mailer_class.test_mail.deliver_later }

    it_behaves_like 'a forbiddable operation within a transaction'
  end
end