-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy path11-clear.t
365 lines (336 loc) · 10.9 KB
/
11-clear.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);
workers(1);
plan tests => repeat_each() * 27 + 2;
my $pwd = cwd();
our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_shared_dict test_shm 8m;
lua_shared_dict my_worker_events 8m;
};
run_tests();
__DATA__
=== TEST 1: clear() clears the list, new checkers never see it
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local config = {
name = "testing",
shm_name = "test_shm",
checks = {
active = {
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
}
local checker1 = healthcheck.new(config)
for i = 1, 10 do
checker1:add_target("127.0.0.1", 10000 + i, nil, false)
end
ngx.sleep(0.2) -- wait twice the interval
checker1:clear()
local checker2 = healthcheck.new(config)
ngx.say(true)
}
}
--- request
GET /t
--- response_body
true
--- error_log
initial target list (0 targets)
--- no_error_log
initial target list (1 targets)
initial target list (2 targets)
initial target list (3 targets)
initial target list (4 targets)
initial target list (5 targets)
initial target list (6 targets)
initial target list (7 targets)
initial target list (8 targets)
initial target list (9 targets)
initial target list (10 targets)
initial target list (11 targets)
=== TEST 2: clear() clears the list, other checkers get notified and clear too
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local config = {
name = "testing",
shm_name = "test_shm",
checks = {
active = {
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
}
local checker1 = healthcheck.new(config)
local checker2 = healthcheck.new(config)
for i = 1, 10 do
checker1:add_target("127.0.0.1", 20000 + i, nil, false)
end
checker2:clear()
ngx.sleep(1)
ngx.say(true)
}
}
--- request
GET /t
--- response_body
true
--- error_log
checking unhealthy targets: nothing to do
--- no_error_log
checking unhealthy targets: #10
=== TEST 3: clear() resets counters
--- http_config eval
qq{
$::HttpConfig
server {
listen 21120;
location = /status {
return 503;
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local config = {
name = "testing",
shm_name = "test_shm",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.2,
},
unhealthy = {
interval = 0.2,
http_failures = 3,
}
}
}
}
local checker1 = healthcheck.new(config)
checker1:add_target("127.0.0.1", 21120, nil, true)
ngx.sleep(0.5) -- wait 2.5x the interval
checker1:clear()
checker1:add_target("127.0.0.1", 21120, nil, true)
ngx.sleep(0.3) -- wait 1.5x the interval
ngx.say(true)
}
}
--- request
GET /t
--- response_body
true
--- error_log
unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:21120)'
unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:21120)'
--- no_error_log
unhealthy HTTP increment (3/3) for '(127.0.0.1:21120)'
=== TEST 4: delayed_clear() clears the list, after interval new checkers don't see it
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local config = {
name = "testing",
shm_name = "test_shm",
checks = {
active = {
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
}
local checker1 = healthcheck.new(config)
for i = 1, 10 do
checker1:add_target("127.0.0.1", 10000 + i, nil, false)
end
ngx.sleep(0.2) -- wait twice the interval
ngx.say(checker1:get_target_status("127.0.0.1", 10001))
checker1:delayed_clear(0.2)
local checker2 = healthcheck.new(config)
ngx.say(checker2:get_target_status("127.0.0.1", 10001))
ngx.sleep(2.6) -- wait while the targets are cleared
local status, err = checker2:get_target_status("127.0.0.1", 10001)
if status ~= nil then
ngx.say(status)
else
ngx.say(err)
end
}
}
--- request
GET /t
--- response_body
false
false
target not found
=== TEST 5: delayed_clear() would clear tgt list, but adding again keeps the previous status
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local config = {
name = "testing",
shm_name = "test_shm",
checks = {
active = {
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
}
local checker1 = healthcheck.new(config)
checker1:add_target("127.0.0.1", 10001, nil, false)
checker1:add_target("127.0.0.1", 10002, nil, false)
checker1:add_target("127.0.0.1", 10003, nil, false)
ngx.sleep(0.2) -- wait twice the interval
ngx.say(checker1:get_target_status("127.0.0.1", 10002))
checker1:delayed_clear(0.2)
local checker2 = healthcheck.new(config)
checker2:add_target("127.0.0.1", 10002, nil, true)
ngx.say(checker2:get_target_status("127.0.0.1", 10002))
ngx.sleep(2.6) -- wait while the targets would be cleared
local status, err = checker2:get_target_status("127.0.0.1", 10001)
if status ~= nil then
ngx.say(status)
else
ngx.say(err)
end
status, err = checker2:get_target_status("127.0.0.1", 10002)
if status ~= nil then
ngx.say(status)
else
ngx.say(err)
end
status, err = checker2:get_target_status("127.0.0.1", 10003)
if status ~= nil then
ngx.say(status)
else
ngx.say(err)
end
}
}
--- request
GET /t
--- response_body
false
false
target not found
false
target not found
=== TEST 6: delayed_clear() would clear tgt list when we add two checkers
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local config1 = {
name = "testing",
shm_name = "test_shm",
checks = {
active = {
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
}
local config2 = {
name = "testing2",
shm_name = "test_shm",
checks = {
active = {
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
}
local checker1 = healthcheck.new(config1)
checker1:add_target("127.0.0.1", 10001, nil, true)
checker1:add_target("127.0.0.1", 10002, nil, true)
checker1:add_target("127.0.0.1", 10003, nil, true)
ngx.say(checker1:get_target_status("127.0.0.1", 10002))
checker1:delayed_clear(0.2)
local checker2 = healthcheck.new(config2)
checker2:add_target("127.0.0.1", 10001, nil, true)
checker2:add_target("127.0.0.1", 10002, nil, true)
ngx.say(checker2:get_target_status("127.0.0.1", 10002))
checker2:delayed_clear(0.2)
ngx.sleep(3) -- wait twice the interval
local status, err = checker1:get_target_status("127.0.0.1", 10001)
if status ~= nil then
ngx.say(status)
else
ngx.say(err)
end
status, err = checker2:get_target_status("127.0.0.1", 10002)
if status ~= nil then
ngx.say(status)
else
ngx.say(err)
end
status, err = checker2:get_target_status("127.0.0.1", 10003)
if status ~= nil then
ngx.say(status)
else
ngx.say(err)
end
}
}
--- request
GET /t
--- response_body
true
true
target not found
target not found
target not found