2022-01-26 12:08:38 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Sidekiq::Worker' do
|
2022-07-23 23:45:48 +05:30
|
|
|
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
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'forbids the operation within a transaction' do
|
|
|
|
ApplicationRecord.transaction do
|
|
|
|
expect { operation }.to raise_error(Sidekiq::Worker::EnqueueFromTransactionError)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
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
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
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
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
context 'for sidekiq workers' do
|
|
|
|
let(:worker_class) do
|
|
|
|
Class.new do
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
def perform
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
let(:operation) { worker_class.perform_async }
|
|
|
|
|
|
|
|
it_behaves_like 'a forbiddable operation within a transaction'
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
context 'for mailers' do
|
|
|
|
let(:mailer_class) do
|
|
|
|
Class.new(ApplicationMailer) do
|
|
|
|
def test_mail
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
let(:operation) { mailer_class.test_mail.deliver_later }
|
|
|
|
|
|
|
|
it_behaves_like 'a forbiddable operation within a transaction'
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|