From 7efd77fae58a02c6014211a8f2f9bcce3f4844cc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 2 Aug 2016 14:26:56 -0700 Subject: Fix tests against ActiveSupport::Durations --- activejob/lib/active_job/exceptions.rb | 13 ++++-- activejob/test/cases/exceptions_test.rb | 73 ++++++++++++++++++++------------- activejob/test/jobs/retry_job.rb | 2 + 3 files changed, 56 insertions(+), 32 deletions(-) (limited to 'activejob') diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb index 5488a86756..acbe0c4057 100644 --- a/activejob/lib/active_job/exceptions.rb +++ b/activejob/lib/active_job/exceptions.rb @@ -101,16 +101,21 @@ module ActiveJob end private - def determine_delay(seconds_or_algorithm) - case seconds_or_algorithm + def determine_delay(seconds_or_duration_or_algorithm) + case seconds_or_duration_or_algorithm when :exponentially_longer (executions ** 4) + 2 + when ActiveSupport::Duration + duration = seconds_or_duration_or_algorithm + duration.to_i when Integer - seconds = seconds_or_algorithm + seconds = seconds_or_duration_or_algorithm seconds when Proc - algorithm = seconds_or_algorithm + algorithm = seconds_or_duration_or_algorithm algorithm.call(executions) + else + raise "Couldn't determine a delay based on #{seconds_or_duration_or_algorithm.inspect}" end end end diff --git a/activejob/test/cases/exceptions_test.rb b/activejob/test/cases/exceptions_test.rb index 9ad73ff93c..58d87a339a 100644 --- a/activejob/test/cases/exceptions_test.rb +++ b/activejob/test/cases/exceptions_test.rb @@ -1,59 +1,76 @@ require 'helper' require 'jobs/retry_job' -class ExceptionsTest < ActiveSupport::TestCase +class ExceptionsTest < ActiveJob::TestCase setup do JobBuffer.clear + skip if ActiveJob::Base.queue_adapter.is_a?(ActiveJob::QueueAdapters::InlineAdapter) end test "successfully retry job throwing exception against defaults" do - RetryJob.perform_later 'DefaultsError', 5 + perform_enqueued_jobs do + RetryJob.perform_later 'DefaultsError', 5 - assert_equal [ - "Raised DefaultsError for the 1st time", - "Raised DefaultsError for the 2nd time", - "Raised DefaultsError for the 3rd time", - "Raised DefaultsError for the 4th time", - "Successfully completed job" ], JobBuffer.values + assert_equal [ + "Raised DefaultsError for the 1st time", + "Raised DefaultsError for the 2nd time", + "Raised DefaultsError for the 3rd time", + "Raised DefaultsError for the 4th time", + "Successfully completed job" ], JobBuffer.values + end end test "successfully retry job throwing exception against higher limit" do - RetryJob.perform_later 'ShortWaitTenAttemptsError', 9 - assert_equal 9, JobBuffer.values.count + perform_enqueued_jobs do + RetryJob.perform_later 'ShortWaitTenAttemptsError', 9 + assert_equal 9, JobBuffer.values.count + end end test "failed retry job when exception kept occurring against defaults" do - begin - RetryJob.perform_later 'DefaultsError', 6 - assert_equal "Raised DefaultsError for the 5th time", JobBuffer.last_value - rescue DefaultsError - pass + perform_enqueued_jobs do + begin + RetryJob.perform_later 'DefaultsError', 6 + assert_equal "Raised DefaultsError for the 5th time", JobBuffer.last_value + rescue DefaultsError + pass + end end end test "failed retry job when exception kept occurring against higher limit" do - begin - RetryJob.perform_later 'ShortWaitTenAttemptsError', 11 - assert_equal "Raised ShortWaitTenAttemptsError for the 10th time", JobBuffer.last_value - rescue ShortWaitTenAttemptsError - pass + perform_enqueued_jobs do + begin + RetryJob.perform_later 'ShortWaitTenAttemptsError', 11 + assert_equal "Raised ShortWaitTenAttemptsError for the 10th time", JobBuffer.last_value + rescue ShortWaitTenAttemptsError + pass + end end end test "discard job" do - RetryJob.perform_later 'DiscardableError', 2 - assert_equal "Raised DiscardableError for the 1st time", JobBuffer.last_value + perform_enqueued_jobs do + RetryJob.perform_later 'DiscardableError', 2 + assert_equal "Raised DiscardableError for the 1st time", JobBuffer.last_value + end end test "custom handling of job that exceeds retry attempts" do - RetryJob.perform_later 'CustomCatchError', 6 - assert_equal "Dealt with a job that failed to retry in a custom way", JobBuffer.last_value + perform_enqueued_jobs do + RetryJob.perform_later 'CustomCatchError', 6 + assert_equal "Dealt with a job that failed to retry in a custom way", JobBuffer.last_value + end end -end -class ExponentiallyBackoffExceptionsTest < ActiveJob::TestCase - setup do - JobBuffer.clear + test "long wait job" do + travel_to Time.now + + perform_enqueued_jobs do + assert_performed_with at: (Time.now + 3600.seconds).to_i do + RetryJob.perform_later 'LongWaitError', 5 + end + end end test "exponentially retrying job" do diff --git a/activejob/test/jobs/retry_job.rb b/activejob/test/jobs/retry_job.rb index 321294f892..350edee61c 100644 --- a/activejob/test/jobs/retry_job.rb +++ b/activejob/test/jobs/retry_job.rb @@ -2,6 +2,7 @@ require_relative '../support/job_buffer' require 'active_support/core_ext/integer/inflections' class DefaultsError < StandardError; end +class LongWaitError < StandardError; end class ShortWaitTenAttemptsError < StandardError; end class ExponentialWaitTenAttemptsError < StandardError; end class CustomWaitTenAttemptsError < StandardError; end @@ -10,6 +11,7 @@ class DiscardableError < StandardError; end class RetryJob < ActiveJob::Base retry_on DefaultsError + retry_on LongWaitError, wait: 1.hour, attempts: 10 retry_on ShortWaitTenAttemptsError, wait: 1.second, attempts: 10 retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10 retry_on CustomWaitTenAttemptsError, wait: ->(executions) { executions * 2 }, attempts: 10 -- cgit v1.2.3