Skip to content

Commit

Permalink
Add tests for custom @retry_exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lantins committed Apr 25, 2010
1 parent af2ec5b commit 166857d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/retry_test.rb
Expand Up @@ -63,4 +63,39 @@ def test_fail_five_times_then_succeed
assert_equal 6, Resque.info[:processed], 'processed job'
assert_equal 0, Resque.info[:pending], 'pending jobs'
end

def test_can_determine_if_exception_may_be_retried
assert_equal true, RetryDefaultsJob.retry_exception?(StandardError), 'StandardError may retry'
assert_equal true, RetryDefaultsJob.retry_exception?(CustomException), 'CustomException may retry'
assert_equal true, RetryDefaultsJob.retry_exception?(HierarchyCustomException), 'HierarchyCustomException may retry'

assert_equal true, RetryCustomExceptionsJob.retry_exception?(CustomException), 'CustomException may retry'
assert_equal true, RetryCustomExceptionsJob.retry_exception?(HierarchyCustomException), 'HierarchyCustomException may retry'
assert_equal false, RetryCustomExceptionsJob.retry_exception?(AnotherCustomException), 'AnotherCustomException may not retry'
end

def test_retry_if_failed_and_exception_may_retry
Resque.enqueue(RetryCustomExceptionsJob, CustomException)
Resque.enqueue(RetryCustomExceptionsJob, HierarchyCustomException)
4.times do
perform_next_job(@worker)
end

assert_equal 4, Resque.info[:failed], 'failed jobs'
assert_equal 4, Resque.info[:processed], 'processed job'
assert_equal 2, Resque.info[:pending], 'pending jobs'
end

def test_do_not_retry_if_failed_and_exception_does_not_allow_retry
Resque.enqueue(RetryCustomExceptionsJob, AnotherCustomException)
Resque.enqueue(RetryCustomExceptionsJob, RuntimeError)
4.times do
perform_next_job(@worker)
end

assert_equal 2, Resque.info[:failed], 'failed jobs'
assert_equal 2, Resque.info[:processed], 'processed job'
assert_equal 0, Resque.info[:pending], 'pending jobs'
end

end
20 changes: 20 additions & 0 deletions test/test_jobs.rb
@@ -1,3 +1,7 @@
CustomException = Class.new(StandardError)
HierarchyCustomException = Class.new(CustomException)
AnotherCustomException = Class.new(StandardError)

class GoodJob
@queue = :testing
def self.perform(*args)
Expand Down Expand Up @@ -38,4 +42,20 @@ def self.perform(*args)
class ExponentialBackoffJob < RetryDefaultsJob
extend Resque::Plugins::ExponentialBackoff
@queue = :testing
end

class RetryCustomExceptionsJob < RetryDefaultsJob
@queue = :testing

@retry_limit = 5
@retry_exceptions = [CustomException, HierarchyCustomException]

def self.perform(exception)
case exception
when 'CustomException' then raise CustomException
when 'HierarchyCustomException' then raise HierarchyCustomException
when 'AnotherCustomException' then raise AnotherCustomException
else raise StandardError
end
end
end

0 comments on commit 166857d

Please sign in to comment.