public
Last active

eventually helper method for making assertions against asynchronous systems

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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?

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

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.