Module: Frank::Cucumber::WaitHelper

Included in:
FrankHelper
Defined in:
lib/frank-cucumber/wait_helper.rb

Overview

This module contains a single method called wait_until which implements the / Spin Assert pattern.

When we mix this module into another class or module (such as FrankHelper) then that wait_until method will be available inside that class or module. Because we call module_function at the end of the module this method is also available as a static method on the module. That means you can also call WaitHelper.wait_until from anywhere in your code.

Constant Summary collapse

TIMEOUT =

Default option for how long (in seconds) to keep checking before timing out the entire wait

(ENV['WAIT_TIMEOUT'] || 240).to_i
POLL_SLEEP =

How long to pause (in seconds) inbetween each spin through the assertion block

0.1

Class Method Summary collapse

Class Method Details

.wait_until(opts = {}) { ... } ⇒ Object

Repeatedly evaluate the passed in block until either it returns true or a timeout expires. Between evaluations there is a pause of POLL_SLEEP seconds.

wait_until takes the following options:

:timeout - How long in seconds to keep spinning before timing out of the entire operation. Defaults to TIMEOUT
:message - What to raise in the event of a timeout. Defaults to an empty StandardError

Here’s an example where we will keep calling the reticulate_splines method until either it returns a result greater than 0 or 20 seconds elapses. In the timeout case an exception will be raised saying “timed out waiting for splines to reticulate”:

wait_until( :timeout => 20, :message => 'timed out waiting for splines to reticulate' ) do
  num_splines_reticulated = reticulate_splines(1,2,3)
  num_splines_reticulated > 0
end

Yields:

  • the assertion to wait for

Yield Returns:

  • whether the assertion was met



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/frank-cucumber/wait_helper.rb', line 37

def wait_until(opts = {})
  timeout = opts[:timeout] || TIMEOUT
  message = opts[:message]

  begin
    Timeout::timeout(timeout) do
      until yield
        sleep POLL_SLEEP
      end
    end
  rescue Timeout::Error => e
    raise message if message
    raise
  end
end