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 with test server.
  • Loading branch information
toy committed Sep 29, 2023
1 parent e65c3cd commit 4464ab4
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions spec/image_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
@server.finish
end

def retry_on(exception_class)
attempt = 1
begin
yield
rescue exception_class => e
warn "Attempt #{attempt}: #{e.inspect}"
if attempt < 3
attempt += 1
retry
else
raise
end
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 +167,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 Net::OpenTimeout 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 Net::OpenTimeout 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 Net::OpenTimeout do
ImageSize.url("#{file_url}?redirect=6")
end
end.to raise_error(/Too many redirects/)
end
end
Expand All @@ -176,22 +197,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 Net::OpenTimeout 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 Net::OpenTimeout 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 Net::OpenTimeout 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 4464ab4

Please sign in to comment.