1# vim:set ft= ts=4 sw=4 et fdm=marker: 2 3BEGIN { 4 if (!defined $ENV{LD_PRELOAD}) { 5 $ENV{LD_PRELOAD} = ''; 6 } 7 8 if ($ENV{LD_PRELOAD} !~ /\bmockeagain\.so\b/) { 9 $ENV{LD_PRELOAD} = "mockeagain.so $ENV{LD_PRELOAD}"; 10 } 11 12 if ($ENV{MOCKEAGAIN} eq 'r') { 13 $ENV{MOCKEAGAIN} = 'rw'; 14 15 } else { 16 $ENV{MOCKEAGAIN} = 'w'; 17 } 18 19 $ENV{TEST_NGINX_EVENT_TYPE} = 'poll'; 20 $ENV{MOCKEAGAIN_WRITE_TIMEOUT_PATTERN} = 'get helloworld'; 21} 22 23use Test::Nginx::Socket::Lua; 24 25repeat_each(2); 26 27plan tests => repeat_each() * (blocks() * 4 + 8); 28 29our $HtmlDir = html_dir; 30 31$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; 32$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8'; 33 34no_long_string(); 35no_diff(); 36run_tests(); 37 38__DATA__ 39 40=== TEST 1: lua_socket_connect_timeout only 41--- config 42 server_tokens off; 43 lua_socket_connect_timeout 100ms; 44 resolver $TEST_NGINX_RESOLVER ipv6=off; 45 resolver_timeout 3s; 46 location /t1 { 47 rewrite_by_lua ' 48 local sock = ngx.socket.tcp() 49 local ok, err = sock:connect("127.0.0.2", 12345) 50 if not ok then 51 ngx.say("failed to connect: ", err) 52 return 53 end 54 55 ngx.say("connected: ", ok) 56 '; 57 58 content_by_lua return; 59 } 60--- request 61GET /t1 62--- response_body 63failed to connect: timeout 64--- error_log 65lua tcp socket connect timeout: 100 66lua tcp socket connect timed out, when connecting to 127.0.0.2:12345 67--- timeout: 10 68 69 70 71=== TEST 2: sock:settimeout() overrides lua_socket_connect_timeout 72--- config 73 server_tokens off; 74 lua_socket_connect_timeout 60s; 75 lua_socket_log_errors off; 76 resolver $TEST_NGINX_RESOLVER ipv6=off; 77 resolver_timeout 3s; 78 location /t2 { 79 rewrite_by_lua ' 80 local sock = ngx.socket.tcp() 81 sock:settimeout(150) 82 local ok, err = sock:connect("127.0.0.2", 12345) 83 if not ok then 84 ngx.say("failed to connect: ", err) 85 return 86 end 87 88 ngx.say("connected: ", ok) 89 '; 90 91 content_by_lua return; 92 } 93--- request 94GET /t2 95--- response_body 96failed to connect: timeout 97--- error_log 98lua tcp socket connect timeout: 150 99--- no_error_log 100[error] 101[alert] 102--- timeout: 10 103 104 105 106=== TEST 3: sock:settimeout(nil) does not override lua_socket_connect_timeout 107--- config 108 server_tokens off; 109 lua_socket_log_errors off; 110 lua_socket_connect_timeout 102ms; 111 resolver $TEST_NGINX_RESOLVER ipv6=off; 112 #resolver_timeout 3s; 113 location /t3 { 114 rewrite_by_lua ' 115 local sock = ngx.socket.tcp() 116 sock:settimeout(nil) 117 local ok, err = sock:connect("127.0.0.2", 12345) 118 if not ok then 119 ngx.say("failed to connect: ", err) 120 return 121 end 122 123 ngx.say("connected: ", ok) 124 '; 125 126 content_by_lua return; 127 } 128--- request 129GET /t3 130--- response_body 131failed to connect: timeout 132--- error_log 133lua tcp socket connect timeout: 102 134--- no_error_log 135[error] 136[alert] 137--- timeout: 5 138 139 140 141=== TEST 4: sock:settimeout(0) does not override lua_socket_connect_timeout 142--- config 143 server_tokens off; 144 lua_socket_connect_timeout 102ms; 145 lua_socket_log_errors off; 146 resolver $TEST_NGINX_RESOLVER ipv6=off; 147 resolver_timeout 3s; 148 location /t4 { 149 rewrite_by_lua ' 150 local sock = ngx.socket.tcp() 151 sock:settimeout(0) 152 local ok, err = sock:connect("127.0.0.2", 12345) 153 if not ok then 154 ngx.say("failed to connect: ", err) 155 return 156 end 157 158 ngx.say("connected: ", ok) 159 '; 160 161 content_by_lua return; 162 } 163--- request 164GET /t4 165--- response_body 166failed to connect: timeout 167--- error_log 168lua tcp socket connect timeout: 102 169--- timeout: 5 170--- no_error_log 171[error] 172[alert] 173--- timeout: 10 174 175 176 177=== TEST 5: -1 is bad timeout value 178--- config 179 server_tokens off; 180 lua_socket_connect_timeout 102ms; 181 lua_socket_log_errors off; 182 resolver $TEST_NGINX_RESOLVER ipv6=off; 183 resolver_timeout 3s; 184 location /t5 { 185 rewrite_by_lua ' 186 local sock = ngx.socket.tcp() 187 sock:settimeout(-1) 188 local ok, err = sock:connect("127.0.0.2", 12345) 189 if not ok then 190 ngx.say("failed to connect: ", err) 191 return 192 end 193 194 ngx.say("connected: ", ok) 195 '; 196 197 content_by_lua return; 198 } 199--- request 200GET /t5 201--- response_body_like chomp 202500 Internal Server Error 203--- error_log 204bad timeout value 205--- timeout: 10 206--- error_code: 500 207 208 209 210=== TEST 6: lua_socket_read_timeout only 211--- config 212 server_tokens off; 213 lua_socket_read_timeout 100ms; 214 location /t { 215 rewrite_by_lua ' 216 local sock = ngx.socket.tcp() 217 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 218 if not ok then 219 ngx.say("failed to connect: ", err) 220 return 221 end 222 223 ngx.say("connected: ", ok) 224 225 local line 226 line, err = sock:receive() 227 if line then 228 ngx.say("received: ", line) 229 else 230 ngx.say("failed to receive: ", err) 231 end 232 '; 233 234 content_by_lua return; 235 } 236--- request 237GET /t 238--- response_body 239connected: 1 240failed to receive: timeout 241--- error_log 242lua tcp socket read timeout: 100 243lua tcp socket connect timeout: 60000 244lua tcp socket read timed out 245 246 247 248=== TEST 7: sock:settimeout() overrides lua_socket_read_timeout 249--- config 250 server_tokens off; 251 lua_socket_read_timeout 60s; 252 #resolver $TEST_NGINX_RESOLVER ipv6=off; 253 location /t { 254 rewrite_by_lua ' 255 local sock = ngx.socket.tcp() 256 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 257 if not ok then 258 ngx.say("failed to connect: ", err) 259 return 260 end 261 262 ngx.say("connected: ", ok) 263 264 sock:settimeout(150) 265 266 local line 267 line, err = sock:receive() 268 if line then 269 ngx.say("received: ", line) 270 else 271 ngx.say("failed to receive: ", err) 272 end 273 '; 274 275 content_by_lua return; 276 } 277--- request 278GET /t 279--- response_body 280connected: 1 281failed to receive: timeout 282--- error_log 283lua tcp socket connect timeout: 60000 284lua tcp socket read timeout: 150 285lua tcp socket read timed out 286 287 288 289=== TEST 8: sock:settimeout(nil) does not override lua_socket_read_timeout 290--- config 291 server_tokens off; 292 lua_socket_read_timeout 102ms; 293 #resolver $TEST_NGINX_RESOLVER ipv6=off; 294 location /t { 295 rewrite_by_lua ' 296 local sock = ngx.socket.tcp() 297 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 298 if not ok then 299 ngx.say("failed to connect: ", err) 300 return 301 end 302 303 ngx.say("connected: ", ok) 304 305 sock:settimeout(nil) 306 307 local line 308 line, err = sock:receive() 309 if line then 310 ngx.say("received: ", line) 311 else 312 ngx.say("failed to receive: ", err) 313 end 314 '; 315 316 content_by_lua return; 317 } 318--- request 319GET /t 320--- response_body 321connected: 1 322failed to receive: timeout 323--- error_log 324lua tcp socket connect timeout: 60000 325lua tcp socket read timeout: 102 326lua tcp socket read timed out 327 328 329 330=== TEST 9: sock:settimeout(0) does not override lua_socket_read_timeout 331--- config 332 server_tokens off; 333 lua_socket_read_timeout 102ms; 334 #resolver $TEST_NGINX_RESOLVER ipv6=off; 335 location /t { 336 rewrite_by_lua ' 337 local sock = ngx.socket.tcp() 338 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 339 if not ok then 340 ngx.say("failed to connect: ", err) 341 return 342 end 343 344 ngx.say("connected: ", ok) 345 346 sock:settimeout(0) 347 348 local line 349 line, err = sock:receive() 350 if line then 351 ngx.say("received: ", line) 352 else 353 ngx.say("failed to receive: ", err) 354 end 355 356 '; 357 358 content_by_lua return; 359 } 360--- request 361GET /t 362--- response_body 363connected: 1 364failed to receive: timeout 365--- error_log 366lua tcp socket connect timeout: 60000 367lua tcp socket read timeout: 102 368lua tcp socket read timed out 369 370 371 372=== TEST 10: -1 is bad timeout value 373--- config 374 server_tokens off; 375 lua_socket_read_timeout 102ms; 376 #resolver $TEST_NGINX_RESOLVER ipv6=off; 377 location /t { 378 rewrite_by_lua ' 379 local sock = ngx.socket.tcp() 380 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 381 if not ok then 382 ngx.say("failed to connect: ", err) 383 return 384 end 385 386 sock:settimeout(-1) 387 388 local line 389 line, err = sock:receive() 390 if line then 391 ngx.say("received: ", line) 392 else 393 ngx.say("failed to receive: ", err) 394 end 395 '; 396 397 content_by_lua return; 398 } 399--- request 400GET /t 401--- response_body_like chomp 402500 Internal Server Error 403--- error_log 404bad timeout value 405--- timeout: 10 406--- error_code: 500 407 408 409 410=== TEST 11: lua_socket_send_timeout only 411--- config 412 server_tokens off; 413 lua_socket_send_timeout 100ms; 414 resolver $TEST_NGINX_RESOLVER ipv6=off; 415 location /t { 416 rewrite_by_lua ' 417 local sock = ngx.socket.tcp() 418 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 419 if not ok then 420 ngx.say("failed to connect: ", err) 421 return 422 end 423 424 ngx.say("connected: ", ok) 425 426 local bytes 427 bytes, err = sock:send("get helloworld!") 428 if bytes then 429 ngx.say("sent: ", bytes) 430 else 431 ngx.say("failed to send: ", err) 432 end 433 '; 434 435 content_by_lua return; 436 } 437--- request 438GET /t 439--- response_body 440connected: 1 441failed to send: timeout 442--- error_log 443lua tcp socket send timeout: 100 444lua tcp socket connect timeout: 60000 445lua tcp socket write timed out 446 447 448 449=== TEST 12: sock:settimeout() overrides lua_socket_send_timeout 450--- config 451 server_tokens off; 452 lua_socket_send_timeout 60s; 453 #resolver $TEST_NGINX_RESOLVER ipv6=off; 454 location /t { 455 rewrite_by_lua ' 456 local sock = ngx.socket.tcp() 457 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 458 if not ok then 459 ngx.say("failed to connect: ", err) 460 return 461 end 462 463 ngx.say("connected: ", ok) 464 465 sock:settimeout(150) 466 467 local bytes 468 bytes, err = sock:send("get helloworld!") 469 if bytes then 470 ngx.say("sent: ", bytes) 471 else 472 ngx.say("failed to send: ", err) 473 end 474 '; 475 476 content_by_lua return; 477 } 478--- request 479GET /t 480--- response_body 481connected: 1 482failed to send: timeout 483--- error_log 484lua tcp socket connect timeout: 60000 485lua tcp socket send timeout: 150 486lua tcp socket write timed out 487 488 489 490=== TEST 13: sock:settimeout(nil) does not override lua_socket_send_timeout 491--- config 492 server_tokens off; 493 lua_socket_send_timeout 102ms; 494 #resolver $TEST_NGINX_RESOLVER ipv6=off; 495 location /t { 496 rewrite_by_lua ' 497 local sock = ngx.socket.tcp() 498 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 499 if not ok then 500 ngx.say("failed to connect: ", err) 501 return 502 end 503 504 ngx.say("connected: ", ok) 505 506 sock:settimeout(nil) 507 508 local bytes 509 bytes, err = sock:send("get helloworld!") 510 if bytes then 511 ngx.say("sent: ", bytes) 512 else 513 ngx.say("failed to send: ", err) 514 end 515 '; 516 517 content_by_lua return; 518 } 519--- request 520GET /t 521--- response_body 522connected: 1 523failed to send: timeout 524--- error_log 525lua tcp socket connect timeout: 60000 526lua tcp socket send timeout: 102 527lua tcp socket write timed out 528 529 530 531=== TEST 14: sock:settimeout(0) does not override lua_socket_send_timeout 532--- config 533 server_tokens off; 534 lua_socket_send_timeout 102ms; 535 #resolver $TEST_NGINX_RESOLVER ipv6=off; 536 location /t { 537 rewrite_by_lua ' 538 local sock = ngx.socket.tcp() 539 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 540 if not ok then 541 ngx.say("failed to connect: ", err) 542 return 543 end 544 545 ngx.say("connected: ", ok) 546 547 sock:settimeout(0) 548 549 local bytes 550 bytes, err = sock:send("get helloworld!") 551 if bytes then 552 ngx.say("sent: ", bytes) 553 else 554 ngx.say("failed to send: ", err) 555 end 556 '; 557 558 content_by_lua return; 559 } 560--- request 561GET /t 562--- response_body 563connected: 1 564failed to send: timeout 565--- error_log 566lua tcp socket connect timeout: 60000 567lua tcp socket send timeout: 102 568lua tcp socket write timed out 569 570 571 572=== TEST 15: -1 is bad timeout value 573--- config 574 server_tokens off; 575 lua_socket_send_timeout 102ms; 576 #resolver $TEST_NGINX_RESOLVER ipv6=off; 577 location /t { 578 rewrite_by_lua ' 579 local sock = ngx.socket.tcp() 580 local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) 581 if not ok then 582 ngx.say("failed to connect: ", err) 583 return 584 end 585 586 sock:settimeout(-1) 587 588 local bytes 589 bytes, err = sock:send("get helloworld!") 590 if bytes then 591 ngx.say("sent: ", bytes) 592 else 593 ngx.say("failed to send: ", err) 594 end 595 '; 596 597 content_by_lua return; 598 } 599--- request 600GET /t 601--- response_body_like chomp 602500 Internal Server Error 603--- error_log 604bad timeout value 605--- timeout: 10 606--- error_code: 500 607