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

Merge pull request #38749 from etiennebarrie/perform_enqueued_jobs_only_once

Perform enqueued jobs only once
This commit is contained in:
Rafael Mendonça França 2020-03-19 18:29:18 -04:00
commit d9dc569d5b
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948
3 changed files with 35 additions and 2 deletions

View file

@ -1,3 +1,16 @@
* `ActiveJob::TestCase#perform_enqueued_jobs` without a block removes performed jobs from the queue.
That way the helper can be called multiple times and not perform a job invocation multiple times.
```ruby
def test_jobs
HelloJob.perform_later("rafael")
perform_enqueued_jobs # only runs with "rafael"
HelloJob.perform_later("david")
perform_enqueued_jobs # only runs with "david"
end
```
* `ActiveJob::TestCase#perform_enqueued_jobs` will no longer perform retries: * `ActiveJob::TestCase#perform_enqueued_jobs` will no longer perform retries:
When calling `perform_enqueued_jobs` without a block, the adapter will When calling `perform_enqueued_jobs` without a block, the adapter will

View file

@ -641,6 +641,7 @@ module ActiveJob
def flush_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil) def flush_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil)
enqueued_jobs_with(only: only, except: except, queue: queue, at: at) do |payload| enqueued_jobs_with(only: only, except: except, queue: queue, at: at) do |payload|
queue_adapter.enqueued_jobs.delete(payload)
queue_adapter.performed_jobs << payload queue_adapter.performed_jobs << payload
instantiate_job(payload).perform_now instantiate_job(payload).perform_now
end end

View file

@ -927,7 +927,24 @@ class PerformedJobsTest < ActiveJob::TestCase
end end
assert_equal(1, performed_jobs.size) assert_equal(1, performed_jobs.size)
assert_equal(2, enqueued_jobs.size) assert_equal(1, enqueued_jobs.size)
end
def test_perform_enqueued_jobs_without_block_removes_from_enqueued_jobs
HelloJob.perform_later("rafael")
assert_equal(0, performed_jobs.size)
assert_equal(1, enqueued_jobs.size)
perform_enqueued_jobs
assert_equal(1, performed_jobs.size)
assert_equal(0, enqueued_jobs.size)
end
def test_perform_enqueued_jobs_without_block_only_performs_once
JobBuffer.clear
RescueJob.perform_later("no exception")
perform_enqueued_jobs
perform_enqueued_jobs
assert_equal(1, JobBuffer.values.size)
end end
def test_assert_performed_jobs def test_assert_performed_jobs
@ -1872,6 +1889,7 @@ class PerformedJobsTest < ActiveJob::TestCase
HelloJob.perform_later HelloJob.perform_later
end end
assert_equal 0, queue_adapter.enqueued_jobs.count
assert_equal 2, queue_adapter.performed_jobs.count assert_equal 2, queue_adapter.performed_jobs.count
end end
@ -1880,10 +1898,11 @@ class PerformedJobsTest < ActiveJob::TestCase
perform_enqueued_jobs perform_enqueued_jobs
assert_performed_with(job: HelloJob) assert_performed_with(job: HelloJob)
perform_enqueued_jobs
HelloJob.perform_later HelloJob.perform_later
perform_enqueued_jobs
assert_performed_with(job: HelloJob) assert_performed_with(job: HelloJob)
assert_equal 0, queue_adapter.enqueued_jobs.count
assert_equal 2, queue_adapter.performed_jobs.count assert_equal 2, queue_adapter.performed_jobs.count
end end