Skip to content

Instantly share code, notes, and snippets.

@mipearson
Created September 24, 2013 05:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipearson/6680724 to your computer and use it in GitHub Desktop.
Save mipearson/6680724 to your computer and use it in GitHub Desktop.
def with_retries(timeout = 5.seconds, retry_delay: 0.1.seconds, &blk)
start = Time.now
begin
blk.call
rescue
if Time.now > start + timeout
raise
else
sleep retry_delay
retry
end
end
end
@JonRowe
Copy link

JonRowe commented Sep 24, 2013

I'd use Timeout rather than manually inspecting time, plus yield is a slight performance increase over the block itself.

def with_retries(timeout = 5.seconds, retry_delay: 0.1.seconds)
  Timeout::timeout(timeout) do
    begin
      yield
    rescue RSpec::Expectations::ExpectationNotMetError
      sleep retry_delay
      retry
    end
  rescue Timeout::Error
    yield
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment