Correctly break out of http fetches to ensure fast return

This commit is contained in:
Stephen Sykes 2013-03-07 09:26:29 +02:00
parent cb664556ce
commit a25363e5bb
3 changed files with 22 additions and 0 deletions

View File

@ -201,6 +201,8 @@ class FastImage
res.read_body do |str|
break if parse_packet(str)
end
break # needed to actively quit out of the fetch
end
end

1
test/.rbenv-version Normal file
View File

@ -0,0 +1 @@
1.8.7-p357

View File

@ -26,6 +26,12 @@ BadFixtures = [
TestUrl = "http://example.nowhere/"
# this image fetch allows me to really test that fastimage is truly fast
# but it's not ideal relying on external resources and connectivity speed
LargeImage = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
LargeImageInfo = [:jpeg, [9545, 6623]]
LargeImageFetchLimit = 2 # seconds
GoodFixtures.each do |fn, info|
FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :body => File.join(FixturePath, fn))
end
@ -172,4 +178,17 @@ class FastImageTest < Test::Unit::TestCase
resp['Location'] = to
FakeWeb.register_uri(:get, from, :response=>resp)
end
def test_should_fetch_info_of_large_image_faster_than_downloading_the_whole_thing
time = Time.now
size = FastImage.size(LargeImage)
size_time = Time.now
assert size_time - time < LargeImageFetchLimit
assert_equal LargeImageInfo[1], size
time = Time.now
type = FastImage.type(LargeImage)
type_time = Time.now
assert type_time - time < LargeImageFetchLimit
assert_equal LargeImageInfo[0], type
end
end