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

View File

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

View File

@ -46,6 +46,9 @@ describe UserAvatar do
describe 'when avatar url returns an invalid status code' do describe 'when avatar url returns an invalid status code' do
it 'should not do anything' 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/" url = "http://thisfakesomething.something.com/"
UserAvatar.import_url_for_user(url, user) UserAvatar.import_url_for_user(url, user)