From ee69d58a59c824a7a0b1146710930c5861d3ca26 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 28 Mar 2018 14:44:42 +0800 Subject: [PATCH] FIX: Tests could get stucked in infinite loop if it fails to resolve IP of a hostname. --- app/jobs/regular/pull_hotlinked_images.rb | 15 +++++++++++---- lib/final_destination.rb | 10 +++++----- spec/models/user_avatar_spec.rb | 3 +++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index 7de892a84e0..0bbef3a4706 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -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 diff --git a/lib/final_destination.rb b/lib/final_destination.rb index e85aff8c01e..3b91788a525 100644 --- a/lib/final_destination.rb +++ b/lib/final_destination.rb @@ -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 diff --git a/spec/models/user_avatar_spec.rb b/spec/models/user_avatar_spec.rb index eb7f723064e..75f31ac8db8 100644 --- a/spec/models/user_avatar_spec.rb +++ b/spec/models/user_avatar_spec.rb @@ -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)