AJ perform_enqueued_jobs shouldn't perform job retries:

- ### Problem

  If we use `perform_enqueued_jobs` without a block, a job that
  uses a retry mechanism to reeenqueue itself would get performed
  right away.
  This behaviour make sense when using `perform_enqueued_jobs` with
  a block.

  However I'm expecting `perform_enqueued_jobs` without a block to
  perform jobs that are **already** in the queue not the ones that
  will get enqueued afterwards.

  ### Solution

  Dup the array of jobs given to avoid future mutation.
This commit is contained in:
Edouard CHIN 2020-03-09 19:33:27 -04:00
parent 13cb5b78a8
commit 17e304def8
3 changed files with 27 additions and 6 deletions

@ -1,3 +1,13 @@
* `ActiveJob::TestCase#perform_enqueued_jobs` will no longer perform retries:
When calling `perform_enqueued_jobs` without a block, the adapter will
now perform jobs that are **already** in the queue. Jobs that will end up in
the queue afterwards won't be performed.
This change only affects `perform_enqueued_jobs` when no block is given.
*Edouard Chin*
* Add queue name support to Que adapter * Add queue name support to Que adapter
*Brad Nauta*, *Wojciech Wnętrzak* *Brad Nauta*, *Wojciech Wnętrzak*

@ -602,7 +602,7 @@ def clear_performed_jobs
def jobs_with(jobs, only: nil, except: nil, queue: nil, at: nil) def jobs_with(jobs, only: nil, except: nil, queue: nil, at: nil)
validate_option(only: only, except: except) validate_option(only: only, except: except)
jobs.count do |job| jobs.dup.count do |job|
job_class = job.fetch(:job) job_class = job.fetch(:job)
if only if only

@ -919,6 +919,17 @@ def test_perform_enqueued_jobs_properly_count_job_that_raises
assert_equal(1, performed_jobs.size) assert_equal(1, performed_jobs.size)
end end
def test_perform_enqueued_jobs_dont_perform_retries
RaisingJob.perform_later
assert_nothing_raised do
perform_enqueued_jobs(only: RaisingJob)
end
assert_equal(1, performed_jobs.size)
assert_equal(2, enqueued_jobs.size)
end
def test_assert_performed_jobs def test_assert_performed_jobs
assert_nothing_raised do assert_nothing_raised do
assert_performed_jobs 1 do assert_performed_jobs 1 do
@ -1877,13 +1888,13 @@ def test_assert_performed_with_without_block_does_not_change_jobs_count
end end
test "TestAdapter respect max attempts" do test "TestAdapter respect max attempts" do
RaisingJob.perform_later perform_enqueued_jobs(only: RaisingJob) do
assert_raises(RaisingJob::MyError) do
assert_raises(RaisingJob::MyError) do RaisingJob.perform_later
perform_enqueued_jobs end
end end
assert_equal 2, queue_adapter.enqueued_jobs.count assert_equal 2, queue_adapter.performed_jobs.count
end end
end end