Skip to content

@mattwynne /gist:1228927
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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 = Time.now + timeout
loop do
begin
yield
rescue => error
end
return if error.nil?
raise error if Time.now >= time_limit
sleep interval
end
end
end
# Can we put this into RSpec somewhere?
@aslakhellesoy

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

@mattwynne
Owner

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

@mitchlloyd

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

@xam7247

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:
https://gist.github.com/xam7247/290868f3c6f0f72e9b01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.