Skip to content

Commit e1cb725

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 5713482 commit e1cb725

3 files changed

Lines changed: 26 additions & 15 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
@@ -19,6 +19,13 @@
1919

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

2330
describe "#perform" do
2431
it "calls AutoEvaluation::AnswerRelevancy the configured number of times with the correct arguments" do
@@ -126,21 +133,6 @@
126133
end
127134
end
128135

129-
context "when the AnswerRelevancy metric raises an Aws::Errors::ServiceError" do
130-
it "retries the job the max number of times" do
131-
allow(AutoEvaluation::AnswerRelevancy)
132-
.to receive(:call)
133-
.and_raise(Aws::Errors::ServiceError.new(nil, "error"))
134-
135-
described_class.perform_later(answer.id)
136-
137-
assert_performed_jobs described_class::MAX_RETRIES do
138-
expect { perform_enqueued_jobs }
139-
.to raise_error(Aws::Errors::ServiceError)
140-
end
141-
end
142-
end
143-
144136
context "when the answer is not eligible for auto-evaluation" do
145137
let(:answer) { create(:answer, status: Answer.statuses.except(:answered).keys.sample) }
146138

spec/jobs/answer_analysis/tag_topics_job_spec.rb

Lines changed: 7 additions & 0 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 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)
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

spec/support/job_examples.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ 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.perform_later(answer.id)
43+
44+
assert_performed_jobs described_class::MAX_RETRIES do
45+
expect { perform_enqueued_jobs }
46+
.to raise_error(error_class)
47+
end
48+
end
49+
end
3850
end

0 commit comments

Comments
 (0)