1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow perform_enqueued_jobs to be called without a block.

Performs all of the jobs that have been enqueued up to this point in the test.
This commit is contained in:
Kevin Deisz 2018-08-15 15:00:40 -04:00
parent 977d77e9e2
commit ec16301488
No known key found for this signature in database
GPG key ID: D78C2D8FB232C59C
3 changed files with 45 additions and 8 deletions

View file

@ -1,3 +1,9 @@
* Allow `perform_enqueued_jobs` to be called without a block.
Performs all of the jobs that have been enqueued up to this point in the test.
*Kevin Deisz*
* Move `enqueue`/`enqueue_at` notifications to an around callback.
Improves timing accuracy over the old after callback by including

View file

@ -117,12 +117,12 @@ module ActiveJob
# end
def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil)
if block_given?
original_count = enqueued_jobs_size(only: only, except: except, queue: queue)
original_count = enqueued_jobs_with(only: only, except: except, queue: queue)
yield
new_count = enqueued_jobs_size(only: only, except: except, queue: queue)
new_count = enqueued_jobs_with(only: only, except: except, queue: queue)
assert_equal number, new_count - original_count, "#{number} jobs expected, but #{new_count - original_count} were enqueued"
else
actual_count = enqueued_jobs_size(only: only, except: except, queue: queue)
actual_count = enqueued_jobs_with(only: only, except: except, queue: queue)
assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued"
end
end
@ -362,7 +362,9 @@ module ActiveJob
instantiate_job(matching_job)
end
# Performs all enqueued jobs in the duration of the block.
# Performs all enqueued jobs. If a block is given, performs all of the jobs
# that were enqueued throughout the duration of the block. If a block is
# not given, performs all of the enqueued jobs up to this point in the test.
#
# def test_perform_enqueued_jobs
# perform_enqueued_jobs do
@ -405,7 +407,8 @@ module ActiveJob
queue_adapter.perform_enqueued_at_jobs = true
queue_adapter.filter = only
queue_adapter.reject = except
yield
block_given? ? yield : flush_enqueued_jobs(only: only, except: except)
ensure
queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs
queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs
@ -432,10 +435,12 @@ module ActiveJob
performed_jobs.clear
end
def enqueued_jobs_size(only: nil, except: nil, queue: nil)
def enqueued_jobs_with(only: nil, except: nil, queue: nil)
validate_option(only: only, except: except)
enqueued_jobs.count do |job|
job_class = job.fetch(:job)
if only
next false unless Array(only).include?(job_class)
elsif except
@ -444,10 +449,20 @@ module ActiveJob
if queue
next false unless queue.to_s == job.fetch(:queue, job_class.queue_name)
end
yield job if block_given?
true
end
end
def flush_enqueued_jobs(only: nil, except: nil)
enqueued_jobs_with(only: only, except: except) do |payload|
args = ActiveJob::Arguments.deserialize(payload[:args])
instantiate_job(payload.merge(args: args)).perform_now
queue_adapter.performed_jobs << payload
end
end
def serialize_args_for_assertion(args)
args.dup.tap do |serialized_args|
serialized_args[:args] = ActiveJob::Arguments.serialize(serialized_args[:args]) if serialized_args[:args]

View file

@ -610,7 +610,7 @@ class EnqueuedJobsTest < ActiveJob::TestCase
end
class PerformedJobsTest < ActiveJob::TestCase
def test_performed_enqueue_jobs_with_only_option_doesnt_leak_outside_the_block
def test_perform_enqueued_jobs_with_only_option_doesnt_leak_outside_the_block
assert_nil queue_adapter.filter
perform_enqueued_jobs only: HelloJob do
assert_equal HelloJob, queue_adapter.filter
@ -618,7 +618,7 @@ class PerformedJobsTest < ActiveJob::TestCase
assert_nil queue_adapter.filter
end
def test_performed_enqueue_jobs_with_except_option_doesnt_leak_outside_the_block
def test_perform_enqueued_jobs_with_except_option_doesnt_leak_outside_the_block
assert_nil queue_adapter.reject
perform_enqueued_jobs except: HelloJob do
assert_equal HelloJob, queue_adapter.reject
@ -626,6 +626,22 @@ class PerformedJobsTest < ActiveJob::TestCase
assert_nil queue_adapter.reject
end
def test_perform_enqueued_jobs_without_block
HelloJob.perform_later("kevin")
assert_performed_jobs 1, only: HelloJob do
perform_enqueued_jobs
end
end
def test_perform_enqueued_jobs_without_block_respects_filter
HelloJob.perform_later("kevin")
assert_no_performed_jobs do
perform_enqueued_jobs only: LoggingJob
end
end
def test_assert_performed_jobs
assert_nothing_raised do
assert_performed_jobs 1 do