PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Bazaar/nginx/ngx_ext_modules/lua-nginx-module/t/065-tcp-socket-timeout.t

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