Class: Vapir::Waiter
- Inherits:
-
Object
- Object
- Vapir::Waiter
- Defined in:
- lib/vapir-common/waiter.rb
Constant Summary collapse
- @@default_polling_interval =
0.5
- @@default_timeout =
60.0
Instance Attribute Summary collapse
-
#polling_interval ⇒ Object
How long to wait between each iteration through the wait_until loop.
-
#timeout ⇒ Object
Timeout for wait_until.
-
#timer ⇒ Object
This is an interface to a TimeKeeper which proxies calls to “sleep” and “Time.now”.
Class Method Summary collapse
-
.wait_until(timeout = @@default_timeout, polling_interval = @@default_polling_interval) ⇒ Object
IDEA: wait_until: remove defaults from Waiter.wait_until.
Instance Method Summary collapse
-
#initialize(timeout = @@default_timeout, polling_interval = @@default_polling_interval) ⇒ Waiter
constructor
A new instance of Waiter.
-
#wait_until ⇒ Object
Execute the provided block until either (1) it returns true, or (2) the timeout (in seconds) has been reached.
Constructor Details
#initialize(timeout = @@default_timeout, polling_interval = @@default_polling_interval) ⇒ Waiter
Returns a new instance of Waiter.
38 39 40 41 42 43 |
# File 'lib/vapir-common/waiter.rb', line 38 def initialize(timeout=@@default_timeout, polling_interval=@@default_polling_interval) @timeout = timeout @polling_interval = polling_interval @timer = TimeKeeper.new end |
Instance Attribute Details
#polling_interval ⇒ Object
How long to wait between each iteration through the wait_until loop. In seconds.
30 31 32 |
# File 'lib/vapir-common/waiter.rb', line 30 def polling_interval @polling_interval end |
#timeout ⇒ Object
Timeout for wait_until.
33 34 35 |
# File 'lib/vapir-common/waiter.rb', line 33 def timeout @timeout end |
#timer ⇒ Object
This is an interface to a TimeKeeper which proxies calls to “sleep” and “Time.now”. Useful for unit testing Waiter.
26 27 28 |
# File 'lib/vapir-common/waiter.rb', line 26 def timer @timer end |
Class Method Details
.wait_until(timeout = @@default_timeout, polling_interval = @@default_polling_interval) ⇒ Object
IDEA: wait_until: remove defaults from Waiter.wait_until
77 78 79 80 81 |
# File 'lib/vapir-common/waiter.rb', line 77 def self.wait_until(timeout=@@default_timeout, polling_interval=@@default_polling_interval) waiter = new(timeout, polling_interval) waiter.wait_until { yield } end |
Instance Method Details
#wait_until ⇒ Object
Execute the provided block until either (1) it returns true, or (2) the timeout (in seconds) has been reached. If the timeout is reached, a TimeOutException will be raised. The block will always execute at least once.
waiter = Waiter.new(5) waiter.wait_until ‘hello’
This code will print out “hello” for five seconds, and then raise a Vapir::TimeOutException.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/vapir-common/waiter.rb', line 55 def wait_until # block start_time = now until yield do if (duration = now - start_time) > @timeout raise Vapir::Exception::TimeOutException.new(duration, @timeout), "Timed out after #{duration} seconds." end sleep @polling_interval end end |