From 24292959fffe8c3b11ea73a1ed4021a97a50759d Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Fri, 29 Sep 2023 16:50:11 +0200 Subject: [PATCH] try 3 times fetching from url during tests I can't find a way to reproduce getting Net::OpenTimeout (Timeout::Error) with test server. --- spec/image_size_spec.rb | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/spec/image_size_spec.rb b/spec/image_size_spec.rb index d872023..3847ac7 100644 --- a/spec/image_size_spec.rb +++ b/spec/image_size_spec.rb @@ -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 @@ -152,14 +165,18 @@ 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 @@ -167,7 +184,9 @@ def supported_formats 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 @@ -176,14 +195,18 @@ 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 @@ -191,7 +214,9 @@ def supported_formats 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