demarches-normaliennes/spec/jobs/api_entreprise/job_spec.rb

64 lines
1.8 KiB
Ruby
Raw Permalink Normal View History

include ActiveJob::TestHelper
2020-08-05 18:40:47 +02:00
RSpec.describe APIEntreprise::Job, type: :job do
2020-12-03 11:17:27 +01:00
# https://api.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html
# #method-i-retry_on
describe '#perform' do
let(:dossier) { create(:dossier, :with_entreprise) }
2021-11-12 08:27:02 +01:00
context 'when an un-retriable error is raised' do
2020-12-03 11:17:27 +01:00
let(:errors) { [:standard_error] }
it 'does not retry' do
ensure_errors_force_n_retry(errors, 1)
end
end
2021-11-12 08:27:02 +01:00
context 'when a retriable error is raised' do
2020-12-03 11:17:27 +01:00
let(:errors) { [:service_unavaible, :bad_gateway, :timed_out] }
2020-12-03 11:17:27 +01:00
it 'retries 5 times' do
ensure_errors_force_n_retry(errors, 5)
2020-08-05 18:40:47 +02:00
expect(dossier.reload.api_entreprise_job_exceptions.first).to match('APIEntreprise::API::Error::ServiceUnavailable')
2020-12-03 11:17:27 +01:00
end
end
2020-12-03 11:17:27 +01:00
def ensure_errors_force_n_retry(errors, retry_nb)
etablissement = dossier.etablissement
2020-12-03 11:17:27 +01:00
errors.each do |error|
assert_performed_jobs(retry_nb) do
ErrorJob.perform_later(error, etablissement) rescue StandardError
2020-12-03 11:17:27 +01:00
end
end
end
end
2020-08-05 18:40:47 +02:00
class ErrorJob < APIEntreprise::Job
def perform(error, etablissement)
@etablissement = etablissement
2020-12-03 16:52:58 +01:00
response = OpenStruct.new(
effective_url: 'http://host.com/path',
code: '666',
body: 'body',
return_message: 'return_message',
total_time: 10,
connect_time: 20,
headers: 'headers'
)
2020-12-03 11:17:27 +01:00
case error
when :service_unavaible
2020-08-05 18:40:47 +02:00
raise APIEntreprise::API::Error::ServiceUnavailable.new(response)
when :bad_gateway
2020-08-05 18:40:47 +02:00
raise APIEntreprise::API::Error::BadGateway.new(response)
when :timed_out
2020-08-05 18:40:47 +02:00
raise APIEntreprise::API::Error::TimedOut.new(response)
else
raise StandardError
end
end
end
end