diff options
author | bogdanvlviv <bogdanvlviv@gmail.com> | 2018-08-16 18:14:53 +0300 |
---|---|---|
committer | bogdanvlviv <bogdanvlviv@gmail.com> | 2018-08-20 00:20:06 +0300 |
commit | ec2e8f645eded183224420e209420376c63b99bb (patch) | |
tree | 6718896aa7aa95a2218d927b96f8590b06e7a3bb /activejob/lib | |
parent | 44007c07098a3c633180881cae9285da4622e63f (diff) | |
download | rails-ec2e8f645eded183224420e209420376c63b99bb.tar.gz rails-ec2e8f645eded183224420e209420376c63b99bb.tar.bz2 rails-ec2e8f645eded183224420e209420376c63b99bb.zip |
Allow `:queue` option to `perform_enqueued_jobs`.
If the `:queue` option is specified, then only the job(s) enqueued to
a specific queue will be performed.
Example:
```
def test_perform_enqueued_jobs_with_queue
perform_enqueued_jobs queue: :some_queue do
MyJob.set(queue: :some_queue).perform_later(1, 2, 3) # will be performed
HelloJob.set(queue: :other_queue).perform_later(1, 2, 3) # will not be performed
end
assert_performed_jobs 1
end
```
Follow up #33265
[bogdanvlviv & Jeremy Daer]
Diffstat (limited to 'activejob/lib')
-rw-r--r-- | activejob/lib/active_job/queue_adapters/test_adapter.rb | 14 | ||||
-rw-r--r-- | activejob/lib/active_job/test_helper.rb | 22 |
2 files changed, 29 insertions, 7 deletions
diff --git a/activejob/lib/active_job/queue_adapters/test_adapter.rb b/activejob/lib/active_job/queue_adapters/test_adapter.rb index 3aa25425eb..f73ad444ba 100644 --- a/activejob/lib/active_job/queue_adapters/test_adapter.rb +++ b/activejob/lib/active_job/queue_adapters/test_adapter.rb @@ -12,7 +12,7 @@ module ActiveJob # # Rails.application.config.active_job.queue_adapter = :test class TestAdapter - attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject) + attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject, :queue) attr_writer(:enqueued_jobs, :performed_jobs) # Provides a store of all the enqueued jobs with the TestAdapter so you can check them. @@ -54,12 +54,20 @@ module ActiveJob end def filtered?(job) + filtered_queue?(job) || filtered_job_class?(job) + end + + def filtered_queue?(job) + if queue + job.queue_name != queue.to_s + end + end + + def filtered_job_class?(job) if filter !Array(filter).include?(job.class) elsif reject Array(reject).include?(job.class) - else - false end end end diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index b45cc57fb0..8f798ac2aa 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -403,25 +403,39 @@ module ActiveJob # assert_performed_jobs 1 # end # - def perform_enqueued_jobs(only: nil, except: nil) + # If the +:queue+ option is specified, + # then only the job(s) enqueued to a specific queue will be performed. + # + # def test_perform_enqueued_jobs_with_queue + # perform_enqueued_jobs queue: :some_queue do + # MyJob.set(queue: :some_queue).perform_later(1, 2, 3) # will be performed + # HelloJob.set(queue: :other_queue).perform_later(1, 2, 3) # will not be performed + # end + # assert_performed_jobs 1 + # end + # + def perform_enqueued_jobs(only: nil, except: nil, queue: nil) validate_option(only: only, except: except) old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs old_filter = queue_adapter.filter old_reject = queue_adapter.reject + old_queue = queue_adapter.queue begin queue_adapter.perform_enqueued_jobs = true queue_adapter.perform_enqueued_at_jobs = true queue_adapter.filter = only queue_adapter.reject = except + queue_adapter.queue = queue - block_given? ? yield : flush_enqueued_jobs(only: only, except: except) + block_given? ? yield : flush_enqueued_jobs(only: only, except: except, queue: queue) ensure queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs queue_adapter.filter = old_filter queue_adapter.reject = old_reject + queue_adapter.queue = old_queue end end @@ -463,8 +477,8 @@ module ActiveJob end end - def flush_enqueued_jobs(only: nil, except: nil) - enqueued_jobs_with(only: only, except: except) do |payload| + def flush_enqueued_jobs(only: nil, except: nil, queue: nil) + enqueued_jobs_with(only: only, except: except, queue: queue) do |payload| args = ActiveJob::Arguments.deserialize(payload[:args]) instantiate_job(payload.merge(args: args)).perform_now queue_adapter.performed_jobs << payload |