spec/reporter.rb in health-reporter-0.0.3 vs spec/reporter.rb in health-reporter-0.1.0
- old
+ new
@@ -11,10 +11,11 @@
subject.healthy_cache_ttl = 60
subject.unhealthy_cache_ttl = 30
subject.self_test = lambda{ true }
subject.class_variable_set(:@@last_check_time, nil)
subject.class_variable_set(:@@healthy, nil)
+ subject.clear_dependencies
Timecop.return
reset_lambda_runner_spy
end
context 'when configuring' do
@@ -39,10 +40,42 @@
it 'remembers the cache ttl when not healthy' do
subject.unhealthy_cache_ttl = 5
expect(subject.unhealthy_cache_ttl).to eq 5
end
+
+ it 'remembers when you add a dependencies' do
+ subject.register_dependency(url: 'https://hardware-store/status', code: 123, timeout: 1)
+ expect(subject.dependencies).to eq({
+ 'https://hardware-store/status' => { :code => 123, :timeout => 1 }
+ })
+ end
+
+ it 'validates the urls of the dependencies during registration' do
+ expect{subject.register_dependency(url: 'no-valid-url')}.to raise_error RuntimeError, "Configured URL no-valid-url is invalid"
+ end
+
+ it 'adds dependencies without removing the dependencies already registered' do
+ subject.register_dependency(url: 'https://hardware-store/status', code: 123, timeout: 1)
+ subject.register_dependency(url: 'https://grocery-store/status', code: 123, timeout: 1)
+ expect(subject.dependencies).to eq({
+ 'https://hardware-store/status' => { :code => 123, :timeout => 1 },
+ 'https://grocery-store/status' => { :code => 123, :timeout => 1 }
+ })
+ end
+
+ it 'does not duplicate similar dependency urls' do
+ subject.register_dependency(url: 'https://hardware-store/status', code: 123, timeout: 1)
+ subject.register_dependency(url: 'https://hardware-store/status', code: 123, timeout: 1)
+ subject.register_dependency(url: 'https://hardware-store/status', code: 123, timeout: 1)
+ subject.register_dependency(url: 'https://hardware-store/status', code: 123, timeout: 1)
+ subject.register_dependency(url: 'https://grocery-store/status', code: 123, timeout: 1)
+ expect(subject.dependencies).to eq({
+ 'https://hardware-store/status' => { :code => 123, :timeout => 1 },
+ 'https://grocery-store/status' => { :code => 123, :timeout => 1 }
+ })
+ end
end
context 'when exercising self-test lambda' do
it 'allows true to be returned by self-test lambda' do
test_lambda = lambda{ true }
@@ -191,9 +224,56 @@
subject.healthy? #request here and test if it was run in expect below
expect(spy_lambda_was_run?).to eq true
end
it 'returns the current unhealthy state' do
expect(subject.healthy?).to be false
+ end
+ end
+ end
+
+ context 'when checking dependencies' do
+ context 'when there are no dependencies registered' do
+ it 'only performs the self-test' do
+ subject.self_test = spy_lambda_returning_false
+ expect(subject.healthy?).to be false
+ expect(spy_lambda_was_run?).to eq true
+ end
+ end
+
+ context 'when there are multiple dependencies registered' do
+ before(:each) do
+ subject.register_dependency(url: 'https://hardware-store/status')
+ subject.register_dependency(url: 'https://grocery-store/status')
+ subject.self_test = spy_lambda_returning_true
+ end
+
+ it 'performs the self-test and checks all dependencies' do
+ stub_request(:get, "https://hardware-store/status").to_return(:status => 200, :body => "", :headers => {})
+ stub_request(:get, "https://grocery-store/status").to_return(:status => 200, :body => "", :headers => {})
+ expect(subject.healthy?).to be true
+ expect(spy_lambda_was_run?).to eq true
+ end
+
+ it 'indicates healthy if all of the dependencies are healthy' do
+ stub_request(:get, "https://hardware-store/status").to_return(:status => 200, :body => "", :headers => {})
+ stub_request(:get, "https://grocery-store/status").to_return(:status => 200, :body => "", :headers => {})
+ expect(subject.healthy?).to be true
+ expect(spy_lambda_was_run?).to eq true
+ end
+
+ it 'raises a detailed exception indicating why a dependency was determined to be unhealthy state was uncached' do
+ stub_request(:get, "https://hardware-store/status").to_return(:status => 500, :body => "", :headers => {})
+ stub_request(:get, "https://grocery-store/status").to_return(:status => 200, :body => "", :headers => {})
+ expect{subject.healthy?}.to raise_error RuntimeError, "Dependency <https://hardware-store/status> failed check due to RuntimeError: Response expected to be 200 but is 500"
+ end
+
+ it 'indicates cached unhealthy state if it is unhealthy because a dependency was unhealthy' do
+ stub_request(:get, "https://hardware-store/status").to_return(:status => 500, :body => "", :headers => {})
+ stub_request(:get, "https://grocery-store/status").to_return(:status => 200, :body => "", :headers => {})
+ expect{subject.healthy?}.to raise_error RuntimeError, "Dependency <https://hardware-store/status> failed check due to RuntimeError: Response expected to be 200 but is 500"
+ reset_lambda_runner_spy
+ expect(subject.healthy?).to be false
+ expect(spy_lambda_was_run?).to eq false
end
end
end
end