Skip to content

Commit ab1adca

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 2ae7a2e commit ab1adca

3 files changed

Lines changed: 26 additions & 32 deletions

File tree

spec/jobs/answer_analysis/answer_relevancy_job_spec.rb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
it_behaves_like "a job in queue", "default"
2020
it_behaves_like "a job that adheres to the auto_evaluation quota", AutoEvaluation::AnswerRelevancy
21+
it_behaves_like "a job that retries on service errors", Aws::Errors::ServiceError do
22+
before do
23+
allow(AutoEvaluation::AnswerRelevancy)
24+
.to receive(:call)
25+
.and_raise(Aws::Errors::ServiceError.new(nil, "error"))
26+
end
27+
end
2128

2229
describe "#perform" do
2330
it "calls AutoEvaluation::AnswerRelevancy the configured number of times with the correct arguments" do
@@ -119,21 +126,6 @@
119126
end
120127
end
121128

122-
context "when the AnswerRelevancy metric raises an Aws::Errors::ServiceError" do
123-
it "retries the job the max number of times" do
124-
allow(AutoEvaluation::AnswerRelevancy)
125-
.to receive(:call)
126-
.and_raise(Aws::Errors::ServiceError.new(nil, "error"))
127-
128-
described_class.perform_later(answer.id)
129-
130-
assert_performed_jobs described_class::MAX_RETRIES do
131-
expect { perform_enqueued_jobs }
132-
.to raise_error(Aws::Errors::ServiceError)
133-
end
134-
end
135-
end
136-
137129
context "when the answer is not eligible for auto-evaluation" do
138130
let(:answer) { create(:answer, status: Answer.statuses.except(:answered).keys.sample) }
139131

spec/jobs/answer_analysis/tag_topics_job_spec.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
it_behaves_like "a job in queue", "default"
2222
it_behaves_like "a job that adheres to the auto_evaluation 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)
26+
.to receive(:call)
27+
.and_raise(Anthropic::Errors::APIError.new(url: "url"))
28+
end
29+
end
2330

2431
describe "#perform" do
2532
it "calls the AutoEvaluation::TopicTagger with the answer message" do
@@ -69,23 +76,6 @@
6976
end
7077
end
7178

72-
context "when AutoEvaluation::TopicTagger raises an Anthropic::Errors::APIError" do
73-
it "retries the job the max number of times" do
74-
allow(AutoEvaluation::TopicTagger).to receive(:call)
75-
.and_raise(Anthropic::Errors::APIError.new(
76-
url: "url",
77-
))
78-
79-
(described_class::MAX_RETRIES - 1).times do
80-
described_class.perform_later(answer.id)
81-
expect { perform_enqueued_jobs }.not_to raise_error
82-
end
83-
84-
described_class.perform_later(answer.id)
85-
expect { perform_enqueued_jobs }.to raise_error(Anthropic::Errors::APIError)
86-
end
87-
end
88-
8979
context "when the answer is not eligible for topic analysis" do
9080
let(:answer) { create(:answer, status: Answer::STATUSES_EXCLUDED_FROM_TOPIC_ANALYSIS.sample) }
9181

spec/support/job_examples.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ module JobExamples
3737
described_class.new.perform(answer.id)
3838
end
3939
end
40+
41+
shared_examples "a job that retries on service errors" do |error_class|
42+
let(:answer) { create(:answer) }
43+
it "retries the job the max number of times on #{error_class}" do
44+
described_class.perform_later(answer.id)
45+
46+
assert_performed_jobs described_class::MAX_RETRIES do
47+
expect { perform_enqueued_jobs }
48+
.to raise_error(error_class)
49+
end
50+
end
51+
end
4052
end

0 commit comments

Comments
 (0)