aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activejob/CHANGELOG.md6
-rw-r--r--activejob/lib/active_job/test_helper.rb27
-rw-r--r--activejob/test/cases/test_helper_test.rb20
3 files changed, 45 insertions, 8 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md
index 8526741383..8c4a74f739 100644
--- a/activejob/CHANGELOG.md
+++ b/activejob/CHANGELOG.md
@@ -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
diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index 04cde28a96..e9b1d5c6fc 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -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]
diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb
index d0a21a5da3..53b332f6e3 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -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