2021-11-29 22:13:04 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Sidekiq::Worker' do
|
2022-05-31 05:08:17 -04:00
|
|
|
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
|
2021-11-29 22:13:04 -05:00
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
it 'forbids the operation within a transaction' do
|
|
|
|
ApplicationRecord.transaction do
|
|
|
|
expect { operation }.to raise_error(Sidekiq::Worker::EnqueueFromTransactionError)
|
2021-11-29 22:13:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
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
|
2021-11-29 22:13:04 -05:00
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
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
|
2021-11-29 22:13:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
context 'for sidekiq workers' do
|
|
|
|
let(:worker_class) do
|
|
|
|
Class.new do
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
def perform
|
|
|
|
end
|
2021-11-29 22:13:04 -05:00
|
|
|
end
|
|
|
|
end
|
2022-05-31 05:08:17 -04:00
|
|
|
|
|
|
|
let(:operation) { worker_class.perform_async }
|
|
|
|
|
|
|
|
it_behaves_like 'a forbiddable operation within a transaction'
|
2021-11-29 22:13:04 -05:00
|
|
|
end
|
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
context 'for mailers' do
|
|
|
|
let(:mailer_class) do
|
|
|
|
Class.new(ApplicationMailer) do
|
|
|
|
def test_mail
|
|
|
|
end
|
|
|
|
end
|
2021-11-29 22:13:04 -05:00
|
|
|
end
|
2022-05-31 05:08:17 -04:00
|
|
|
|
|
|
|
let(:operation) { mailer_class.test_mail.deliver_later }
|
|
|
|
|
|
|
|
it_behaves_like 'a forbiddable operation within a transaction'
|
2021-11-29 22:13:04 -05:00
|
|
|
end
|
|
|
|
end
|