|
40 | 40 | start_time = Time.now |
41 | 41 | get "/?wait_for_status=red" |
42 | 42 | end_time = Time.now |
43 | | - expect(end_time - start_time).to be < 1 |
| 43 | + expect(end_time - start_time).to be < 0.5 |
44 | 44 | end |
45 | 45 | end |
46 | 46 |
|
47 | 47 | context "timeout is provided" do |
48 | 48 |
|
49 | | - let(:timeout) { 1 } |
| 49 | + context "the timeout value is not a valid integer" do |
| 50 | + |
| 51 | + let(:timeout) { "invalid" } |
| 52 | + |
| 53 | + it 'returns immediately' do |
| 54 | + start_time = Time.now |
| 55 | + get "/?wait_for_status=red&timeout=#{timeout}" |
| 56 | + end_time = Time.now |
| 57 | + expect(end_time - start_time).to be < 0.5 |
| 58 | + end |
| 59 | + end |
50 | 60 |
|
51 | 61 | context "the status doesn't change" do |
52 | 62 |
|
53 | | - let(:return_time) { timeout + 0.1 } |
| 63 | + let(:timeout) { 1 } |
54 | 64 |
|
55 | 65 | let(:return_statuses) do |
56 | 66 | [ |
|
66 | 76 | start_time = Time.now |
67 | 77 | get "/?wait_for_status=green&timeout=#{timeout}" |
68 | 78 | end_time = Time.now |
69 | | - expect(end_time - start_time).to be_within(margin_of_error).of(return_time) |
| 79 | + expect(end_time - start_time).to be >= timeout |
70 | 80 | end |
71 | 81 |
|
72 | 82 | it 'returns status code 503' do |
|
75 | 85 | end |
76 | 86 | end |
77 | 87 |
|
78 | | - context 'the status changes within the timeout' do |
| 88 | + context 'the status changes to the target status within the timeout' do |
79 | 89 |
|
80 | | - let(:timeout) { 2 } |
| 90 | + let(:timeout) do |
| 91 | + # The first wait interval is 1 second |
| 92 | + 2 |
| 93 | + end |
81 | 94 |
|
82 | 95 | let(:return_statuses) do |
83 | 96 | [ |
|
99 | 112 | end |
100 | 113 | end |
101 | 114 |
|
102 | | - context "green is provided" do |
| 115 | + context 'status is provided' do |
103 | 116 |
|
104 | | - let(:timeout) do |
105 | | - # Two statuses are checked before the target is reached. The first wait time is 1 second, |
106 | | - # the second wait time is 2 seconds. So it takes at least 3 seconds to reach target status. |
107 | | - 4 |
108 | | - end |
| 117 | + context 'no timeout' do |
109 | 118 |
|
110 | | - let(:return_time) { 3.1 } |
| 119 | + described_class::HEALTH_STATUS.each do |status| |
111 | 120 |
|
112 | | - let(:return_statues) do |
113 | | - [ |
114 | | - org.logstash.health.Status::RED, |
115 | | - org.logstash.health.Status::YELLOW, |
116 | | - org.logstash.health.Status::GREEN, |
117 | | - ] |
118 | | - end |
| 121 | + context "with status \"#{status}\"" do |
119 | 122 |
|
120 | | - before do |
121 | | - allow(@agent.health_observer).to receive(:status).and_return(*return_statues) |
| 123 | + it 'returns immediately' do |
| 124 | + start_time = Time.now |
| 125 | + get "/?wait_for_status=#{status}" |
| 126 | + end_time = Time.now |
| 127 | + expect(end_time - start_time).to be < 0.5 |
| 128 | + end |
| 129 | + end |
| 130 | + end |
122 | 131 | end |
123 | 132 |
|
124 | | - it 'checks for the status until it changes' do |
125 | | - start_time = Time.now |
126 | | - get "/?wait_for_status=green&timeout=#{timeout}" |
127 | | - end_time = Time.now |
128 | | - expect(end_time - start_time).to be_within(margin_of_error).of(return_time) |
| 133 | + context "invalid status" do |
| 134 | + |
| 135 | + it "pending", :skip do |
| 136 | + # start_time = Time.now |
| 137 | + # get "/?wait_for_status=invalid" |
| 138 | + # end_time = Time.now |
| 139 | + # expect(end_time - start_time).to be < 0.5 |
| 140 | + end |
129 | 141 | end |
130 | | - end |
131 | 142 |
|
132 | | - context "yellow is provided" do |
| 143 | + context 'status is formatted differently' do |
133 | 144 |
|
134 | | - let(:timeout) do |
135 | | - # Two statuses are checked before the target is reached. The first wait time is 1 second, |
136 | | - # the second wait time is 2 seconds. So it takes at least 3 seconds to reach target status. |
137 | | - 2 |
138 | | - end |
| 145 | + let(:timeout) { 2 } |
139 | 146 |
|
140 | | - let(:return_statues) do |
141 | | - [ |
142 | | - org.logstash.health.Status::RED, |
143 | | - org.logstash.health.Status::YELLOW |
144 | | - ] |
145 | | - end |
| 147 | + let(:return_statuses) do |
| 148 | + [ |
| 149 | + org.logstash.health.Status::RED, |
| 150 | + org.logstash.health.Status::YELLOW |
| 151 | + ] |
| 152 | + end |
146 | 153 |
|
147 | | - before do |
148 | | - allow(@agent.health_observer).to receive(:status).and_return(*return_statues) |
149 | | - end |
| 154 | + before do |
| 155 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 156 | + end |
150 | 157 |
|
151 | | - it 'checks for the status until it changes' do |
152 | | - start_time = Time.now |
153 | | - get "/?wait_for_status=yellow&timeout=#{timeout}" |
154 | | - end_time = Time.now |
155 | | - expect(end_time - start_time).to be < timeout |
| 158 | + it 'normalizes the format and checks the status' do |
| 159 | + start_time = Time.now |
| 160 | + get "/?wait_for_status=yElLoW&timeout=#{timeout}" |
| 161 | + end_time = Time.now |
| 162 | + expect(end_time - start_time).to be < timeout |
| 163 | + end |
156 | 164 | end |
157 | | - end |
158 | 165 |
|
159 | | - context "red is provided" do |
| 166 | + context 'target status is green' do |
160 | 167 |
|
161 | | - let(:timeout) do |
162 | | - # Two statuses are checked before the target is reached. The first wait time is 1 second, |
163 | | - # the second wait time is 2 seconds. So it takes at least 3 seconds to reach target status. |
164 | | - 2 |
165 | | - end |
| 168 | + let(:timeout) { 2 } |
166 | 169 |
|
167 | | - let(:return_statues) do |
168 | | - [ |
169 | | - org.logstash.health.Status::RED |
170 | | - ] |
171 | | - end |
| 170 | + context 'the status does not change' do |
| 171 | + |
| 172 | + let(:return_statuses) do |
| 173 | + [ |
| 174 | + org.logstash.health.Status::RED, |
| 175 | + org.logstash.health.Status::YELLOW |
| 176 | + ] |
| 177 | + end |
| 178 | + |
| 179 | + before do |
| 180 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 181 | + end |
| 182 | + |
| 183 | + it 'checks for the status until the timeout is reached' do |
| 184 | + start_time = Time.now |
| 185 | + get "/?wait_for_status=green&timeout=#{timeout}" |
| 186 | + end_time = Time.now |
| 187 | + expect(end_time - start_time).to be >= timeout |
| 188 | + end |
| 189 | + end |
172 | 190 |
|
173 | | - let(:return_time) { 0.2 } |
| 191 | + context 'the status changes to green' do |
174 | 192 |
|
175 | | - before do |
176 | | - allow(@agent.health_observer).to receive(:status).and_return(*return_statues) |
177 | | - end |
| 193 | + let(:timeout) { 2 } |
178 | 194 |
|
179 | | - it 'checks for the status until it changes to the target' do |
180 | | - start_time = Time.now |
181 | | - get "/?wait_for_status=red&timeout=#{timeout}" |
182 | | - end_time = Time.now |
183 | | - expect(end_time - start_time).to be_within(margin_of_error).of(return_time) |
| 195 | + let(:return_statuses) do |
| 196 | + [ |
| 197 | + org.logstash.health.Status::RED, |
| 198 | + org.logstash.health.Status::GREEN |
| 199 | + ] |
| 200 | + end |
| 201 | + |
| 202 | + before do |
| 203 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 204 | + end |
| 205 | + |
| 206 | + it 'checks for the status until the timeout is reached' do |
| 207 | + start_time = Time.now |
| 208 | + get "/?wait_for_status=green&timeout=#{timeout}" |
| 209 | + end_time = Time.now |
| 210 | + expect(end_time - start_time).to be < timeout |
| 211 | + end |
| 212 | + end |
184 | 213 | end |
185 | | - end |
186 | 214 |
|
187 | | - context 'status string is formatted differently' do |
| 215 | + context 'target status is yellow' do |
188 | 216 |
|
189 | | - let(:timeout) { 2 } |
| 217 | + let(:timeout) { 2 } |
190 | 218 |
|
191 | | - let(:return_statuses) do |
192 | | - [ |
193 | | - org.logstash.health.Status::RED, |
194 | | - org.logstash.health.Status::YELLOW |
195 | | - ] |
196 | | - end |
| 219 | + context 'the status does not change' do |
197 | 220 |
|
198 | | - before do |
199 | | - allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
200 | | - end |
| 221 | + let(:return_statuses) do |
| 222 | + [ |
| 223 | + org.logstash.health.Status::RED |
| 224 | + ] |
| 225 | + end |
201 | 226 |
|
202 | | - it 'normalizes the format and checks the status' do |
203 | | - start_time = Time.now |
204 | | - get "/?wait_for_status=yElLoW&timeout=#{timeout}" |
205 | | - end_time = Time.now |
206 | | - expect(end_time - start_time).to be < timeout |
| 227 | + before do |
| 228 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 229 | + end |
| 230 | + |
| 231 | + it 'checks for the status until the timeout is reached' do |
| 232 | + start_time = Time.now |
| 233 | + get "/?wait_for_status=yellow&timeout=#{timeout}" |
| 234 | + end_time = Time.now |
| 235 | + expect(end_time - start_time).to be >= timeout |
| 236 | + end |
| 237 | + end |
| 238 | + |
| 239 | + context 'the status changes to yellow' do |
| 240 | + |
| 241 | + let(:timeout) { 2 } |
| 242 | + |
| 243 | + let(:return_statuses) do |
| 244 | + [ |
| 245 | + org.logstash.health.Status::RED, |
| 246 | + org.logstash.health.Status::YELLOW |
| 247 | + ] |
| 248 | + end |
| 249 | + |
| 250 | + before do |
| 251 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 252 | + end |
| 253 | + |
| 254 | + it 'checks for the status until the yellow status is reached' do |
| 255 | + start_time = Time.now |
| 256 | + get "/?wait_for_status=yellow&timeout=#{timeout}" |
| 257 | + end_time = Time.now |
| 258 | + expect(end_time - start_time).to be < timeout |
| 259 | + end |
| 260 | + end |
| 261 | + |
| 262 | + context 'the status changes to green' do |
| 263 | + |
| 264 | + let(:timeout) { 2 } |
| 265 | + |
| 266 | + let(:return_statuses) do |
| 267 | + [ |
| 268 | + org.logstash.health.Status::RED, |
| 269 | + org.logstash.health.Status::GREEN |
| 270 | + ] |
| 271 | + end |
| 272 | + |
| 273 | + before do |
| 274 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 275 | + end |
| 276 | + |
| 277 | + it 'checks for the status until a status that is better (green) is reached' do |
| 278 | + start_time = Time.now |
| 279 | + get "/?wait_for_status=yellow&timeout=#{timeout}" |
| 280 | + end_time = Time.now |
| 281 | + expect(end_time - start_time).to be < timeout |
| 282 | + end |
| 283 | + end |
207 | 284 | end |
208 | | - end |
209 | 285 |
|
210 | | - context "invalid status is provided" do |
| 286 | + context 'target status is red' do |
211 | 287 |
|
212 | | - let(:timeout) { 2 } |
213 | | - let(:return_time) { 0.1 } |
| 288 | + let(:timeout) { 2 } |
214 | 289 |
|
215 | | - it 'returns immediately' do |
216 | | - start_time = Time.now |
217 | | - get "/?wait_for_status=invalid&timeout=#{timeout}" |
218 | | - end_time = Time.now |
219 | | - expect(end_time - start_time).to be_within(margin_of_error).of(return_time) |
| 290 | + context 'the status does not change' do |
| 291 | + |
| 292 | + let(:return_statuses) do |
| 293 | + [ |
| 294 | + org.logstash.health.Status::RED |
| 295 | + ] |
| 296 | + end |
| 297 | + |
| 298 | + before do |
| 299 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 300 | + end |
| 301 | + |
| 302 | + it 'returns immediately' do |
| 303 | + start_time = Time.now |
| 304 | + get "/?wait_for_status=red&timeout=#{timeout}" |
| 305 | + end_time = Time.now |
| 306 | + expect(end_time - start_time).to be < 0.5 |
| 307 | + end |
| 308 | + end |
| 309 | + |
| 310 | + context 'the status changes to yellow' do |
| 311 | + |
| 312 | + let(:timeout) { 2 } |
| 313 | + |
| 314 | + let(:return_statuses) do |
| 315 | + [ |
| 316 | + org.logstash.health.Status::RED, |
| 317 | + org.logstash.health.Status::YELLOW |
| 318 | + ] |
| 319 | + end |
| 320 | + |
| 321 | + before do |
| 322 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 323 | + end |
| 324 | + |
| 325 | + it 'returns immediately' do |
| 326 | + start_time = Time.now |
| 327 | + get "/?wait_for_status=red&timeout=#{timeout}" |
| 328 | + end_time = Time.now |
| 329 | + expect(end_time - start_time).to be < 0.5 |
| 330 | + end |
| 331 | + end |
| 332 | + |
| 333 | + context 'the status changes to green' do |
| 334 | + |
| 335 | + let(:timeout) { 2 } |
| 336 | + |
| 337 | + let(:return_statuses) do |
| 338 | + [ |
| 339 | + org.logstash.health.Status::RED, |
| 340 | + org.logstash.health.Status::GREEN |
| 341 | + ] |
| 342 | + end |
| 343 | + |
| 344 | + before do |
| 345 | + allow(@agent.health_observer).to receive(:status).and_return(*return_statuses) |
| 346 | + end |
| 347 | + |
| 348 | + it 'returns immediately' do |
| 349 | + start_time = Time.now |
| 350 | + get "/?wait_for_status=red&timeout=#{timeout}" |
| 351 | + end_time = Time.now |
| 352 | + expect(end_time - start_time).to be < 0.5 |
| 353 | + end |
| 354 | + end |
220 | 355 | end |
221 | 356 | end |
222 | 357 | end |
|
0 commit comments