FIX: Tests could get stucked in infinite loop if it fails to resolve IP of a hostname.

This commit is contained in:
Guo Xiang Tan 2018-03-28 14:44:42 +08:00
parent 03725c7c8a
commit ee69d58a59
3 changed files with 19 additions and 9 deletions

View File

@ -62,7 +62,9 @@ module Jobs
if is_valid_image_url(src)
begin
# have we already downloaded that file?
unless downloaded_images.include?(src) || large_images.include?(src) || broken_images.include?(src)
schemeless_src = remove_scheme(original_src)
unless downloaded_images.include?(schemeless_src) || large_images.include?(schemeless_src) || broken_images.include?(schemeless_src)
if hotlinked = download(src)
if File.size(hotlinked.path) <= @max_size
filename = File.basename(URI.parse(src).path)
@ -70,17 +72,17 @@ module Jobs
upload = UploadCreator.new(hotlinked, filename, origin: src).create_for(post.user_id)
if upload.persisted?
downloaded_urls[src] = upload.url
downloaded_images[src.sub(/^https?:/i, "")] = upload.id
downloaded_images[remove_scheme(src)] = upload.id
has_downloaded_image = true
else
log(:info, "Failed to pull hotlinked image for post: #{post_id}: #{src} - #{upload.errors.full_messages.join("\n")}")
end
else
large_images << original_src.sub(/^https?:/i, "")
large_images << remove_scheme(src)
has_new_large_image = true
end
else
broken_images << original_src.sub(/^https?:/i, "")
broken_images << remove_scheme(src)
has_new_broken_image = true
end
end
@ -170,6 +172,11 @@ module Jobs
)
end
private
def remove_scheme(src)
src.sub(/^https?:/i, "")
end
end
end

View File

@ -312,11 +312,11 @@ class FinalDestination
end
def self.lookup_ip(host)
# TODO clean this up in the test suite, cause it is a mess
# if Rails.env == "test"
# STDERR.puts "WARNING FinalDestination.lookup_ip was called with host: #{host}, this is network call that should be mocked"
# end
IPSocket::getaddress(host)
if Rails.env.test?
"0.0.0.0"
else
IPSocket::getaddress(host)
end
rescue SocketError
nil
end

View File

@ -46,6 +46,9 @@ describe UserAvatar do
describe 'when avatar url returns an invalid status code' do
it 'should not do anything' do
stub_request(:get, "http://thisfakesomething.something.com/")
.to_return(status: 500, body: "", headers: {})
url = "http://thisfakesomething.something.com/"
UserAvatar.import_url_for_user(url, user)