spec/retry_spec.rb in activejob-retry-0.6.0 vs spec/retry_spec.rb in activejob-retry-0.6.1

- old
+ new

@@ -10,11 +10,11 @@ raise RuntimeError end end.include(retry_instance) end - describe '.constant_retry' do + describe 'constant strategy' do let(:strategy) { :constant } let(:options) { { limit: 10, delay: 5 } } it 'sets a ConstantBackoffStrategy' do expect(job.backoff_strategy).to be_a(ActiveJob::Retry::ConstantBackoffStrategy) @@ -26,13 +26,21 @@ specify do expect { retry_instance }. to raise_error(ActiveJob::Retry::InvalidConfigurationError) end end + + context 'subclassing' do + let(:subclass) { Class.new(job) } + it 'has the ConstantBackoffStrategy' do + expect(subclass.backoff_strategy). + to be_a(ActiveJob::Retry::ConstantBackoffStrategy) + end + end end - describe '.variable_retry' do + describe 'variable strategy' do let(:strategy) { :variable } let(:options) { { delays: [0, 5, 10, 60, 200] } } it 'sets a VariableBackoffStrategy' do expect(job.backoff_strategy).to be_a(ActiveJob::Retry::VariableBackoffStrategy) @@ -44,13 +52,27 @@ specify do expect { retry_instance }. to raise_error(ActiveJob::Retry::InvalidConfigurationError) end end + + context 'subclassing' do + let(:subclass) { Class.new(job) } + it 'has the VariableBackoffStrategy' do + expect(subclass.backoff_strategy). + to be_a(ActiveJob::Retry::VariableBackoffStrategy) + end + + it 'allows overriding' do + subclass.include(described_class.new(strategy: :constant)) + expect(subclass.backoff_strategy). + to be_a(ActiveJob::Retry::ConstantBackoffStrategy) + end + end end - describe '.exponential_retry' do + describe 'exponential strategy' do let(:strategy) { :exponential } let(:options) { { limit: 10 } } it 'sets an ExponentialBackoffStrategy' do expect(job.backoff_strategy).to be_a(ActiveJob::Retry::ExponentialBackoffStrategy) @@ -71,10 +93,18 @@ specify do expect { retry_instance }. to raise_error(ActiveJob::Retry::InvalidConfigurationError) end end + + context 'subclassing' do + let(:subclass) { Class.new(job) } + it 'has the ExponentialBackoffStrategy' do + expect(subclass.backoff_strategy). + to be_a(ActiveJob::Retry::ExponentialBackoffStrategy) + end + end end describe 'custom strategy' do module CustomBackoffStrategy def self.should_retry?(_attempt, _exception) @@ -94,10 +124,17 @@ let(:strategy) { CustomBackoffStrategy } it 'sets the backoff_strategy when it is valid' do expect(job.backoff_strategy).to eq(CustomBackoffStrategy) end + + context 'subclassing' do + let(:subclass) { Class.new(job) } + it 'has the CustomBackoffStrategy' do + expect(subclass.backoff_strategy).to eq(strategy) + end + end end describe '#serialize' do let(:instance) { job.new } subject { instance.serialize } @@ -113,10 +150,30 @@ context '7th attempt' do before { instance.instance_variable_set(:@retry_attempt, 7) } it { is_expected.to include('retry_attempt' => 7) } end + + context 'when inherited' do + let(:subclass) { Class.new(job) } + let(:instance) { subclass.new } + subject { instance.serialize } + + context 'first instantiated' do + it { is_expected.to include('retry_attempt' => 1) } + end + + context '1st attempt' do + before { instance.instance_variable_set(:@retry_attempt, 1) } + it { is_expected.to include('retry_attempt' => 1) } + end + + context '7th attempt' do + before { instance.instance_variable_set(:@retry_attempt, 7) } + it { is_expected.to include('retry_attempt' => 7) } + end + end end describe '#deserialize' do subject(:instance) { job.new } before { instance.deserialize(job_data) } @@ -148,9 +205,46 @@ end its(:job_id) { is_expected.to eq('uuid') } its(:arguments) { is_expected.to eq(['arg1', { 'arg' => 2 }]) } its(:retry_attempt) { is_expected.to eq(7) } + end + + context 'when subclassing' do + let(:subclass) { Class.new(job) } + subject(:instance) { subclass.new } + before { instance.deserialize(job_data) } + before { instance.send(:deserialize_arguments_if_needed) } + + context '1st attempt' do + let(:job_data) do + { + 'job_class' => 'SomeJob', + 'job_id' => 'uuid', + 'arguments' => ['arg1', { 'arg' => 2 }], + 'retry_attempt' => 1 + } + end + + its(:job_id) { is_expected.to eq('uuid') } + its(:arguments) { is_expected.to eq(['arg1', { 'arg' => 2 }]) } + its(:retry_attempt) { is_expected.to eq(1) } + end + + context '7th attempt' do + let(:job_data) do + { + 'job_class' => 'SomeJob', + 'job_id' => 'uuid', + 'arguments' => ['arg1', { 'arg' => 2 }], + 'retry_attempt' => 7 + } + end + + its(:job_id) { is_expected.to eq('uuid') } + its(:arguments) { is_expected.to eq(['arg1', { 'arg' => 2 }]) } + its(:retry_attempt) { is_expected.to eq(7) } + end end end describe '#rescue_with_handler' do let(:mod) { described_class.new(strategy: :constant, limit: 100) }