Skip to content

Commit f123e5e

Browse files
committed
Fix perform_enqueued_jobs without a block with other helpers
assert_enqueued_with with a block ignores all the jobs enqueued before the block for its assertions by counting the number of jobs and dropping the n first elements from the Array, but since we're now mutating the Array in perform_enqueued_jobs without a block, it's broken. This uses another implementation which is correct when the array is mutated, by getting a duplicated array of jobs, then removing them from the original array. Similarly assert_enqueued_jobs with a block was using counts only, now keeps track of the specific jobs to count them at the end.
1 parent 855c989 commit f123e5e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

activejob/lib/active_job/test_helper.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ def queue_adapter_for_test
119119
# end
120120
def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block)
121121
if block_given?
122-
original_count = enqueued_jobs_with(only: only, except: except, queue: queue)
122+
original_jobs = enqueued_jobs_with(only: only, except: except, queue: queue)
123123

124124
assert_nothing_raised(&block)
125125

126-
new_count = enqueued_jobs_with(only: only, except: except, queue: queue)
126+
new_jobs = enqueued_jobs_with(only: only, except: except, queue: queue)
127127

128-
actual_count = new_count - original_count
128+
actual_count = (new_jobs - original_jobs).count
129129
else
130-
actual_count = enqueued_jobs_with(only: only, except: except, queue: queue)
130+
actual_count = enqueued_jobs_with(only: only, except: except, queue: queue).count
131131
end
132132

133133
assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued"
@@ -279,7 +279,7 @@ def assert_performed_jobs(number, only: nil, except: nil, queue: nil, &block)
279279

280280
performed_jobs_size = new_count - original_count
281281
else
282-
performed_jobs_size = performed_jobs_with(only: only, except: except, queue: queue)
282+
performed_jobs_size = performed_jobs_with(only: only, except: except, queue: queue).count
283283
end
284284

285285
assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed"
@@ -385,11 +385,11 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil, &block)
385385
potential_matches = []
386386

387387
if block_given?
388-
original_enqueued_jobs_count = enqueued_jobs.count
388+
original_enqueued_jobs = enqueued_jobs.dup
389389

390390
assert_nothing_raised(&block)
391391

392-
jobs = enqueued_jobs.drop(original_enqueued_jobs_count)
392+
jobs = enqueued_jobs - original_enqueued_jobs
393393
else
394394
jobs = enqueued_jobs
395395
end
@@ -602,7 +602,7 @@ def clear_performed_jobs
602602
def jobs_with(jobs, only: nil, except: nil, queue: nil, at: nil)
603603
validate_option(only: only, except: except)
604604

605-
jobs.dup.count do |job|
605+
jobs.dup.select do |job|
606606
job_class = job.fetch(:job)
607607

608608
if only
@@ -644,7 +644,7 @@ def flush_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil)
644644
queue_adapter.enqueued_jobs.delete(payload)
645645
queue_adapter.performed_jobs << payload
646646
instantiate_job(payload).perform_now
647-
end
647+
end.count
648648
end
649649

650650
def prepare_args_for_assertion(args)

activejob/test/cases/test_helper_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,19 @@ def test_perform_enqueued_jobs_without_block_removes_from_enqueued_jobs
961961
assert_equal(0, enqueued_jobs.size)
962962
end
963963

964+
def test_perform_enqueued_jobs_without_block_works_with_other_helpers
965+
NestedJob.perform_later
966+
assert_equal(0, performed_jobs.size)
967+
assert_equal(1, enqueued_jobs.size)
968+
assert_enqueued_jobs(1) do
969+
assert_enqueued_with(job: LoggingJob) do
970+
perform_enqueued_jobs
971+
end
972+
end
973+
assert_equal(1, performed_jobs.size)
974+
assert_equal(1, enqueued_jobs.size)
975+
end
976+
964977
def test_perform_enqueued_jobs_without_block_only_performs_once
965978
JobBuffer.clear
966979
RescueJob.perform_later("no exception")

0 commit comments

Comments
 (0)