Skip to content

Commit

Permalink
refactor to test both DeliveryJob and MailDeliveryJob
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed Jan 2, 2024
1 parent ea734f7 commit cb80d3e
Showing 1 changed file with 82 additions and 50 deletions.
132 changes: 82 additions & 50 deletions spec/rollbar/plugins/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,86 +22,89 @@ def perform(exception, job_id)
end
end

class TestMailer < ActionMailer::Base
attr_accessor :arguments

def test_email(*_arguments)
error = StandardError.new('oh no')
raise(error)
end
end

before { reconfigure_notifier }

let(:exception) { StandardError.new('oh no') }
let(:job_id) { '123' }
let(:argument) { 12 }

it 'reports the error to Rollbar' do
expected_params = {
let(:expected_params) do
{
:job => 'TestJob',
:job_id => job_id,
:use_exception_level_filters => true,
:arguments => [argument]
}
expect(Rollbar).to receive(:error).with(exception, expected_params)
begin
TestJob.new(argument).perform(exception, job_id)
rescue StandardError
nil
end
end

it 'reraises the error so the job backend can handle the failure and retry' do
expect do
TestJob.new(argument).perform(exception, job_id)
end.to raise_error exception
end
shared_examples 'an ActiveMailer plugin' do
it 'reraises the error so the job backend can handle the failure and retry' do
expect do
TestJob.new(argument).perform(exception, job_id)
end.to raise_error exception
end

it 'scrubs all arguments if job has `log_arguments` disabled' do
allow(TestJob).to receive(:log_arguments?).and_return(false)
it 'scrubs all arguments if job has `log_arguments` disabled' do
allow(TestJob).to receive(:log_arguments?).and_return(false)

expected_params = {
:job => 'TestJob',
:job_id => job_id,
:use_exception_level_filters => true,
:arguments => ['******', '******', '******']
}
expect(Rollbar).to receive(:error).with(exception, expected_params)
begin
TestJob.new(1, 2, 3).perform(exception, job_id)
rescue StandardError
nil
expected_params[:job_id] = job_id
expected_params[:arguments] = ['******', '******', '******']
expect(Rollbar).to receive(:error).with(exception, expected_params)
begin
TestJob.new(1, 2, 3).perform(exception, job_id)
rescue StandardError
nil
end
end
end

context 'disabled in config' do
subject { Rollbar.plugins.get('active_job') }
before do
subject.unload!

# Configur will load all plugins after applying the config.
Rollbar.configure { |c| c.disable_action_mailer_monkey_patch = true }
it 'job is created' do
ActiveJob::Base.queue_adapter = :test
expect do
TestMailer.test_email(argument).deliver_later
end.to have_enqueued_job.on_queue('mailers')
end

it "isn't loaded" do
expect(subject.loaded).to eq(false)
context 'disabled in config' do
subject { Rollbar.plugins.get('active_job') }
before do
subject.unload!

# Configur will load all plugins after applying the config.
Rollbar.configure { |c| c.disable_action_mailer_monkey_patch = true }
end

it "isn't loaded" do
expect(subject.loaded).to eq(false)
end
end
end

context 'using ActionMailer::DeliveryJob', :if => Gem::Version.new(Rails.version) < Gem::Version.new('6.0') do
include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper)

class TestMailer < ActionMailer::Base
attr_accessor :arguments
it_behaves_like 'an ActiveMailer plugin'

def test_email(*_arguments)
error = StandardError.new('oh no')
raise(error)
it 'reports the job error to Rollbar' do
expect(Rollbar).to receive(:error).with(exception, expected_params)
begin
TestJob.new(argument).perform(exception, job_id)
rescue StandardError
nil
end
end

it 'job is created' do
ActiveJob::Base.queue_adapter = :test
expect do
TestMailer.test_email(argument).deliver_later
end.to have_enqueued_job.on_queue('mailers')
end

it 'reports the error to Rollbar' do
it 'reports the mailer error to Rollbar' do
expected_params = {
:job => 'ActionMailer::DeliveryJob',
:job_id => job_id,
:use_exception_level_filters => true,
:arguments => ['TestMailer', 'test_email', 'deliver_now', 12]
}
Expand Down Expand Up @@ -151,4 +154,33 @@ def test_email(*_arguments)
.should match(/^*+$/)
end
end

context 'using ActionMailer::MailDeliveryJob', :if => Gem::Version.new(Rails.version) >= Gem::Version.new('6.0') do
include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper)

let(:expected_params) do
{
:job => 'TestJob',
:use_exception_level_filters => true
}
end

it_behaves_like 'an ActiveMailer plugin'

it 'reports the error to Rollbar' do
expected_params.delete(:job)

# In 6+, the re-raise in the plugin will cause the rescue_from to be called twice.
expect(Rollbar).to receive(:error).twice.with(kind_of(StandardError),
hash_including(expected_params))

perform_enqueued_jobs do
begin
TestMailer.test_email(argument).deliver_later
rescue StandardError => e
nil
end
end
end
end
end

0 comments on commit cb80d3e

Please sign in to comment.