2019-11-24 12:06:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.shared_context 'simple_check' do |metrics_prefix, check_name, success_result|
|
2017-04-07 10:27:15 +00:00
|
|
|
describe '#metrics' do
|
|
|
|
subject { described_class.metrics }
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2017-04-07 10:27:15 +00:00
|
|
|
context 'Check is passing' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:check).and_return success_result
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_success", value: 1)) }
|
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_timeout", value: 0)) }
|
2017-07-12 12:48:46 +00:00
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_latency_seconds", value: be >= 0)) }
|
2017-04-07 10:27:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'Check is misbehaving' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:check).and_return 'error!'
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_success", value: 0)) }
|
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_timeout", value: 0)) }
|
2017-07-12 12:48:46 +00:00
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_latency_seconds", value: be >= 0)) }
|
2017-04-07 10:27:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'Check is timeouting' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:check).and_return Timeout::Error.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_success", value: 0)) }
|
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_timeout", value: 1)) }
|
2017-07-12 12:48:46 +00:00
|
|
|
it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_latency_seconds", value: be >= 0)) }
|
2017-04-07 10:27:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#readiness' do
|
|
|
|
subject { described_class.readiness }
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2017-04-07 10:27:15 +00:00
|
|
|
context 'Check returns ok' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:check).and_return success_result
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to have_attributes(success: true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Check is misbehaving' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:check).and_return 'error!'
|
|
|
|
end
|
|
|
|
|
2017-07-11 03:35:47 +00:00
|
|
|
it { is_expected.to have_attributes(success: false, message: "unexpected #{described_class.human_name} check result: error!") }
|
2017-04-07 10:27:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'Check is timeouting' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:check ).and_return Timeout::Error.new
|
|
|
|
end
|
|
|
|
|
2017-07-11 03:35:47 +00:00
|
|
|
it { is_expected.to have_attributes(success: false, message: "#{described_class.human_name} check timed out") }
|
2017-04-07 10:27:15 +00:00
|
|
|
end
|
2019-09-26 15:06:18 +00:00
|
|
|
|
|
|
|
context 'Check is raising an unhandled exception' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:check ).and_raise "unexpected error"
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to have_attributes(success: false, message: "unexpected #{described_class.human_name} check result: unexpected error") }
|
|
|
|
end
|
2017-04-07 10:27:15 +00:00
|
|
|
end
|
|
|
|
end
|