Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
eventually helper method for making assertions against asynchronous systems
# usage:
# it "should return a result of 5" do
# eventually { long_running_thing.result.should eq(5) }
# end
module AsyncHelper
def eventually(:options = {})
timeout = options[:timeout] || 2
interval = options[:interval] || 0.1
time_limit = + timeout
loop do
rescue => error
return if error.nil?
raise error if >= time_limit
sleep interval
# Can we put this into RSpec somewhere?

This comment has been minimized.

Copy link

aslakhellesoy commented Sep 20, 2011

Wouldn't this peg the CPU and possibly starve the thing you're waiting for?


This comment has been minimized.

Copy link
Owner Author

mattwynne commented Sep 20, 2011

Good point. Needs to take a nap in the loop. Updated.


This comment has been minimized.

Copy link

mitchlloyd commented Oct 4, 2013

@mattwynne Looks like there is a syntax error on line 6 with the colon.


This comment has been minimized.

Copy link

xam7247 commented Aug 10, 2015

Stumbled across this, and realised that the gist no more works since rspec expectation failure now subclasses from Exception rather than StandardError (rspec/rspec-expectations@eafa55d#diff-f14d34de26325e61579baa36128c97cb)

Here's a fork that rescues both StandardError and RSpec::Expectations::ExpectationNotMetError:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.