Class: Waiter::ChainableWait

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

Constant Summary collapse

DEFAULT_TIMEOUT =
15
DEFAULT_POLLING =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ChainableWait

Returns a new instance of ChainableWait.



60
61
62
63
64
# File 'lib/waiter.rb', line 60

def initialize(opts = {})
  @timeout = opts[:timeout] || DEFAULT_TIMEOUT
  @polling = opts[:polling] || DEFAULT_POLLING
  @failure_message = opts[:failure_message]
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



55
56
57
# File 'lib/waiter.rb', line 55

def message
  @message
end

#pollingObject

Returns the value of attribute polling.



55
56
57
# File 'lib/waiter.rb', line 55

def polling
  @polling
end

#targetObject (readonly)

Returns the value of attribute target.



54
55
56
# File 'lib/waiter.rb', line 54

def target
  @target
end

#timeoutObject

Returns the value of attribute timeout.



55
56
57
# File 'lib/waiter.rb', line 55

def timeout
  @timeout
end

Instance Method Details

#every(seconds) ⇒ Object Also known as: polling_every



81
82
83
84
# File 'lib/waiter.rb', line 81

def every(seconds)
  @polling = seconds
  self
end

#fail_with(message) ⇒ Object



87
88
89
90
# File 'lib/waiter.rb', line 87

def fail_with(message)
  @failure_message = message
  self
end

#for(value = nil, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/waiter.rb', line 66

def for(value = nil, &block)
  if value
    @target = value
  elsif block_given?
    @target = block
  end

  self
end

#not_to(matcher = nil, &block) ⇒ Object Also known as: to_not



96
97
98
# File 'lib/waiter.rb', line 96

def not_to(matcher = nil, &block)
  WaitExpectationTarget.new(@target).not_to(self, matcher, &block)
end

#to(matcher = nil, &block) ⇒ Object



92
93
94
# File 'lib/waiter.rb', line 92

def to(matcher = nil, &block)
  WaitExpectationTarget.new(@target).to(self, matcher, &block)
end

#until(&block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/waiter.rb', line 101

def until(&block)
  unless @timeout > @polling
    raise ArgumentError, 'Timeout must be a higher value than polling.'
  end

  current_timeout = @timeout

  while current_timeout > 0 && current_timeout > @polling
    trap('SIGINT') { break }

    @result = begin
      block.call
    rescue SystemExit, Interrupt
      break
    rescue Exception => error
      @error = error
      false
    end

    break if @result
    sleep @polling
    current_timeout = current_timeout - @polling
  end

  unless @result
    if @error
      @error = Waiter::TimeoutError.new(build_error(@error))
    else
      @error = Waiter::TimeoutError.new(build_error)
    end

    raise @error
  end
end

#up_to(seconds) ⇒ Object



76
77
78
79
# File 'lib/waiter.rb', line 76

def up_to(seconds)
  @timeout = seconds
  self
end