Skip to content

Commit 319ae5b

Browse files
committed
Add shared example for retries on service errors
There's essentially duplicate tests for retries in the topics and relevancy job specs. This adds a shared example we can reuse across all the metric specs.
1 parent e0a6336 commit 319ae5b

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

spec/jobs/answer_analysis/answer_relevancy_job_spec.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757

5858
it_behaves_like "a job in queue", "default"
5959
it_behaves_like "a job that adheres to the metric quota", AutoEvaluation::AnswerRelevancy
60+
it_behaves_like "a job that retries on service errors", Aws::Errors::ServiceError do
61+
before do
62+
allow(AutoEvaluation::AnswerRelevancy).to receive(:call)
63+
.and_raise(
64+
Aws::Errors::ServiceError.new(nil, "error"),
65+
)
66+
end
67+
end
6068

6169
describe "#perform" do
6270
it "calls AutoEvaluation::AnswerRelevancy the configured number of times with the correct arguments" do
@@ -169,21 +177,6 @@
169177
end
170178
end
171179

172-
context "when the AnswerRelevancy metric raises an Aws::Errors::ServiceError" do
173-
it "retries the job the max number of times" do
174-
allow(AutoEvaluation::AnswerRelevancy).to receive(:call)
175-
.and_raise(Aws::Errors::ServiceError.new(nil, "error"))
176-
177-
(described_class::MAX_RETRIES - 1).times do
178-
described_class.perform_later(answer.id)
179-
expect { perform_enqueued_jobs }.not_to raise_error
180-
end
181-
182-
described_class.perform_later(answer.id)
183-
expect { perform_enqueued_jobs }.to raise_error(Aws::Errors::ServiceError)
184-
end
185-
end
186-
187180
context "when the answer is not eligible for auto-evaluation" do
188181
let(:answer) { create(:answer, status: Answer.statuses.except(:answered).keys.sample) }
189182

spec/jobs/answer_analysis/tag_topics_job_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
it_behaves_like "a job in queue", "default"
2222
it_behaves_like "a job that adheres to the metric quota", AutoEvaluation::TopicTagger
23+
it_behaves_like "a job that retries on service errors", Anthropic::Errors::APIError do
24+
before do
25+
allow(AutoEvaluation::TopicTagger).to receive(:call)
26+
.and_raise(Anthropic::Errors::APIError.new(url: "url"))
27+
end
28+
end
2329

2430
describe "#perform" do
2531
it "calls the AutoEvaluation::TopicTagger with the answer message" do

spec/support/job_examples.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,17 @@ module JobExamples
3535
described_class.new.perform(answer.id)
3636
end
3737
end
38+
39+
shared_examples "a job that retries on service errors" do |error_class|
40+
let(:answer) { create(:answer) }
41+
it "retries the job the max number of times on #{error_class}" do
42+
(described_class::MAX_RETRIES - 1).times do
43+
described_class.perform_later(answer.id)
44+
expect { perform_enqueued_jobs }.not_to raise_error
45+
end
46+
47+
described_class.perform_later(answer.id)
48+
expect { perform_enqueued_jobs }.to raise_error(error_class)
49+
end
50+
end
3851
end

0 commit comments

Comments
 (0)