discourse/spec/jobs/run_problem_check_spec.rb
Ted Johansson 3137e60653
DEV: Database backed admin notices (#26192)
This PR introduces a basic AdminNotice model to store these notices. Admin notices are categorized by their source/type (currently only notices from problem check.) They also have a priority.
2024-05-23 09:29:08 +08:00

124 lines
3.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Jobs::RunProblemCheck do
after { Discourse.redis.flushdb }
context "when there are problems" do
around do |example|
ProblemCheck::TestCheck =
Class.new(ProblemCheck) do
self.perform_every = 30.minutes
self.max_retries = 0
def call
[
ProblemCheck::Problem.new("Big problem"),
ProblemCheck::Problem.new(
"Yuge problem",
priority: "high",
identifier: "config_is_a_mess",
),
]
end
end
stub_const(ProblemCheck, "CORE_PROBLEM_CHECKS", [ProblemCheck::TestCheck], &example)
ProblemCheck.send(:remove_const, "TestCheck")
end
it "updates the problem check tracker" do
expect {
described_class.new.execute(check_identifier: :test_check, retry_count: 0)
}.to change { ProblemCheckTracker.failing.count }.by(1)
end
end
context "when there are retries remaining" do
around do |example|
ProblemCheck::TestCheck =
Class.new(ProblemCheck) do
self.perform_every = 30.minutes
self.max_retries = 2
def call
[ProblemCheck::Problem.new("Yuge problem")]
end
end
stub_const(ProblemCheck, "CORE_PROBLEM_CHECKS", [ProblemCheck::TestCheck], &example)
ProblemCheck.send(:remove_const, "TestCheck")
end
it "does not yet update the problem check tracker" do
expect {
described_class.new.execute(check_identifier: :test_check, retry_count: 1)
}.not_to change { ProblemCheckTracker.where("blips > ?", 0).count }
end
it "schedules a retry" do
expect_enqueued_with(
job: :run_problem_check,
args: {
check_identifier: :test_check,
retry_count: 1,
},
) { described_class.new.execute(check_identifier: :test_check) }
end
end
context "when there are no retries remaining" do
around do |example|
ProblemCheck::TestCheck =
Class.new(ProblemCheck) do
self.perform_every = 30.minutes
self.max_retries = 1
def call
[ProblemCheck::Problem.new("Yuge problem")]
end
end
stub_const(ProblemCheck, "CORE_PROBLEM_CHECKS", [ProblemCheck::TestCheck], &example)
ProblemCheck.send(:remove_const, "TestCheck")
end
it "updates the problem check tracker" do
expect {
described_class.new.execute(check_identifier: :test_check, retry_count: 1)
}.to change { ProblemCheckTracker.where("blips > ?", 0).count }.by(1)
end
it "does not schedule a retry" do
expect_not_enqueued_with(job: :run_problem_check) do
described_class.new.execute(check_identifier: :test_check, retry_count: 1)
end
end
end
context "when the check unexpectedly errors out" do
around do |example|
ProblemCheck::TestCheck =
Class.new(ProblemCheck) do
self.max_retries = 1
def call
raise StandardError.new("Something went wrong")
end
end
stub_const(ProblemCheck, "CORE_PROBLEM_CHECKS", [ProblemCheck::TestCheck], &example)
ProblemCheck.send(:remove_const, "TestCheck")
end
it "does not add a problem to the Redis array" do
described_class.new.execute(check_identifier: :test_check)
expect(AdminDashboardData.load_found_scheduled_check_problems).to be_empty
end
end
end