Module: Cistern::WaitFor

Included in:
Cistern
Defined in:
lib/cistern/timeout.rb,
lib/cistern/wait_for.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

3 minutes

3 * 60
DEFAULT_POLL_INTERVAL =

seconds

10

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wait_for(timeout = Cistern.timeout, interval = Cistern.poll_interval, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cistern/timeout.rb', line 3

def self.wait_for(timeout = Cistern.timeout, interval = Cistern.poll_interval, &block)
  duration = 0
  start    = Time.now

  until yield || duration > timeout
    sleep(interval.to_f)
    duration = Time.now - start
  end

  if duration > timeout
    false
  else
    { :duration => duration }
  end
end

.wait_for!(*arg) ⇒ Object



19
20
21
# File 'lib/cistern/timeout.rb', line 19

def self.wait_for!(*arg)
  wait_for
end

Instance Method Details

#poll_intervalObject



10
# File 'lib/cistern/wait_for.rb', line 10

def poll_interval; @poll_interval || DEFAULT_POLL_INTERVAL; end

#poll_interval=(poll_interval) ⇒ Object



11
# File 'lib/cistern/wait_for.rb', line 11

def poll_interval=(poll_interval); @poll_interval = poll_interval; end

#timeoutObject



8
# File 'lib/cistern/wait_for.rb', line 8

def timeout; @timeout || DEFAULT_TIMEOUT; end

#timeout=(timeout) ⇒ Object



9
# File 'lib/cistern/wait_for.rb', line 9

def timeout=(timeout); @timeout = timeout; end

#timeout_errorObject



13
# File 'lib/cistern/wait_for.rb', line 13

def timeout_error; @timeout_error || self.const_defined?(:Timeout) && self.const_get(:Timeout) || ::Timeout::Error; end

#timeout_error=(timeout_error) ⇒ Object



12
# File 'lib/cistern/wait_for.rb', line 12

def timeout_error=(timeout_error); @timeout_error = timeout_error; end

#wait_for(timeout = self.timeout, interval = self.poll_interval, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cistern/wait_for.rb', line 15

def wait_for(timeout = self.timeout, interval = self.poll_interval, &block)
  duration = 0
  start    = Time.now

  until yield || duration > timeout
    sleep(interval.to_f)
    duration = Time.now - start
  end

  duration > timeout ? false : duration
end

#wait_for!(timeout = self.timeout, interval = self.poll_interval, &block) ⇒ Object



27
28
29
# File 'lib/cistern/wait_for.rb', line 27

def wait_for!(timeout = self.timeout, interval = self.poll_interval, &block)
  wait_for(timeout, interval, &block) || raise(timeout_error, "wait_for(#{timeout}) exceeded")
end