Ensure that the DAO::Jenkins calls properly ignore network errors but not others

Fixes #1
This commit is contained in:
R. Tyler Croy 2017-09-04 09:35:23 -07:00
parent f8ecd9e629
commit e0a67300fe
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
3 changed files with 39 additions and 4 deletions

View File

@ -1,14 +1,14 @@
require 'concurrent/hash'
require 'mini_cache'
require 'net/http'
module CodeValet
module Canary
# The DAO module contains some data-access objects which are to be used
# from the web tier.
module DAO
# Unless otherwise specified, cached entries should "live" for this
# number of seconds
DEFAULT_CACHE_SECONDS = 120
NET_ERRORS = [Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError]
# Access the caching object
#

View File

@ -1,5 +1,6 @@
require 'faraday'
require 'canary/dao'
require 'raven'
module CodeValet::Canary::DAO
class Jenkins
@ -34,15 +35,32 @@ module CodeValet::Canary::DAO
# NOTE: worth investigating whether Jenkins will provide the appropriate
# caching headers to
url = "#{URL_BASE}/u/#{user}/userContent/builtOn.txt"
response = Faraday.get(url)
response = connection.get(url)
if response.success?
return response.body
end
@error = response.status
return nil
rescue *CodeValet::Canary::DAO::NET_ERRORS => e
@error = e
return nil
rescue StandardError => e
@error = e
Raven.capture_exception(e)
return nil
end
private
def connection
return Faraday.new(:ssl => {:verify => true}) do |f|
f.adapter Faraday.default_adapter
f.options.timeout = 4
f.options.open_timeout = 3
end
end
def cache
return CodeValet::Canary::DAO.cache
end

View File

@ -1,5 +1,6 @@
require 'spec_helper'
require 'json'
require 'canary/dao/jenkins'
describe CodeValet::Canary::DAO::Jenkins do
@ -39,5 +40,21 @@ describe CodeValet::Canary::DAO::Jenkins do
end
context 'with network errors' do
it 'should silently handle ECONNRESET' do
expect(subject).to receive(:connection).and_raise(Errno::ECONNRESET)
expect(subject.rebuiltGA).to be_nil
expect(subject).to be_errored
end
end
context 'with unknown errors' do
it 'should record an exception and return nil' do
expect(subject).to receive(:connection).and_raise(JSON::ParserError)
expect(Raven).to receive(:capture_exception)
expect(subject.rebuiltGA).to be_nil
expect(subject).to be_errored
end
end
end