Waiter

A simple wait/polling gem.

Installation

Add this line to your application's Gemfile:

gem 'waiter_'

And then execute:

$ bundle install

Usage

Just include Waiter and the following APIs will be available.

wait.for('foo').to eq 'foo' # Will pass.
wait.for('foo').to eq 'bar' # Will throw exception.
wait.for('foo').to_not eq 'bar' # Will pass.
wait.for('foo').to_not eq 'foo' # Will fail.

To adjust the timeout/polling, simple chain methods are supported.

The up_to method accepts an Integer and adjusts the timeout. Use the every method with an Integer to adjust the polling time.

# Wait for 30 seconds, polling every 2 second.
wait.every(2).up_to(30).for('foo').to eq 'foo'

You do not have to use both methods, you can adjust one or the other. They can also be used in any order.

# Wait for 30 seconds, using the default 1 second poll.
wait.for('foo').up_to(30).to eq 'foo'

# Wait for the default 15 seconds, polling every 5 seconds.
wait.for('foo').every(5).to eq 'foo'

You can also pass a block to be evaluated, using until, until it is true.

wait.until { true == true }

wait.every(5).up_to(30).until {
  true == true
}

A custom failure message can be prepended.

wait.fail_with("A custom message").for('foo').to eq 'bar'

#=> Waiter::TimeoutError:
#     A custom message
#     Timed out after waiting for 15s.
#     Polled every 1s.
#
#     Expected: 'bar'
#     got: 'foo'