diff options
Diffstat (limited to 'activejob/test/cases')
-rw-r--r-- | activejob/test/cases/argument_serialization_test.rb | 19 | ||||
-rw-r--r-- | activejob/test/cases/exceptions_test.rb | 22 | ||||
-rw-r--r-- | activejob/test/cases/logging_test.rb | 10 | ||||
-rw-r--r-- | activejob/test/cases/queue_naming_test.rb | 29 | ||||
-rw-r--r-- | activejob/test/cases/test_helper_test.rb | 80 |
5 files changed, 142 insertions, 18 deletions
diff --git a/activejob/test/cases/argument_serialization_test.rb b/activejob/test/cases/argument_serialization_test.rb index f07529d743..da198abc0b 100644 --- a/activejob/test/cases/argument_serialization_test.rb +++ b/activejob/test/cases/argument_serialization_test.rb @@ -5,6 +5,7 @@ require "active_job/arguments" require "models/person" require "active_support/core_ext/hash/indifferent_access" require "jobs/kwargs_job" +require "support/stubs/strong_parameters" class ArgumentSerializationTest < ActiveSupport::TestCase setup do @@ -40,6 +41,10 @@ class ArgumentSerializationTest < ActiveSupport::TestCase assert_arguments_roundtrip [@person] end + test "should keep Global IDs strings as they are" do + assert_arguments_roundtrip [@person.to_gid.to_s] + end + test "should dive deep into arrays and hashes" do assert_arguments_roundtrip [3, [@person]] assert_arguments_roundtrip [{ "a" => @person }] @@ -49,6 +54,15 @@ class ArgumentSerializationTest < ActiveSupport::TestCase assert_arguments_roundtrip([a: 1, "b" => 2]) end + test "serialize a ActionController::Parameters" do + parameters = Parameters.new(a: 1) + + assert_equal( + { "a" => 1, "_aj_hash_with_indifferent_access" => true }, + ActiveJob::Arguments.serialize([parameters]).first + ) + end + test "serialize a hash" do symbol_key = { a: 1 } string_key = { "a" => 1 } @@ -73,6 +87,7 @@ class ArgumentSerializationTest < ActiveSupport::TestCase string_key = { "a" => 1, "_aj_symbol_keys" => [] } another_string_key = { "a" => 1 } indifferent_access = { "a" => 1, "_aj_hash_with_indifferent_access" => true } + indifferent_access_symbol_key = symbol_key.with_indifferent_access assert_equal( { a: 1 }, @@ -90,6 +105,10 @@ class ArgumentSerializationTest < ActiveSupport::TestCase { "a" => 1 }, ActiveJob::Arguments.deserialize([indifferent_access]).first ) + assert_equal( + { a: 1 }, + ActiveJob::Arguments.deserialize([indifferent_access_symbol_key]).first + ) end test "should maintain hash with indifferent access" do diff --git a/activejob/test/cases/exceptions_test.rb b/activejob/test/cases/exceptions_test.rb index 37bb65538a..b5328b8d6a 100644 --- a/activejob/test/cases/exceptions_test.rb +++ b/activejob/test/cases/exceptions_test.rb @@ -30,6 +30,28 @@ class ExceptionsTest < ActiveJob::TestCase end end + test "keeps the same attempts counter when several exceptions are listed in the same 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 + end + end + + test "keeps a separate attempts counter for each individual declaration" do + exceptions_to_raise = %w(FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo + DefaultsError DefaultsError) + + assert_nothing_raised do + perform_enqueued_jobs do + ExceptionRetryJob.perform_later(exceptions_to_raise) + end + end + end + test "failed retry job when exception kept occurring against defaults" do perform_enqueued_jobs do begin diff --git a/activejob/test/cases/logging_test.rb b/activejob/test/cases/logging_test.rb index b5bf40c83b..48ef39aaca 100644 --- a/activejob/test/cases/logging_test.rb +++ b/activejob/test/cases/logging_test.rb @@ -169,19 +169,19 @@ class LoggingTest < ActiveSupport::TestCase def test_enqueue_retry_logging perform_enqueued_jobs do RetryJob.perform_later "DefaultsError", 2 - assert_match(/Retrying RetryJob in \d+ seconds, due to a DefaultsError\. The original exception was nil\./, @logger.messages) + assert_match(/Retrying RetryJob in 3 seconds, due to a DefaultsError\./, @logger.messages) end end def test_enqueue_retry_logging_on_retry_job perform_enqueued_jobs { RescueJob.perform_later "david" } - assert_match(/Retrying RescueJob in nil seconds, due to a nil\. The original exception was nil\./, @logger.messages) + assert_match(/Retrying RescueJob in 0 seconds\./, @logger.messages) end def test_retry_stopped_logging perform_enqueued_jobs do RetryJob.perform_later "CustomCatchError", 6 - assert_match(/Stopped retrying RetryJob due to a CustomCatchError, which reoccurred on \d+ attempts\. The original exception was #<CustomCatchError: CustomCatchError>\./, @logger.messages) + assert_match(/Stopped retrying RetryJob due to a CustomCatchError, which reoccurred on \d+ attempts\./, @logger.messages) end end @@ -190,7 +190,7 @@ class LoggingTest < ActiveSupport::TestCase begin RetryJob.perform_later "DefaultsError", 6 rescue DefaultsError - assert_match(/Stopped retrying RetryJob due to a DefaultsError, which reoccurred on \d+ attempts\. The original exception was #<DefaultsError: DefaultsError>\./, @logger.messages) + assert_match(/Stopped retrying RetryJob due to a DefaultsError, which reoccurred on \d+ attempts\./, @logger.messages) end end end @@ -198,7 +198,7 @@ class LoggingTest < ActiveSupport::TestCase def test_discard_logging perform_enqueued_jobs do RetryJob.perform_later "DiscardableError", 2 - assert_match(/Discarded RetryJob due to a DiscardableError\. The original exception was nil\./, @logger.messages) + assert_match(/Discarded RetryJob due to a DiscardableError\./, @logger.messages) end end end diff --git a/activejob/test/cases/queue_naming_test.rb b/activejob/test/cases/queue_naming_test.rb index b64a38f91e..4b43c7c3c5 100644 --- a/activejob/test/cases/queue_naming_test.rb +++ b/activejob/test/cases/queue_naming_test.rb @@ -7,7 +7,7 @@ require "jobs/nested_job" class QueueNamingTest < ActiveSupport::TestCase test "name derived from base" do - assert_equal "default", HelloJob.queue_name + assert_equal "default", HelloJob.new.queue_name end test "uses given queue name job" do @@ -97,6 +97,33 @@ class QueueNamingTest < ActiveSupport::TestCase end end + test "using a custom default_queue_name" do + original_default_queue_name = ActiveJob::Base.default_queue_name + + begin + ActiveJob::Base.default_queue_name = "default_queue_name" + + assert_equal "default_queue_name", HelloJob.new.queue_name + ensure + ActiveJob::Base.default_queue_name = original_default_queue_name + end + end + + test "queue_name_prefix prepended to the default_queue_name" do + original_queue_name_prefix = ActiveJob::Base.queue_name_prefix + original_default_queue_name = ActiveJob::Base.default_queue_name + + begin + ActiveJob::Base.queue_name_prefix = "prefix" + ActiveJob::Base.default_queue_name = "default_queue_name" + + assert_equal "prefix_default_queue_name", HelloJob.new.queue_name + ensure + ActiveJob::Base.queue_name_prefix = original_queue_name_prefix + ActiveJob::Base.default_queue_name = original_default_queue_name + end + end + test "uses queue passed to #set" do job = HelloJob.set(queue: :some_queue).perform_later assert_equal "some_queue", job.queue_name diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index 83c71ab1c4..046033921d 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -114,6 +114,16 @@ class EnqueuedJobsTest < ActiveJob::TestCase end end + def test_assert_enqueued_jobs_with_only_option_as_proc + assert_nothing_raised do + assert_enqueued_jobs(1, only: ->(job) { job.fetch(:job).name == "HelloJob" }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later + LoggingJob.perform_later + end + end + end + def test_assert_enqueued_jobs_with_except_option assert_nothing_raised do assert_enqueued_jobs 1, except: LoggingJob do @@ -124,6 +134,16 @@ class EnqueuedJobsTest < ActiveJob::TestCase end end + def test_assert_enqueued_jobs_with_except_option_as_proc + assert_nothing_raised do + assert_enqueued_jobs(1, except: ->(job) { job.fetch(:job).name == "LoggingJob" }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later + LoggingJob.perform_later + end + end + end + def test_assert_enqueued_jobs_with_only_and_except_option error = assert_raise ArgumentError do assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do @@ -476,23 +496,23 @@ class EnqueuedJobsTest < ActiveJob::TestCase def test_assert_enqueued_with_returns job = assert_enqueued_with(job: LoggingJob) do - LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3) + LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3, keyword: true) end assert_instance_of LoggingJob, job assert_in_delta 5.minutes.from_now, job.scheduled_at, 1 assert_equal "default", job.queue_name - assert_equal [1, 2, 3], job.arguments + assert_equal [1, 2, 3, { keyword: true }], job.arguments end def test_assert_enqueued_with_with_no_block_returns - LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3) + LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3, keyword: true) job = assert_enqueued_with(job: LoggingJob) assert_instance_of LoggingJob, job assert_in_delta 5.minutes.from_now, job.scheduled_at, 1 assert_equal "default", job.queue_name - assert_equal [1, 2, 3], job.arguments + assert_equal [1, 2, 3, { keyword: true }], job.arguments end def test_assert_enqueued_with_failure @@ -911,6 +931,15 @@ class PerformedJobsTest < ActiveJob::TestCase end end + def test_assert_performed_jobs_with_only_option_as_proc + assert_nothing_raised do + assert_performed_jobs(1, only: ->(job) { job.is_a?(HelloJob) }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + end + end + end + def test_assert_performed_jobs_without_block_with_only_option HelloJob.perform_later("jeremy") LoggingJob.perform_later("bogdan") @@ -920,6 +949,15 @@ class PerformedJobsTest < ActiveJob::TestCase assert_performed_jobs 1, only: HelloJob end + def test_assert_performed_jobs_without_block_with_only_option_as_proc + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs(1, only: ->(job) { job.fetch(:job).name == "HelloJob" }) + end + def test_assert_performed_jobs_without_block_with_only_option_failure LoggingJob.perform_later("jeremy") LoggingJob.perform_later("bogdan") @@ -942,6 +980,15 @@ class PerformedJobsTest < ActiveJob::TestCase end end + def test_assert_performed_jobs_with_except_option_as_proc + assert_nothing_raised do + assert_performed_jobs(1, except: ->(job) { job.is_a?(HelloJob) }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + end + end + end + def test_assert_performed_jobs_without_block_with_except_option HelloJob.perform_later("jeremy") LoggingJob.perform_later("bogdan") @@ -951,6 +998,15 @@ class PerformedJobsTest < ActiveJob::TestCase assert_performed_jobs 1, except: HelloJob end + def test_assert_performed_jobs_without_block_with_except_option_as_proc + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs(1, except: ->(job) { job.fetch(:job).name == "HelloJob" }) + end + def test_assert_performed_jobs_without_block_with_except_option_failure HelloJob.perform_later("jeremy") HelloJob.perform_later("bogdan") @@ -1515,26 +1571,26 @@ class PerformedJobsTest < ActiveJob::TestCase end def test_assert_performed_with_returns - job = assert_performed_with(job: NestedJob, queue: "default") do - NestedJob.perform_later + job = assert_performed_with(job: LoggingJob, queue: "default") do + LoggingJob.perform_later(keyword: :sym) end - assert_instance_of NestedJob, job + assert_instance_of LoggingJob, job assert_nil job.scheduled_at - assert_equal [], job.arguments + assert_equal [{ keyword: :sym }], job.arguments assert_equal "default", job.queue_name end def test_assert_performed_with_without_block_returns - NestedJob.perform_later + LoggingJob.perform_later(keyword: :sym) perform_enqueued_jobs - job = assert_performed_with(job: NestedJob, queue: "default") + job = assert_performed_with(job: LoggingJob, queue: "default") - assert_instance_of NestedJob, job + assert_instance_of LoggingJob, job assert_nil job.scheduled_at - assert_equal [], job.arguments + assert_equal [{ keyword: :sym }], job.arguments assert_equal "default", job.queue_name end |