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
|
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua;
#worker_connections(1014);
#master_on();
#workers(2);
#log_level('warn');
repeat_each(2);
plan tests => repeat_each() * (blocks() * 7 + 2);
#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: simple very
--- config
location /t {
content_by_lua_block {
local begin = ngx.now()
local function f(premature)
print("elapsed: ", ngx.now() - begin)
print("timer prematurely expired: ", premature)
end
local ok, err = ngx.timer.every(0.05, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.say("registered timer")
}
}
--- request
GET /t
--- response_body
registered timer
--- wait: 0.11
--- no_error_log
[error]
[alert]
[crit]
timer prematurely expired: true
--- error_log eval
[
qr/\[lua\] content_by_lua\(nginx\.conf:\d+\):\d+: elapsed: 0\.0(?:4[4-9]|5[0-6])\d*, context: ngx\.timer, client: \d+\.\d+\.\d+\.\d+, server: 0\.0\.0\.0:\d+/,
qr/\[lua\] content_by_lua\(nginx\.conf:\d+\):\d+: elapsed: 0\.(?:09|10)\d*, context: ngx\.timer, client: \d+\.\d+\.\d+\.\d+, server: 0\.0\.0\.0:\d+/,
"lua ngx.timer expired",
"http lua close fake http connection",
"timer prematurely expired: false",
]
=== TEST 2: separated global env
--- config
location /t {
content_by_lua_block {
local begin = ngx.now()
local function f()
foo = 3
print("foo in timer: ", foo)
end
local ok, err = ngx.timer.every(0.05, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.sleep(0.11)
ngx.say("foo = ", foo)
}
}
--- request
GET /t
--- response_body
foo = nil
--- wait: 0.12
--- no_error_log
[error]
[alert]
[crit]
--- error_log eval
[
qr/\[lua\] content_by_lua\(nginx\.conf:\d+\):\d+: foo in timer: 3/,
"lua ngx.timer expired",
"http lua close fake http connection"
]
=== TEST 3: lua variable sharing via upvalue
--- config
location /t {
content_by_lua_block {
local begin = ngx.now()
local foo = 0
local function f()
foo = foo + 3
print("foo in timer: ", foo)
end
local ok, err = ngx.timer.every(0.05, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.say("registered timer")
ngx.sleep(0.11)
ngx.say("foo = ", foo)
}
}
--- request
GET /t
--- response_body
registered timer
foo = 6
--- wait: 0.12
--- no_error_log
[error]
[alert]
[crit]
--- error_log eval
[
qr/\[lua\] content_by_lua\(nginx\.conf:\d+\):\d+: foo in timer: 3/,
qr/\[lua\] content_by_lua\(nginx\.conf:\d+\):\d+: foo in timer: 6/,
"lua ngx.timer expired",
"http lua close fake http connection"
]
=== TEST 4: create the next timer immediately when timer start running
--- config
location /t {
content_by_lua_block {
local begin = ngx.now()
local foo = 0
local function f()
foo = foo + 3
print("foo in timer: ", foo)
ngx.sleep(0.1)
end
local ok, err = ngx.timer.every(0.05, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.say("registered timer")
ngx.sleep(0.11)
ngx.say("foo = ", foo)
}
}
--- request
GET /t
--- response_body
registered timer
foo = 6
--- wait: 0.12
--- no_error_log
[error]
[alert]
[crit]
--- error_log eval
[
qr/\[lua\] content_by_lua\(nginx\.conf:\d+\):\d+: foo in timer: 3/,
qr/\[lua\] content_by_lua\(nginx\.conf:\d+\):\d+: foo in timer: 6/,
"lua ngx.timer expired",
"http lua close fake http connection"
]
=== TEST 5: callback args
--- config
location /t {
content_by_lua_block {
local n = 0
local function f(premature, a, b, c)
n = n + 1
print("the ", n, " time, args: ", a, ", ", b, ", ", c)
a, b, c = 0, 0, 0
end
local ok, err = ngx.timer.every(0.05, f, 1, 2)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
ngx.say("registered timer")
ngx.sleep(0.11)
}
}
--- request
GET /t
--- response_body
registered timer
--- wait: 0.12
--- no_error_log
[error]
[alert]
[crit]
--- error_log eval
[
"the 1 time, args: 1, 2, nil",
"the 2 time, args: 1, 2, nil",
"lua ngx.timer expired",
"http lua close fake http connection"
]
=== TEST 6: memory leak check
--- config
location /t {
content_by_lua_block {
local function f()
local a = 1
-- do nothing
end
for i = 1, 100 do
local ok, err = ngx.timer.every(0.1, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
end
ngx.say("registered timer")
collectgarbage("collect")
local start = collectgarbage("count")
ngx.sleep(0.21)
collectgarbage("collect")
local growth1 = collectgarbage("count") - start
ngx.sleep(0.51)
collectgarbage("collect")
local growth2 = collectgarbage("count") - start
ngx.say("growth1 == growth2: ", growth1 == growth2)
}
}
--- request
GET /t
--- response_body
registered timer
growth1 == growth2: true
--- no_error_log
[error]
[alert]
[crit]
--- timeout: 8
=== TEST 7: respect lua_max_pending_timers
--- http_config
lua_max_pending_timers 10;
--- config
location /t {
content_by_lua_block {
local function f()
local a = 1
-- do nothing
end
for i = 1, 11 do
local ok, err = ngx.timer.every(0.1, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
end
ngx.say("registered 10 timers")
}
}
--- request
GET /t
--- response_body
failed to set timer: too many pending timers
--- no_error_log
[error]
[alert]
[crit]
=== TEST 8: respect lua_max_running_timers
--- http_config
lua_max_pending_timers 100;
lua_max_running_timers 9;
--- config
location /t {
content_by_lua_block {
local function f()
local a = 1
ngx.sleep(0.02)
-- do nothing
end
for i = 1, 10 do
local ok, err = ngx.timer.every(0.01, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
end
ngx.say("registered 10 timers")
ngx.sleep(0.03)
}
}
--- request
GET /t
--- response_body
registered 10 timers
--- no_error_log
[error]
[crit]
--- error_log
lua_max_running_timers are not enough
=== TEST 9: lua_code_cache off
FIXME: it is know that this test case leaks memory.
so we skip it in the "check leak" testing mode.
--- http_config
lua_code_cache off;
--- config
location /t {
content_by_lua_block {
local function f()
local a = 1
-- do nothing
end
local ok, err = ngx.timer.every(0.01, f)
if not ok then
ngx.say("failed to set timer: ", err)
return
end
collectgarbage("collect")
ngx.say("registered timer")
ngx.sleep(0.03)
collectgarbage("collect")
ngx.sleep(0.03)
collectgarbage("collect")
ngx.say("ok")
}
}
--- request
GET /t
--- response_body
registered timer
ok
--- no_error_log
[error]
[crit]
--- no_check_leak
|