Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retry on timeout?

I have a Cucumber Scenario for testing UI features. Sometimes due to one of the several issues, web-page takes lot of time to respond and Capybara times out with following error.

ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill'
ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill'
ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
ruby-1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
ruby-1.9.3-p327/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
ruby-1.9.3-p327/lib/ruby/1.9.1/net/http.rb:2551:in `read_new' 

My question is-

Can I somehow force Cucumber scenario or Capybara to retry (for constant number of times) whole scenario or step respectively, on timeout error?

like image 450
tracker_smurf04 Avatar asked Dec 24 '12 09:12

tracker_smurf04


2 Answers

Maybe, you can do it like this:

Around do |scenario, block|
  for i in 1..5
    begin
      block.call
      break
    rescue Timeout::Error
      next
    end
  end
end

But I can't figure out if this code works because of the bug (It's not possible to call block several times in Around hook)

like image 108
Andrei Botalov Avatar answered Nov 07 '22 03:11

Andrei Botalov


From The Cucumber book:

Add a eventually method that keeps trying to run a block of code until it either stops raising an error or it reaches a time limit.

Here is the code for that method:

module AsyncSupport
  def eventually
    timeout = 2
    polling_interval = 0.1
    time_limit = Time.now + timeout
    loop do
      begin 
        yield
      rescue Exception => error
      end
      return if error.nil?
      raise error if Time.now >= time_limit sleep polling_interval
    end
  end
end
World(AsyncSupport) 

The method called be called as follows from a step_definition:

Then /^the balance of my account should be (#{CAPTURE_CASH_AMOUNT})$/ do |amount|
  eventually { my_account.balance.should eq(amount) }
end
like image 40
Prakash Murthy Avatar answered Nov 07 '22 03:11

Prakash Murthy