Skip to content

Commit f6316ca

Browse files
committed
Fixup have_enqueued_job matcher on job retries
Previously we were checking only job counts, so if one job was performed and one job was added - matcher failed. Check by unique id (`#hash`) instead. We cannot use `job['job_id']` here, because job retains `job_id` on retry.
1 parent 620a869 commit f6316ca

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
- 'main'
66
- '*-maintenance'
77
- '*-dev'
8+
- '*'
89
pull_request:
910
branches:
1011
- '*'

lib/rspec/rails/matchers/active_job.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,11 @@ def initialize(job)
230230
def matches?(proc)
231231
raise ArgumentError, "have_enqueued_job and enqueue_job only support block expectations" unless Proc === proc
232232

233-
original_enqueued_jobs_count = queue_adapter.enqueued_jobs.count
233+
original_enqueued_jobs_hashes = queue_adapter.enqueued_jobs.map(&:hash)
234234
proc.call
235-
in_block_jobs = queue_adapter.enqueued_jobs.drop(original_enqueued_jobs_count)
235+
in_block_jobs = queue_adapter.enqueued_jobs.reject do |job|
236+
original_enqueued_jobs_hashes.include?(job.hash)
237+
end
236238

237239
check(in_block_jobs)
238240
end

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ def self.name; "LoggingJob"; end
9898
expect { }.not_to have_enqueued_job
9999
end
100100

101+
context "when job is retried" do
102+
include ActiveJob::TestHelper
103+
104+
let(:retried_job) do
105+
Class.new(ActiveJob::Base) do
106+
retry_on StandardError, queue: :retry
107+
108+
def self.name; "RetriedJob"; end
109+
def perform; raise StandardError; end
110+
end
111+
end
112+
113+
before { stub_const("RetriedJob", retried_job) }
114+
115+
it "passes with reenqueued job" do
116+
retried_job.perform_later
117+
118+
time = Time.current.change(usec: 0)
119+
travel_to time do
120+
expect { perform_enqueued_jobs(queue: :default) }
121+
.to have_enqueued_job(retried_job).on_queue(:retry).at(time + 3)
122+
end
123+
end
124+
end
125+
101126
it "fails when job is not enqueued" do
102127
expect {
103128
expect { }.to have_enqueued_job

0 commit comments

Comments
 (0)