diff options
Diffstat (limited to 'activejob/test')
-rw-r--r-- | activejob/test/cases/callbacks_test.rb | 28 | ||||
-rw-r--r-- | activejob/test/cases/exceptions_test.rb | 196 | ||||
-rw-r--r-- | activejob/test/cases/logging_test.rb | 8 | ||||
-rw-r--r-- | activejob/test/cases/queuing_test.rb | 20 | ||||
-rw-r--r-- | activejob/test/integration/queuing_test.rb | 28 | ||||
-rw-r--r-- | activejob/test/jobs/retry_job.rb | 22 | ||||
-rw-r--r-- | activejob/test/support/integration/test_case_helpers.rb | 10 |
7 files changed, 150 insertions, 162 deletions
diff --git a/activejob/test/cases/callbacks_test.rb b/activejob/test/cases/callbacks_test.rb index 267d58a7c8..895edb34a5 100644 --- a/activejob/test/cases/callbacks_test.rb +++ b/activejob/test/cases/callbacks_test.rb @@ -25,26 +25,22 @@ class CallbacksTest < ActiveSupport::TestCase end test "#enqueue returns false when before_enqueue aborts callback chain and return_false_on_aborted_enqueue = true" do - begin - prev = ActiveJob::Base.return_false_on_aborted_enqueue - ActiveJob::Base.return_false_on_aborted_enqueue = true - assert_equal false, AbortBeforeEnqueueJob.new.enqueue - ensure - ActiveJob::Base.return_false_on_aborted_enqueue = prev - end + prev = ActiveJob::Base.return_false_on_aborted_enqueue + ActiveJob::Base.return_false_on_aborted_enqueue = true + assert_equal false, AbortBeforeEnqueueJob.new.enqueue + ensure + ActiveJob::Base.return_false_on_aborted_enqueue = prev end test "#enqueue returns self when before_enqueue aborts callback chain and return_false_on_aborted_enqueue = false" do - begin - prev = ActiveJob::Base.return_false_on_aborted_enqueue - ActiveJob::Base.return_false_on_aborted_enqueue = false - job = AbortBeforeEnqueueJob.new - assert_deprecated do - assert_equal job, job.enqueue - end - ensure - ActiveJob::Base.return_false_on_aborted_enqueue = prev + prev = ActiveJob::Base.return_false_on_aborted_enqueue + ActiveJob::Base.return_false_on_aborted_enqueue = false + job = AbortBeforeEnqueueJob.new + assert_deprecated do + assert_equal job, job.enqueue end + ensure + ActiveJob::Base.return_false_on_aborted_enqueue = prev end test "#enqueue returns self when the job was enqueued" do diff --git a/activejob/test/cases/exceptions_test.rb b/activejob/test/cases/exceptions_test.rb index b5328b8d6a..c88162bf58 100644 --- a/activejob/test/cases/exceptions_test.rb +++ b/activejob/test/cases/exceptions_test.rb @@ -4,161 +4,167 @@ require "helper" require "jobs/retry_job" require "models/person" -class ExceptionsTest < ActiveJob::TestCase +class ExceptionsTest < ActiveSupport::TestCase setup do JobBuffer.clear - skip if ActiveJob::Base.queue_adapter.is_a?(ActiveJob::QueueAdapters::InlineAdapter) + skip if adapter_skips_scheduling?(ActiveJob::Base.queue_adapter) end test "successfully retry job throwing exception against defaults" do - perform_enqueued_jobs do - RetryJob.perform_later "DefaultsError", 5 + 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 - end + 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 test "successfully retry job throwing exception against higher limit" do - perform_enqueued_jobs do - RetryJob.perform_later "ShortWaitTenAttemptsError", 9 - assert_equal 9, JobBuffer.values.count - end + RetryJob.perform_later "ShortWaitTenAttemptsError", 9 + assert_equal 9, JobBuffer.values.count end - test "keeps the same attempts counter when several exceptions are listed in the same declaration" do + test "keeps the same attempts counter for several exceptions listed in the same retry_on declaration" do exceptions_to_raise = %w(FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo SecondRetryableErrorOfTwo SecondRetryableErrorOfTwo) assert_raises SecondRetryableErrorOfTwo do - perform_enqueued_jobs do - ExceptionRetryJob.perform_later(exceptions_to_raise) - end + RetryJob.perform_later(exceptions_to_raise, 5) + + assert_equal [ + "Raised FirstRetryableErrorOfTwo for the 1st time", + "Raised FirstRetryableErrorOfTwo for the 2nd time", + "Raised FirstRetryableErrorOfTwo for the 3rd time", + "Raised SecondRetryableErrorOfTwo for the 4th time", + "Raised SecondRetryableErrorOfTwo for the 5th time", + ], JobBuffer.values end end - test "keeps a separate attempts counter for each individual declaration" do - exceptions_to_raise = %w(FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo - DefaultsError DefaultsError) + test "keeps a separate attempts counter for each individual retry_on declaration" do + exceptions_to_raise = %w(DefaultsError DefaultsError DefaultsError DefaultsError + FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo) assert_nothing_raised do - perform_enqueued_jobs do - ExceptionRetryJob.perform_later(exceptions_to_raise) - end + RetryJob.perform_later(exceptions_to_raise, 10) + + 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", + "Raised FirstRetryableErrorOfTwo for the 5th time", + "Raised FirstRetryableErrorOfTwo for the 6th time", + "Raised FirstRetryableErrorOfTwo for the 7th time", + "Successfully completed job" + ], JobBuffer.values end end test "failed retry job when exception kept occurring against defaults" do - 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 + RetryJob.perform_later "DefaultsError", 6 + assert_equal "Raised DefaultsError for the 5th time", JobBuffer.last_value + rescue DefaultsError + pass end test "failed retry job when exception kept occurring against higher limit" do - 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 + RetryJob.perform_later "ShortWaitTenAttemptsError", 11 + assert_equal "Raised ShortWaitTenAttemptsError for the 10th time", JobBuffer.last_value + rescue ShortWaitTenAttemptsError + pass end test "discard job" do - perform_enqueued_jobs do - RetryJob.perform_later "DiscardableError", 2 - assert_equal "Raised DiscardableError for the 1st time", JobBuffer.last_value - end + RetryJob.perform_later "DiscardableError", 2 + assert_equal "Raised DiscardableError for the 1st time", JobBuffer.last_value end test "custom handling of discarded job" do - perform_enqueued_jobs do - RetryJob.perform_later "CustomDiscardableError", 2 - assert_equal "Dealt with a job that was discarded in a custom way. Message: CustomDiscardableError", JobBuffer.last_value - end + RetryJob.perform_later "CustomDiscardableError", 2 + assert_equal "Dealt with a job that was discarded in a custom way. Message: CustomDiscardableError", JobBuffer.last_value end test "custom handling of job that exceeds retry attempts" do - perform_enqueued_jobs do - RetryJob.perform_later "CustomCatchError", 6 - assert_equal "Dealt with a job that failed to retry in a custom way after 6 attempts. Message: CustomCatchError", JobBuffer.last_value - end + RetryJob.perform_later "CustomCatchError", 6 + assert_equal "Dealt with a job that failed to retry in a custom way after 6 attempts. Message: CustomCatchError", JobBuffer.last_value end 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 + RetryJob.perform_later "LongWaitError", 2, :log_scheduled_at + + assert_equal [ + "Raised LongWaitError for the 1st time", + "Next execution scheduled at #{(Time.now + 3600.seconds).to_f}", + "Successfully completed job" + ], JobBuffer.values end test "exponentially retrying job" do travel_to Time.now - perform_enqueued_jobs do - assert_performed_with at: (Time.now + 3.seconds).to_i do - assert_performed_with at: (Time.now + 18.seconds).to_i do - assert_performed_with at: (Time.now + 83.seconds).to_i do - assert_performed_with at: (Time.now + 258.seconds).to_i do - RetryJob.perform_later "ExponentialWaitTenAttemptsError", 5 - end - end - end - end - end + RetryJob.perform_later "ExponentialWaitTenAttemptsError", 5, :log_scheduled_at + + assert_equal [ + "Raised ExponentialWaitTenAttemptsError for the 1st time", + "Next execution scheduled at #{(Time.now + 3.seconds).to_f}", + "Raised ExponentialWaitTenAttemptsError for the 2nd time", + "Next execution scheduled at #{(Time.now + 18.seconds).to_f}", + "Raised ExponentialWaitTenAttemptsError for the 3rd time", + "Next execution scheduled at #{(Time.now + 83.seconds).to_f}", + "Raised ExponentialWaitTenAttemptsError for the 4th time", + "Next execution scheduled at #{(Time.now + 258.seconds).to_f}", + "Successfully completed job" + ], JobBuffer.values end test "custom wait retrying job" do travel_to Time.now - perform_enqueued_jobs do - assert_performed_with at: (Time.now + 2.seconds).to_i do - assert_performed_with at: (Time.now + 4.seconds).to_i do - assert_performed_with at: (Time.now + 6.seconds).to_i do - assert_performed_with at: (Time.now + 8.seconds).to_i do - RetryJob.perform_later "CustomWaitTenAttemptsError", 5 - end - end - end - end - end + RetryJob.perform_later "CustomWaitTenAttemptsError", 5, :log_scheduled_at + + assert_equal [ + "Raised CustomWaitTenAttemptsError for the 1st time", + "Next execution scheduled at #{(Time.now + 2.seconds).to_f}", + "Raised CustomWaitTenAttemptsError for the 2nd time", + "Next execution scheduled at #{(Time.now + 4.seconds).to_f}", + "Raised CustomWaitTenAttemptsError for the 3rd time", + "Next execution scheduled at #{(Time.now + 6.seconds).to_f}", + "Raised CustomWaitTenAttemptsError for the 4th time", + "Next execution scheduled at #{(Time.now + 8.seconds).to_f}", + "Successfully completed job" + ], JobBuffer.values end test "successfully retry job throwing one of two retryable exceptions" do - perform_enqueued_jobs do - RetryJob.perform_later "SecondRetryableErrorOfTwo", 3 + RetryJob.perform_later "SecondRetryableErrorOfTwo", 3 - assert_equal [ - "Raised SecondRetryableErrorOfTwo for the 1st time", - "Raised SecondRetryableErrorOfTwo for the 2nd time", - "Successfully completed job" ], JobBuffer.values - end + assert_equal [ + "Raised SecondRetryableErrorOfTwo for the 1st time", + "Raised SecondRetryableErrorOfTwo for the 2nd time", + "Successfully completed job" ], JobBuffer.values end test "discard job throwing one of two discardable exceptions" do - perform_enqueued_jobs do - RetryJob.perform_later "SecondDiscardableErrorOfTwo", 2 - assert_equal [ "Raised SecondDiscardableErrorOfTwo for the 1st time" ], JobBuffer.values - end + RetryJob.perform_later "SecondDiscardableErrorOfTwo", 2 + assert_equal [ "Raised SecondDiscardableErrorOfTwo for the 1st time" ], JobBuffer.values end test "successfully retry job throwing DeserializationError" do - perform_enqueued_jobs do - RetryJob.perform_later Person.new(404), 5 - assert_equal ["Raised ActiveJob::DeserializationError for the 5 time"], JobBuffer.values - end + RetryJob.perform_later Person.new(404), 5 + assert_equal ["Raised ActiveJob::DeserializationError for the 5 time"], JobBuffer.values end + + private + def adapter_skips_scheduling?(queue_adapter) + [ + ActiveJob::QueueAdapters::InlineAdapter, + ActiveJob::QueueAdapters::AsyncAdapter, + ActiveJob::QueueAdapters::SneakersAdapter + ].include?(queue_adapter.class) + end end diff --git a/activejob/test/cases/logging_test.rb b/activejob/test/cases/logging_test.rb index 48ef39aaca..6154ba301d 100644 --- a/activejob/test/cases/logging_test.rb +++ b/activejob/test/cases/logging_test.rb @@ -187,11 +187,9 @@ class LoggingTest < ActiveSupport::TestCase def test_retry_stopped_logging_without_block perform_enqueued_jobs do - begin - RetryJob.perform_later "DefaultsError", 6 - rescue DefaultsError - assert_match(/Stopped retrying RetryJob due to a DefaultsError, which reoccurred on \d+ attempts\./, @logger.messages) - end + RetryJob.perform_later "DefaultsError", 6 + rescue DefaultsError + assert_match(/Stopped retrying RetryJob due to a DefaultsError, which reoccurred on \d+ attempts\./, @logger.messages) end end diff --git a/activejob/test/cases/queuing_test.rb b/activejob/test/cases/queuing_test.rb index 0e843b7215..e7bad83400 100644 --- a/activejob/test/cases/queuing_test.rb +++ b/activejob/test/cases/queuing_test.rb @@ -20,12 +20,10 @@ class QueuingTest < ActiveSupport::TestCase end test "run queued job later" do - begin - result = HelloJob.set(wait_until: 1.second.ago).perform_later "Jamie" - assert result - rescue NotImplementedError - skip - end + result = HelloJob.set(wait_until: 1.second.ago).perform_later "Jamie" + assert result + rescue NotImplementedError + skip end test "job returned by enqueue has the arguments available" do @@ -34,11 +32,9 @@ class QueuingTest < ActiveSupport::TestCase end test "job returned by perform_at has the timestamp available" do - begin - job = HelloJob.set(wait_until: Time.utc(2014, 1, 1)).perform_later - assert_equal Time.utc(2014, 1, 1).to_f, job.scheduled_at - rescue NotImplementedError - skip - end + job = HelloJob.set(wait_until: Time.utc(2014, 1, 1)).perform_later + assert_equal Time.utc(2014, 1, 1).to_f, job.scheduled_at + rescue NotImplementedError + skip end end diff --git a/activejob/test/integration/queuing_test.rb b/activejob/test/integration/queuing_test.rb index 96253773c7..1fa68a8ad5 100644 --- a/activejob/test/integration/queuing_test.rb +++ b/activejob/test/integration/queuing_test.rb @@ -60,25 +60,21 @@ class QueuingTest < ActiveSupport::TestCase end test "should not run job enqueued in the future" do - begin - TestJob.set(wait: 10.minutes).perform_later @id - wait_for_jobs_to_finish_for(5.seconds) - assert_not job_executed - rescue NotImplementedError - skip - end + TestJob.set(wait: 10.minutes).perform_later @id + wait_for_jobs_to_finish_for(5.seconds) + assert_not job_executed + rescue NotImplementedError + skip end test "should run job enqueued in the future at the specified time" do - begin - TestJob.set(wait: 5.seconds).perform_later @id - wait_for_jobs_to_finish_for(2.seconds) - assert_not job_executed - wait_for_jobs_to_finish_for(10.seconds) - assert job_executed - rescue NotImplementedError - skip - end + TestJob.set(wait: 5.seconds).perform_later @id + wait_for_jobs_to_finish_for(2.seconds) + assert_not job_executed + wait_for_jobs_to_finish_for(10.seconds) + assert job_executed + rescue NotImplementedError + skip end test "should supply a provider_job_id when available for immediate jobs" do diff --git a/activejob/test/jobs/retry_job.rb b/activejob/test/jobs/retry_job.rb index 2d19d4c41e..112d672006 100644 --- a/activejob/test/jobs/retry_job.rb +++ b/activejob/test/jobs/retry_job.rb @@ -18,7 +18,7 @@ class CustomDiscardableError < StandardError; end class RetryJob < ActiveJob::Base retry_on DefaultsError - retry_on FirstRetryableErrorOfTwo, SecondRetryableErrorOfTwo + retry_on FirstRetryableErrorOfTwo, SecondRetryableErrorOfTwo, attempts: 4 retry_on LongWaitError, wait: 1.hour, attempts: 10 retry_on ShortWaitTenAttemptsError, wait: 1.second, attempts: 10 retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10 @@ -30,8 +30,15 @@ class RetryJob < ActiveJob::Base discard_on FirstDiscardableErrorOfTwo, SecondDiscardableErrorOfTwo discard_on(CustomDiscardableError) { |job, error| JobBuffer.add("Dealt with a job that was discarded in a custom way. Message: #{error.message}") } - def perform(raising, attempts) - if executions < attempts + before_enqueue do |job| + if job.arguments.include?(:log_scheduled_at) && job.scheduled_at + JobBuffer.add("Next execution scheduled at #{job.scheduled_at}") + end + end + + def perform(raising, attempts, *) + raising = raising.shift if raising.is_a?(Array) + if raising && executions < attempts JobBuffer.add("Raised #{raising} for the #{executions.ordinalize} time") raise raising.constantize else @@ -39,12 +46,3 @@ class RetryJob < ActiveJob::Base end end end - -class ExceptionRetryJob < ActiveJob::Base - retry_on FirstRetryableErrorOfTwo, SecondRetryableErrorOfTwo, attempts: 4 - retry_on DefaultsError - - def perform(exceptions) - raise exceptions.shift.constantize.new unless exceptions.empty? - end -end diff --git a/activejob/test/support/integration/test_case_helpers.rb b/activejob/test/support/integration/test_case_helpers.rb index 3d9b265b66..973ee07764 100644 --- a/activejob/test/support/integration/test_case_helpers.rb +++ b/activejob/test/support/integration/test_case_helpers.rb @@ -33,14 +33,12 @@ module TestCaseHelpers end def wait_for_jobs_to_finish_for(seconds = 60) - begin - Timeout.timeout(seconds) do - while !job_executed do - sleep 0.25 - end + Timeout.timeout(seconds) do + while !job_executed do + sleep 0.25 end - rescue Timeout::Error end + rescue Timeout::Error end def job_file(id) |