aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/test/cases
diff options
context:
space:
mode:
Diffstat (limited to 'activejob/test/cases')
-rw-r--r--activejob/test/cases/callbacks_test.rb28
-rw-r--r--activejob/test/cases/exceptions_test.rb239
-rw-r--r--activejob/test/cases/job_serialization_test.rb11
-rw-r--r--activejob/test/cases/logging_test.rb10
-rw-r--r--activejob/test/cases/queuing_test.rb20
-rw-r--r--activejob/test/cases/test_helper_test.rb56
6 files changed, 236 insertions, 128 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..1f07b7b294 100644
--- a/activejob/test/cases/exceptions_test.rb
+++ b/activejob/test/cases/exceptions_test.rb
@@ -4,161 +4,212 @@ 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 "use individual execution timers when calculating retry delay" do
+ travel_to Time.now
+
+ exceptions_to_raise = %w(ExponentialWaitTenAttemptsError CustomWaitTenAttemptsError ExponentialWaitTenAttemptsError CustomWaitTenAttemptsError)
+
+ RetryJob.perform_later exceptions_to_raise, 5, :log_scheduled_at
+
+ assert_equal [
+ "Raised ExponentialWaitTenAttemptsError for the 1st time",
+ "Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
+ "Raised CustomWaitTenAttemptsError for the 2nd time",
+ "Next execution scheduled at #{(Time.now + 2.seconds).to_f}",
+ "Raised ExponentialWaitTenAttemptsError for the 3rd time",
+ "Next execution scheduled at #{(Time.now + 18.seconds).to_f}",
+ "Raised CustomWaitTenAttemptsError for the 4th time",
+ "Next execution scheduled at #{(Time.now + 4.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
+ RetryJob.perform_later Person.new(404), 5
+ assert_equal ["Raised ActiveJob::DeserializationError for the 5 time"], JobBuffer.values
+ end
+
+ test "running a job enqueued by AJ 5.2" do
+ job = RetryJob.new("DefaultsError", 6)
+ job.exception_executions = nil # This is how jobs from Rails 5.2 will look
+
+ assert_raises DefaultsError do
+ job.enqueue
+ end
+
+ assert_equal 5, JobBuffer.values.count
+ end
+
+ test "running a job enqueued and attempted under AJ 5.2" do
+ job = RetryJob.new("DefaultsError", 6)
+
+ # Fake 4 previous executions under AJ 5.2
+ job.exception_executions = nil
+ job.executions = 4
+
+ assert_raises DefaultsError do
+ job.enqueue
end
+
+ assert_equal ["Raised DefaultsError for the 5th 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/job_serialization_test.rb b/activejob/test/cases/job_serialization_test.rb
index 86f3651564..c1cec1f1d6 100644
--- a/activejob/test/cases/job_serialization_test.rb
+++ b/activejob/test/cases/job_serialization_test.rb
@@ -61,4 +61,15 @@ class JobSerializationTest < ActiveSupport::TestCase
assert_equal "Hawaii", job.serialize["timezone"]
end
end
+
+ test "serialize stores the enqueued_at time" do
+ h1 = HelloJob.new
+ type = h1.serialize["enqueued_at"].class
+ assert_equal String, type
+
+ h2 = HelloJob.deserialize(h1.serialize)
+ # We should be able to parse a timestamp
+ type = Time.parse(h2.enqueued_at).class
+ assert_equal Time, type
+ end
end
diff --git a/activejob/test/cases/logging_test.rb b/activejob/test/cases/logging_test.rb
index 48ef39aaca..acd37456c9 100644
--- a/activejob/test/cases/logging_test.rb
+++ b/activejob/test/cases/logging_test.rb
@@ -115,6 +115,8 @@ class LoggingTest < ActiveSupport::TestCase
perform_enqueued_jobs do
LoggingJob.perform_later "Dummy"
assert_match(/Performing LoggingJob \(Job ID: .*?\) from .*? with arguments:.*Dummy/, @logger.messages)
+
+ assert_match(/enqueued at /, @logger.messages)
assert_match(/Dummy, here is it: Dummy/, @logger.messages)
assert_match(/Performed LoggingJob \(Job ID: .*?\) from .*? in .*ms/, @logger.messages)
end
@@ -187,11 +189,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/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb
index 046033921d..32471cfacc 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -581,6 +581,33 @@ class EnqueuedJobsTest < ActiveJob::TestCase
end
end
+ def test_assert_enqueued_with_time
+ now = Time.now
+ args = [{ argument1: [now] }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: [now])
+ end
+ end
+
+ def test_assert_enqueued_with_date_time
+ now = DateTime.now
+ args = [{ argument1: [now] }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: [now])
+ end
+ end
+
+ def test_assert_enqueued_with_time_with_zone
+ now = Time.now.in_time_zone("Tokyo")
+ args = [{ argument1: [now] }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: [now])
+ end
+ end
+
def test_assert_enqueued_with_with_no_block_args
assert_raise ArgumentError do
NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
@@ -1681,6 +1708,33 @@ class PerformedJobsTest < ActiveJob::TestCase
end
end
+ def test_assert_performed_with_time
+ now = Time.now
+ args = [{ argument1: { now: now }, argument2: now }]
+
+ assert_performed_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: { now: now }, argument2: now)
+ end
+ end
+
+ def test_assert_performed_with_date_time
+ now = DateTime.now
+ args = [{ argument1: { now: now }, argument2: now }]
+
+ assert_performed_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: { now: now }, argument2: now)
+ end
+ end
+
+ def test_assert_performed_with_time_with_zone
+ now = Time.now.in_time_zone("Tokyo")
+ args = [{ argument1: { now: now }, argument2: now }]
+
+ assert_performed_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: { now: now }, argument2: now)
+ end
+ end
+
def test_assert_performed_with_with_global_id_args
ricardo = Person.new(9)
assert_performed_with(job: HelloJob, args: [ricardo]) do
@@ -1688,7 +1742,7 @@ class PerformedJobsTest < ActiveJob::TestCase
end
end
- def test_assert_performed_with_without_bllock_with_global_id_args
+ def test_assert_performed_with_without_block_with_global_id_args
ricardo = Person.new(9)
HelloJob.perform_later(ricardo)
perform_enqueued_jobs