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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
# vim:set ft= ts=4 sw=4 et fdm=marker:
our $SkipReason;
BEGIN {
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
$SkipReason = "unavailable for the hup tests";
} else {
$ENV{TEST_NGINX_USE_HUP} = 1;
undef $ENV{TEST_NGINX_USE_STAP};
}
}
use Test::Nginx::Socket::Lua $SkipReason ? (skip_all => $SkipReason) : ();
use t::StapThread;
our $GCScript = $t::StapThread::GCScript;
our $StapScript = $t::StapThread::StapScript;
#worker_connections(1014);
#master_on();
#workers(2);
#log_level('warn');
repeat_each(2);
plan tests => repeat_each() * 81;
#no_diff();
no_long_string();
our $HtmlDir = html_dir;
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
$ENV{TEST_NGINX_HTML_DIR} = $HtmlDir;
worker_connections(1024);
run_tests();
__DATA__
=== TEST 1: single timer
--- config
location /t {
content_by_lua '
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
if not f then
ngx.say("failed to open nginx.pid: ", err)
return
end
local pid = f:read()
-- ngx.say("master pid: [", pid, "]")
f:close()
local i = 0
local function f(premature)
i = i + 1
print("timer prematurely expired: ", premature)
print("in callback: hello, ", i)
end
local ok, err = ngx.timer.at(3, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.say("registered timer")
os.execute("kill -HUP " .. pid)
';
}
--- request
GET /t
--- response_body
registered timer
--- wait: 0.3
--- no_error_log
[error]
[alert]
[crit]
in callback: hello, 2
timer prematurely expired: false
--- error_log
lua abort pending timers
lua ngx.timer expired
http lua close fake http connection
in callback: hello, 1
timer prematurely expired: true
=== TEST 2: multiple timers
--- config
location /t {
content_by_lua '
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
if not f then
ngx.say("failed to open nginx.pid: ", err)
return
end
local pid = f:read()
-- ngx.say("master pid: [", pid, "]")
f:close()
local i = 0
local function f(premature)
i = i + 1
print("timer prematurely expired: ", premature)
print("in callback: hello, ", i, "!")
end
for i = 1, 10 do
local ok, err = ngx.timer.at(3, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
end
ngx.say("registered timers")
os.execute("kill -HUP " .. pid)
';
}
--- request
GET /t
--- response_body
registered timers
--- wait: 0.3
--- no_error_log
[error]
[alert]
[crit]
in callback: hello, 11!
timer prematurely expired: false
--- error_log
lua abort pending timers
lua ngx.timer expired
http lua close fake http connection
in callback: hello, 1!
in callback: hello, 2!
in callback: hello, 3!
in callback: hello, 4!
in callback: hello, 5!
in callback: hello, 6!
in callback: hello, 7!
in callback: hello, 8!
in callback: hello, 9!
in callback: hello, 10!
timer prematurely expired: true
=== TEST 3: trying to add new timer after HUP reload
--- config
location /t {
content_by_lua '
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
if not f then
ngx.say("failed to open nginx.pid: ", err)
return
end
local pid = f:read()
-- ngx.say("master pid: [", pid, "]")
f:close()
local function f(premature)
print("timer prematurely expired: ", premature)
local ok, err = ngx.timer.at(3, f)
if not ok then
print("failed to register a new timer after reload: ", err)
else
print("registered a new timer after reload")
end
end
local ok, err = ngx.timer.at(3, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.say("registered timer")
os.execute("kill -HUP " .. pid)
';
}
--- request
GET /t
--- response_body
registered timer
--- wait: 0.2
--- no_error_log
[error]
[alert]
[crit]
in callback: hello, 2
timer prematurely expired: false
--- error_log
lua abort pending timers
lua ngx.timer expired
http lua close fake http connection
timer prematurely expired: true
failed to register a new timer after reload: process exiting, context: ngx.timer
=== TEST 4: trying to add new timer after HUP reload
--- config
location /t {
content_by_lua '
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
if not f then
ngx.say("failed to open nginx.pid: ", err)
return
end
local pid = f:read()
-- ngx.say("master pid: [", pid, "]")
f:close()
local function g(premature)
print("g: timer prematurely expired: ", premature)
print("g: exiting=", ngx.worker.exiting())
end
local function f(premature)
print("f: timer prematurely expired: ", premature)
print("f: exiting=", ngx.worker.exiting())
local ok, err = ngx.timer.at(0, g)
if not ok then
print("f: failed to register a new timer after reload: ", err)
else
print("f: registered a new timer after reload")
end
end
local ok, err = ngx.timer.at(3, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.say("registered timer")
os.execute("kill -HUP " .. pid)
';
}
--- request
GET /t
--- response_body
registered timer
--- wait: 0.2
--- no_error_log
[error]
[alert]
[crit]
in callback: hello, 2
failed to register a new timer after reload
--- error_log
lua abort pending timers
lua ngx.timer expired
http lua close fake http connection
f: timer prematurely expired: true
f: registered a new timer after reload
f: exiting=true
g: timer prematurely expired: false
g: exiting=true
=== TEST 5: HUP reload should abort pending timers
--- config
location /t {
content_by_lua '
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
if not f then
ngx.say("failed to open nginx.pid: ", err)
return
end
local pid = f:read()
-- ngx.say("master pid: [", pid, "]")
f:close()
local function f(premature)
print("f: timer prematurely expired: ", premature)
print("f: exiting=", ngx.worker.exiting())
end
for i = 1, 100 do
local ok, err = ngx.timer.at(3 + i, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
end
ngx.say("ok")
os.execute("kill -HUP " .. pid)
';
}
--- request
GET /t
--- response_body
ok
--- wait: 0.5
--- no_error_log
[error]
[alert]
[crit]
in callback: hello, 2
failed to register a new timer after reload
--- grep_error_log eval: qr/lua found \d+ pending timers/
--- grep_error_log_out
lua found 100 pending timers
=== TEST 6: HUP reload should abort pending timers (coroutine + cosocket)
--- http_config
lua_shared_dict test_dict 1m;
server {
listen 12355;
location = /foo {
echo 'foo';
}
}
--- config
location /t {
content_by_lua '
local http_req = {"GET /foo HTTP/1.1", "Host: localhost:1234", "", ""}
http_req = table.concat(http_req, "\\r\\n")
-- Connect the socket
local sock = ngx.socket.tcp()
local ok,err = sock:connect("127.0.0.1", 12355)
if not ok then
ngx.log(ngx.ERR, err)
end
-- Send the request
local ok,err = sock:send(http_req)
-- Get Headers
repeat
local line, err = sock:receive("*l")
until not line or string.find(line, "^%s*$")
function foo()
repeat
-- Get and read chunk
local line, err = sock:receive("*l")
local len = tonumber(line)
if len > 0 then
local chunk, err = sock:receive(len)
coroutine.yield(chunk)
sock:receive(2)
else
-- Read last newline
sock:receive(2)
end
until len == 0
end
co = coroutine.create(foo)
repeat
local chunk = select(2,coroutine.resume(co))
until chunk == nil
-- Breaks the timer
sock:setkeepalive()
ngx.say("ok")
';
log_by_lua '
local background_thread
background_thread = function(premature)
ngx.log(ngx.DEBUG, premature)
if premature then
ngx.shared["test_dict"]:delete("background_flag")
return
end
local ok, err = ngx.timer.at(1, background_thread)
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
if not f then
ngx.say("failed to open nginx.pid: ", err)
return
end
local pid = f:read()
-- ngx.say("master pid: [", pid, "]")
f:close()
os.execute("kill -HUP " .. pid)
end
local dict = ngx.shared["test_dict"]
if dict:get("background_flag") == nil then
local ok, err = ngx.timer.at(0, background_thread)
if ok then
dict:set("test_dict", 1)
end
end
';
}
--- request
GET /t
--- response_body
ok
--- wait: 0.3
--- no_error_log
[error]
[alert]
[crit]
in callback: hello, 2
failed to register a new timer after reload
--- grep_error_log eval: qr/lua found \d+ pending timers/
--- grep_error_log_out
lua found 1 pending timers
=== TEST 7: HUP reload should abort pending timers (fuzz test)
--- http_config
lua_max_pending_timers 8192;
--- config
location /t {
content_by_lua '
local job = function(premature, kill)
if premature then
return
end
if kill then
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
if not f then
ngx.log(ngx.ERR, "failed to open nginx.pid: ", err)
return
end
local pid = f:read()
-- ngx.say("master pid: [", pid, "]")
f:close()
os.execute("kill -HUP " .. pid)
end
end
math.randomseed(ngx.time())
local rand = math.random
local newtimer = ngx.timer.at
for i = 1, 8191 do
local delay = rand(4096)
local ok, err = newtimer(delay, job, false)
if not ok then
ngx.say("failed to create timer at ", delay, ": ", err)
return
end
end
local ok, err = newtimer(0, job, true)
if not ok then
ngx.say("failed to create the killer timer: ", err)
return
end
ngx.say("ok")
';
}
--- request
GET /t
--- response_body
ok
--- wait: 0.3
--- no_error_log
[error]
[alert]
--- grep_error_log eval: qr/lua found \d+ pending timers/
--- grep_error_log_out
lua found 8191 pending timers
--- timeout: 20
|