2020-12-02 17:17:00 +01:00
|
|
|
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
|
2021-02-04 19:24:52 +01:00
|
|
|
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)
|
2020-12-02 17:17:00 +01:00
|
|
|
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-02 17:17:00 +01:00
|
|
|
|
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
|
2020-12-02 17:17:00 +01:00
|
|
|
end
|
2020-12-03 10:39:51 +01:00
|
|
|
|
2020-12-03 11:17:27 +01:00
|
|
|
def ensure_errors_force_n_retry(errors, retry_nb)
|
2021-02-04 19:24:52 +01:00
|
|
|
etablissement = dossier.etablissement
|
|
|
|
|
2020-12-03 11:17:27 +01:00
|
|
|
errors.each do |error|
|
|
|
|
assert_performed_jobs(retry_nb) do
|
2021-02-04 19:24:52 +01:00
|
|
|
ErrorJob.perform_later(error, etablissement) rescue StandardError
|
2020-12-03 11:17:27 +01:00
|
|
|
end
|
|
|
|
end
|
2020-12-03 10:39:51 +01:00
|
|
|
end
|
2020-12-02 17:17:00 +01:00
|
|
|
end
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
class ErrorJob < APIEntreprise::Job
|
2021-02-04 19:24:52 +01:00
|
|
|
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
|
2020-12-02 17:17:00 +01:00
|
|
|
when :service_unavaible
|
2020-08-05 18:40:47 +02:00
|
|
|
raise APIEntreprise::API::Error::ServiceUnavailable.new(response)
|
2020-12-02 17:17:00 +01:00
|
|
|
when :bad_gateway
|
2020-08-05 18:40:47 +02:00
|
|
|
raise APIEntreprise::API::Error::BadGateway.new(response)
|
2020-12-03 10:39:51 +01:00
|
|
|
when :timed_out
|
2020-08-05 18:40:47 +02:00
|
|
|
raise APIEntreprise::API::Error::TimedOut.new(response)
|
2020-12-02 17:17:00 +01:00
|
|
|
else
|
|
|
|
raise StandardError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|