Skip to content

Commit 71ecbbd

Browse files
committed
Update tests
1 parent 4741401 commit 71ecbbd

File tree

1 file changed

+230
-95
lines changed

1 file changed

+230
-95
lines changed

logstash-core/spec/logstash/api/modules/root_spec.rb

Lines changed: 230 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,27 @@
4040
start_time = Time.now
4141
get "/?wait_for_status=red"
4242
end_time = Time.now
43-
expect(end_time - start_time).to be < 1
43+
expect(end_time - start_time).to be < 0.5
4444
end
4545
end
4646

4747
context "timeout is provided" do
4848

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
5060

5161
context "the status doesn't change" do
5262

53-
let(:return_time) { timeout + 0.1 }
63+
let(:timeout) { 1 }
5464

5565
let(:return_statuses) do
5666
[
@@ -66,7 +76,7 @@
6676
start_time = Time.now
6777
get "/?wait_for_status=green&timeout=#{timeout}"
6878
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
7080
end
7181

7282
it 'returns status code 503' do
@@ -75,9 +85,12 @@
7585
end
7686
end
7787

78-
context 'the status changes within the timeout' do
88+
context 'the status changes to the target status within the timeout' do
7989

80-
let(:timeout) { 2 }
90+
let(:timeout) do
91+
# The first wait interval is 1 second
92+
2
93+
end
8194

8295
let(:return_statuses) do
8396
[
@@ -99,124 +112,246 @@
99112
end
100113
end
101114

102-
context "green is provided" do
115+
context 'status is provided' do
103116

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
109118

110-
let(:return_time) { 3.1 }
119+
described_class::HEALTH_STATUS.each do |status|
111120

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
119122

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
122131
end
123132

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
129141
end
130-
end
131142

132-
context "yellow is provided" do
143+
context 'status is formatted differently' do
133144

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 }
139146

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
146153

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
150157

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
156164
end
157-
end
158165

159-
context "red is provided" do
166+
context 'target status is green' do
160167

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 }
166169

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
172190

173-
let(:return_time) { 0.2 }
191+
context 'the status changes to green' do
174192

175-
before do
176-
allow(@agent.health_observer).to receive(:status).and_return(*return_statues)
177-
end
193+
let(:timeout) { 2 }
178194

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
184213
end
185-
end
186214

187-
context 'status string is formatted differently' do
215+
context 'target status is yellow' do
188216

189-
let(:timeout) { 2 }
217+
let(:timeout) { 2 }
190218

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
197220

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
201226

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
207284
end
208-
end
209285

210-
context "invalid status is provided" do
286+
context 'target status is red' do
211287

212-
let(:timeout) { 2 }
213-
let(:return_time) { 0.1 }
288+
let(:timeout) { 2 }
214289

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
220355
end
221356
end
222357
end

0 commit comments

Comments
 (0)