Skip to content

Commit

Permalink
Add a test for retry_intervals as a lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Cantero committed Mar 10, 2017
1 parent efe1c27 commit 35588df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/shoryuken/middleware/server/exponential_backoff_retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def call(worker, queue, sqs_msg, body)
def get_interval(retry_intervals, attempts)
return retry_intervals.call(attempts) if retry_intervals.respond_to?(:call)

# Array start at 0
# Arrays start at 0
attempts -= 1

if attempts < (retry_intervals = Array(retry_intervals)).size
Expand All @@ -44,9 +44,9 @@ def next_visibility_timeout(interval, started_at)
end

def handle_failure(sqs_msg, started_at, retry_intervals)
return false unless sqs_msg.attributes['ApproximateReceiveCount']
return false if (receive_count = sqs_msg.attributes['ApproximateReceiveCount'].to_i).zero?

return false unless (interval = get_interval(retry_intervals, sqs_msg.attributes['ApproximateReceiveCount'].to_i))
return false unless (interval = get_interval(retry_intervals, receive_count))

sqs_msg.change_visibility(visibility_timeout: next_visibility_timeout(interval, started_at))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
end

context 'and retry_intervals is a lambda' do
it 'retries' do
TestWorker.get_shoryuken_options['retry_intervals'] = ->(_attempts) { 500 }

allow(sqs_msg).to receive(:queue) { sqs_queue }
expect(sqs_msg).to receive(:change_visibility).with(visibility_timeout: 500)

expect { subject.call(TestWorker.new, queue, sqs_msg, sqs_msg.body) { raise 'failed' } }.not_to raise_error
end
end

context 'and retry_intervals is empty' do
Expand Down

0 comments on commit 35588df

Please sign in to comment.