Class: Vapir::Waiter

Inherits:
Object
  • Object
show all
Defined in:
lib/vapir-common/waiter.rb

Constant Summary collapse

@@default_polling_interval =
0.5
@@default_timeout =
60.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_intervalObject

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

#timeoutObject

Timeout for wait_until.



33
34
35
# File 'lib/vapir-common/waiter.rb', line 33

def timeout
  @timeout
end

#timerObject

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_untilObject

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