Skip to content

Commit

Permalink
try 3 times fetching from url during tests
Browse files Browse the repository at this point in the history
I can't find a way to reproduce getting Net::OpenTimeout (Timeout::Error) with test server.
  • Loading branch information
toy committed Sep 29, 2023
1 parent e65c3cd commit 2429295
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions spec/image_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
@server.finish
end

def retry_on(exception_class)
attempt = 1
begin
yield
rescue exception_class => e
warn "Attempt #{attempt}: #{e.inspect}"
raise unless attempt < 3

attempt += 1
retry
end
end

def supported_formats
ImageSize.private_instance_methods.map{ |name| name[/\Asize_of_(.*)\z/, 1] }.compact.sort
end
Expand Down Expand Up @@ -152,22 +165,28 @@ def supported_formats
context 'supporting range' do
context 'without redirects' do
it 'gets format and dimensions' do
image_size = ImageSize.url(file_url)
image_size = retry_on Timeout::Error do
ImageSize.url(file_url)
end
expect(image_size).to have_attributes(attributes)
end
end

context 'with redirects' do
it 'gets format and dimensions' do
image_size = ImageSize.url("#{file_url}?redirect=5")
image_size = retry_on Timeout::Error do
ImageSize.url("#{file_url}?redirect=5")
end
expect(image_size).to have_attributes(attributes)
end
end

context 'with too many redirects' do
it 'gets format and dimensions' do
expect do
ImageSize.url("#{file_url}?redirect=6")
retry_on Timeout::Error do
ImageSize.url("#{file_url}?redirect=6")
end
end.to raise_error(/Too many redirects/)
end
end
Expand All @@ -176,22 +195,28 @@ def supported_formats
context 'not supporting range' do
context 'without redirects' do
it 'gets format and dimensions' do
image_size = ImageSize.url("#{file_url}?ignore_range")
image_size = retry_on Timeout::Error do
ImageSize.url("#{file_url}?ignore_range")
end
expect(image_size).to have_attributes(attributes)
end
end

context 'with redirects' do
it 'gets format and dimensions' do
image_size = ImageSize.url("#{file_url}?ignore_range&redirect=5")
image_size = retry_on Timeout::Error do
ImageSize.url("#{file_url}?ignore_range&redirect=5")
end
expect(image_size).to have_attributes(attributes)
end
end

context 'with too many redirects' do
it 'gets format and dimensions' do
expect do
ImageSize.url("#{file_url}?ignore_range&redirect=6")
retry_on Timeout::Error do
ImageSize.url("#{file_url}?ignore_range&redirect=6")
end
end.to raise_error(/Too many redirects/)
end
end
Expand Down

0 comments on commit 2429295

Please sign in to comment.