refator test
This commit is contained in:
parent
f56235c7ee
commit
05e9130634
1 changed files with 22 additions and 31 deletions
|
@ -1,46 +1,37 @@
|
|||
include ActiveJob::TestHelper
|
||||
|
||||
RSpec.describe ApiEntreprise::Job, type: :job do
|
||||
# https://api.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
|
||||
context 'when an exception is raised' do
|
||||
subject do
|
||||
assert_performed_jobs(try) do
|
||||
ExceptionJob.perform_later(error) rescue StandardError
|
||||
# https://api.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html
|
||||
# #method-i-retry_on
|
||||
describe '#perform' do
|
||||
context 'when a un retryable error is raised' do
|
||||
let(:errors) { [:standard_error] }
|
||||
|
||||
it 'does not retry' do
|
||||
ensure_errors_force_n_retry(errors, 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is a service_unavaible' do
|
||||
let(:error) { :standard_error }
|
||||
let(:try) { 1 }
|
||||
context 'when a retryable error is raised' do
|
||||
let(:errors) { [:service_unavaible, :bad_gateway, :timed_out] }
|
||||
|
||||
it { subject }
|
||||
end
|
||||
|
||||
context 'when it is a service_unavaible' do
|
||||
let(:error) { :service_unavaible }
|
||||
let(:try) { 5 }
|
||||
|
||||
it { subject }
|
||||
end
|
||||
|
||||
context 'when it is a bad gateway' do
|
||||
let(:error) { :bad_gateway }
|
||||
let(:try) { 5 }
|
||||
|
||||
it { subject }
|
||||
end
|
||||
|
||||
context 'when it is timed out' do
|
||||
let(:error) { :timed_out }
|
||||
let(:try) { 5 }
|
||||
|
||||
it { subject }
|
||||
it 'retries 5 times' do
|
||||
ensure_errors_force_n_retry(errors, 5)
|
||||
end
|
||||
end
|
||||
|
||||
class ExceptionJob < ApiEntreprise::Job
|
||||
def perform(exception)
|
||||
case exception
|
||||
def ensure_errors_force_n_retry(errors, retry_nb)
|
||||
errors.each do |error|
|
||||
assert_performed_jobs(retry_nb) do
|
||||
ErrorJob.perform_later(error) rescue StandardError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ErrorJob < ApiEntreprise::Job
|
||||
def perform(error)
|
||||
case error
|
||||
when :service_unavaible
|
||||
raise ApiEntreprise::API::ServiceUnavailable
|
||||
when :bad_gateway
|
||||
|
|
Loading…
Add table
Reference in a new issue