Module: Cistern::WaitFor

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

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



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

def poll_interval
  @poll_interval || Cistern.poll_interval
end

#poll_interval=(poll_interval) ⇒ Object



17
18
19
# File 'lib/cistern/wait_for.rb', line 17

def poll_interval=(poll_interval)
  @poll_interval = poll_interval
end

#timeoutObject



5
6
7
# File 'lib/cistern/wait_for.rb', line 5

def timeout
  @timeout || Cistern.timeout
end

#timeout=(timeout) ⇒ Object



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

def timeout=(timeout)
  @timeout = timeout
end

#timeout_errorObject



25
26
27
# File 'lib/cistern/wait_for.rb', line 25

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

#timeout_error=(timeout_error) ⇒ Object



21
22
23
# File 'lib/cistern/wait_for.rb', line 21

def timeout_error=(timeout_error)
  @timeout_error = timeout_error
end

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



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cistern/wait_for.rb', line 29

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



41
42
43
# File 'lib/cistern/wait_for.rb', line 41

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