Skip to content

Commit a8be0c2

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 30d5ab2 commit a8be0c2

4 files changed

Lines changed: 27 additions & 33 deletions

File tree

app/jobs/answer_analysis/answer_topics_job.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module AnswerAnalysis
22
class AnswerTopicsJob < BaseMetricJob
3-
MAX_RETRIES = 5
43
retry_on Anthropic::Errors::APIError, wait: 1.minute, attempts: MAX_RETRIES
54

65
def perform(answer_id)

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/answer_topics_job_spec.rb

Lines changed: 6 additions & 17 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", TopicTagger
23+
it_behaves_like "a job that retries on service errors", Anthropic::Errors::APIError do
24+
before do
25+
allow(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 TopicTagger with the answer message" do
@@ -79,23 +85,6 @@
7985
end
8086
end
8187

82-
context "when TopicTagger raises an Anthropic::Errors::APIError" do
83-
it "retries the job the max number of times" do
84-
allow(TopicTagger).to receive(:call)
85-
.and_raise(Anthropic::Errors::APIError.new(
86-
url: "url",
87-
))
88-
89-
(described_class::MAX_RETRIES - 1).times do
90-
described_class.perform_later(answer.id)
91-
expect { perform_enqueued_jobs }.not_to raise_error
92-
end
93-
94-
described_class.perform_later(answer.id)
95-
expect { perform_enqueued_jobs }.to raise_error(Anthropic::Errors::APIError)
96-
end
97-
end
98-
9988
context "when the answer is not eligible for topic analysis" do
10089
let(:answer) { create(:answer, status: Answer::STATUSES_EXCLUDED_FROM_TOPIC_ANALYSIS.sample) }
10190

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)