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



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

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



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

def self.wait_for!(*_arg)
  wait_for
end

Instance Method Details

#poll_intervalObject



15
16
17
# File 'lib/cistern/wait_for.rb', line 15

def poll_interval
  @poll_interval || Cistern.poll_interval
end

#poll_interval=(poll_interval) ⇒ Object



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

def poll_interval=(poll_interval)
  @poll_interval = poll_interval
end

#timeoutObject



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

def timeout
  @timeout || Cistern.timeout
end

#timeout=(timeout) ⇒ Object



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

def timeout=(timeout)
  @timeout = timeout
end

#timeout_errorObject



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

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

#timeout_error=(timeout_error) ⇒ Object



23
24
25
# File 'lib/cistern/wait_for.rb', line 23

def timeout_error=(timeout_error)
  @timeout_error = timeout_error
end

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



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

def wait_for(timeout = self.timeout, interval = 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 = poll_interval, &block) ⇒ Object



43
44
45
# File 'lib/cistern/wait_for.rb', line 43

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