From 2b55e5eb2a2d532120f3dc8f6ec3595947cb1163 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Thu, 15 Aug 2019 17:57:37 -0700 Subject: [PATCH 1/3] tweaks: ported adjustments and missing test cases from the ngx_stream_lua module. --- src/ngx_http_lua_util.c | 4 +-- t/058-tcp-socket.t | 58 ++++++++++++++++++++++++++++++++++------- t/091-coroutine.t | 8 +++--- t/161-load-resty-core.t | 29 ++++++++++++++++----- 4 files changed, 77 insertions(+), 22 deletions(-) diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c index 5cd5bac5d8..b3817fddd7 100644 --- a/src/ngx_http_lua_util.c +++ b/src/ngx_http_lua_util.c @@ -1352,7 +1352,7 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r, if (!ctx->cur_co_ctx->is_wrap) { /* - * ended successful, coroutine.resume returns true plus + * ended successfully, coroutine.resume returns true plus * any return values */ lua_pushboolean(next_co, success); @@ -1520,7 +1520,7 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r, if (orig_coctx->is_wrap) { /* * coroutine.wrap propagates errors - * to the parent + * to its parent coroutine */ next_coctx->propagate_error = 1; continue; diff --git a/t/058-tcp-socket.t b/t/058-tcp-socket.t index 837006c638..6ac67e0849 100644 --- a/t/058-tcp-socket.t +++ b/t/058-tcp-socket.t @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua; repeat_each(2); -plan tests => repeat_each() * 219; +plan tests => repeat_each() * 222; our $HtmlDir = html_dir; @@ -3643,7 +3643,45 @@ lua http cleanup reuse -=== TEST 60: options_table is nil +=== TEST 60: setkeepalive on socket already shutdown +--- config + location /t { + set $port $TEST_NGINX_MEMCACHED_PORT; + + content_by_lua_block { + local sock = ngx.socket.tcp() + local port = ngx.var.port + + local ok, err = sock:connect("127.0.0.1", port) + if not ok then + ngx.say("failed to connect: ", err) + return + end + + ngx.say("connected: ", ok) + + local ok, err = sock:close() + if not ok then + ngx.log(ngx.ERR, "failed to close socket: ", err) + return + end + + local ok, err = sock:setkeepalive() + if not ok then + ngx.log(ngx.ERR, "failed to setkeepalive: ", err) + end + } + } +--- request +GET /t +--- response_body +connected: 1 +--- error_log +failed to setkeepalive: closed + + + +=== TEST 61: options_table is nil --- config location /t { set $port $TEST_NGINX_MEMCACHED_PORT; @@ -3693,7 +3731,7 @@ close: 1 nil -=== TEST 61: resolver send query failing immediately in connect() +=== TEST 62: resolver send query failing immediately in connect() this case did not clear coctx->cleanup properly and would lead to memory invalid accesses. this test case requires the following iptables rule to work properly: @@ -3730,7 +3768,7 @@ qr{\[alert\] .*? send\(\) failed \(\d+: Operation not permitted\) while resolvin -=== TEST 62: the upper bound of port range should be 2^16 - 1 +=== TEST 63: the upper bound of port range should be 2^16 - 1 --- config location /t { content_by_lua_block { @@ -3749,7 +3787,7 @@ failed to connect: bad port number: 65536 -=== TEST 63: send boolean and nil +=== TEST 64: send boolean and nil --- config location /t { set $port $TEST_NGINX_SERVER_PORT; @@ -3811,7 +3849,7 @@ received: truefalsenil -=== TEST 64: receiveany method in cosocket +=== TEST 65: receiveany method in cosocket --- config server_tokens off; location = /t { @@ -3900,7 +3938,7 @@ lua tcp socket read any -=== TEST 65: receiveany send data after read side closed +=== TEST 66: receiveany send data after read side closed --- config server_tokens off; location = /t { @@ -3944,7 +3982,7 @@ GET /t -=== TEST 66: receiveany with limited, max <= 0 +=== TEST 67: receiveany with limited, max <= 0 --- config location = /t { set $port $TEST_NGINX_SERVER_PORT; @@ -3980,7 +4018,7 @@ GET /t -=== TEST 67: receiveany with limited, max is larger than data +=== TEST 68: receiveany with limited, max is larger than data --- config server_tokens off; location = /t { @@ -4049,7 +4087,7 @@ lua tcp socket calling receiveany() method to read at most 128 bytes -=== TEST 68: receiveany with limited, max is smaller than data +=== TEST 69: receiveany with limited, max is smaller than data --- config server_tokens off; location = /t { diff --git a/t/091-coroutine.t b/t/091-coroutine.t index 5af1ea3a19..b9b3d9d38d 100644 --- a/t/091-coroutine.t +++ b/t/091-coroutine.t @@ -1533,7 +1533,7 @@ co == co2: false --- config location = /t { content_by_lua_block { - function f() + local function f() local cnt = 0 for i = 1, 20 do ngx.say("co yield: ", cnt) @@ -1567,7 +1567,7 @@ co yield: 2 --- config location = /t { content_by_lua_block { - function f() + local function f() local cnt = 0 for i = 1, 20 do coroutine.yield(cnt, cnt + 1) @@ -1601,7 +1601,7 @@ co yield: 2, 3 --- config location = /t { content_by_lua_block { - function f(step) + local function f(step) local cnt = 0 for i = 1, 20 do ngx.say("co yield: ", cnt) @@ -1637,7 +1637,7 @@ co yield: 2 return 200; header_filter_by_lua_block { - function f() + local function f() local cnt = 0 for i = 1, 20 do print("co yield: ", cnt) diff --git a/t/161-load-resty-core.t b/t/161-load-resty-core.t index 444e3c8ee0..e6a4d0471a 100644 --- a/t/161-load-resty-core.t +++ b/t/161-load-resty-core.t @@ -38,7 +38,24 @@ resty.core loaded: true -=== TEST 2: resty.core is automatically loaded in the Lua VM with 'lua_code_cache off' +=== TEST 2: resty.core is automatically loaded in the Lua VM when 'lua_shared_dict' is used +--- http_config + lua_shared_dict dogs 128k; +--- config + location = /t { + content_by_lua_block { + local loaded_resty_core = package.loaded["resty.core"] + local resty_core = require "resty.core" + + ngx.say("resty.core loaded: ", loaded_resty_core == resty_core) + } + } +--- response_body +resty.core loaded: true + + + +=== TEST 3: resty.core is automatically loaded in the Lua VM with 'lua_code_cache off' --- http_config lua_code_cache off; --- config @@ -55,7 +72,7 @@ resty.core loaded: true -=== TEST 3: resty.core loading honors the lua_package_path directive +=== TEST 4: resty.core loading honors the lua_package_path directive --- http_config eval "lua_package_path '$::HtmlDir/?.lua;;';" --- config @@ -82,7 +99,7 @@ return { -=== TEST 4: resty.core not loading aborts the initialization +=== TEST 5: resty.core not loading aborts the initialization --- http_config eval "lua_package_path '$::HtmlDir/?.lua;';" --- config @@ -95,7 +112,7 @@ qr/\[alert\] .*? failed to load the 'resty\.core' module .*? \(reason: module 'r -=== TEST 5: resty.core not loading produces an error with 'lua_code_cache off' +=== TEST 6: resty.core not loading produces an error with 'lua_code_cache off' --- http_config lua_code_cache off; @@ -116,7 +133,7 @@ qr/\[alert\] .*? failed to load the 'resty\.core' module/ -=== TEST 6: lua_load_resty_core logs a deprecation warning when specified (on) +=== TEST 7: lua_load_resty_core logs a deprecation warning when specified (on) --- http_config lua_load_resty_core on; --- config @@ -132,7 +149,7 @@ qr/\[warn\] .*? lua_load_resty_core is deprecated \(the lua-resty-core library i -=== TEST 7: lua_load_resty_core logs a deprecation warning when specified (off) +=== TEST 8: lua_load_resty_core logs a deprecation warning when specified (off) --- http_config lua_load_resty_core off; --- config From d5e0dfc8091444a26905fa337017a5ee2bdf616b Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Thu, 22 Aug 2019 15:55:52 -0700 Subject: [PATCH 2/3] tests: 087-udp-socket.t: removed a duplicated test case. --- t/087-udp-socket.t | 59 +--------------------------------------------- 1 file changed, 1 insertion(+), 58 deletions(-) diff --git a/t/087-udp-socket.t b/t/087-udp-socket.t index dc6cad5ef8..f6ef26067d 100644 --- a/t/087-udp-socket.t +++ b/t/087-udp-socket.t @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua; repeat_each(2); -plan tests => repeat_each() * (3 * blocks() + 14); +plan tests => repeat_each() * (3 * blocks() + 13); our $HtmlDir = html_dir; @@ -1006,63 +1006,6 @@ qr/runtime error: content_by_lua\(nginx\.conf:\d+\):14: bad request/ #set $port 5000; set $port $TEST_NGINX_MEMCACHED_PORT; - content_by_lua ' - local test = require "test" - if ngx.var.arg_reset then - local sock = test.new_sock() - local ok, err = sock:setpeername("127.0.0.1", ngx.var.port) - if not ok then - ngx.say("failed to set peer: ", err) - else - ngx.say("peer set") - end - return - end - local sock = test.get_sock() - sock:send("a") - '; - } ---- user_files ->>> test.lua -module("test", package.seeall) - -local sock - -function new_sock() - sock = ngx.socket.udp() - return sock -end - -function get_sock() - return sock -end ---- request -GET /main ---- response_body_like eval -qr/^peer set - Date: Thu, 15 Aug 2019 17:55:09 -0700 Subject: [PATCH 3/3] doc: readme: minor updates to align with the ngx_stream_lua module README. --- README.markdown | 19 ++++++++++--------- doc/HttpLuaModule.wiki | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/README.markdown b/README.markdown index f085848da2..0d4e0dd8eb 100644 --- a/README.markdown +++ b/README.markdown @@ -319,10 +319,10 @@ patches](https://github.com/openresty/openresty/tree/master/patches). Alternatively, ngx_lua can be manually compiled into Nginx: -1. LuaJIT can be downloaded from the [latest release of OpenResty's LuaJIT fork](https://github.com/openresty/luajit2/releases). The official LuaJIT 2.x releases are also supported, although performance will be significantly lower for reasons elaborated above.. -1. Download the latest version of the ngx_devel_kit (NDK) module [HERE](https://github.com/simplresty/ngx_devel_kit/tags). -1. Download the latest version of ngx_lua [HERE](https://github.com/openresty/lua-nginx-module/tags). -1. Download the latest version of Nginx [HERE](https://nginx.org/) (See [Nginx Compatibility](#nginx-compatibility)) +1. LuaJIT can be downloaded from the [latest release of OpenResty's LuaJIT fork](https://github.com/openresty/luajit2/releases). The official LuaJIT 2.x releases are also supported, although performance will be significantly lower for reasons elaborated above +1. Download the latest version of the ngx_devel_kit (NDK) module [HERE](https://github.com/simplresty/ngx_devel_kit/tags) +1. Download the latest version of ngx_lua [HERE](https://github.com/openresty/lua-nginx-module/tags) +1. Download the latest supported version of Nginx [HERE](https://nginx.org/) (See [Nginx Compatibility](#nginx-compatibility)) Build the source with this module: @@ -413,7 +413,8 @@ The [openresty](https://groups.google.com/group/openresty) mailing list is for C Code Repository =============== -The code repository of this project is hosted on github at [openresty/lua-nginx-module](https://github.com/openresty/lua-nginx-module). +The code repository of this project is hosted on GitHub at +[openresty/lua-nginx-module](https://github.com/openresty/lua-nginx-module). [Back to TOC](#table-of-contents) @@ -7625,7 +7626,7 @@ tcpsock:settimeout Set the timeout value in milliseconds for subsequent socket operations ([connect](#tcpsockconnect), [receive](#tcpsockreceive), and iterators returned from [receiveuntil](#tcpsockreceiveuntil)). -Settings done by this method takes priority over those config directives, i.e., [lua_socket_connect_timeout](#lua_socket_connect_timeout), [lua_socket_send_timeout](#lua_socket_send_timeout), and [lua_socket_read_timeout](#lua_socket_read_timeout). +Settings done by this method take priority over those specified via config directives (i.e. [lua_socket_connect_timeout](#lua_socket_connect_timeout), [lua_socket_send_timeout](#lua_socket_send_timeout), and [lua_socket_read_timeout](#lua_socket_read_timeout)). Note that this method does *not* affect the [lua_socket_keepalive_timeout](#lua_socket_keepalive_timeout) setting; the `timeout` argument to the [setkeepalive](#tcpsocksetkeepalive) method should be used for this purpose instead. @@ -7640,12 +7641,12 @@ tcpsock:settimeouts **context:** *rewrite_by_lua*, access_by_lua*, content_by_lua*, ngx.timer.*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua** -Sets the connect timeout thresold, send timeout threshold, and read timeout threshold, respetively, in milliseconds, for subsequent socket +Respectively sets the connect, send, and read timeout thresholds (in milliseconds) for subsequent socket operations ([connect](#tcpsockconnect), [send](#tcpsocksend), [receive](#tcpsockreceive), and iterators returned from [receiveuntil](#tcpsockreceiveuntil)). -Settings done by this method takes priority over those config directives, i.e., [lua_socket_connect_timeout](#lua_socket_connect_timeout), [lua_socket_send_timeout](#lua_socket_send_timeout), and [lua_socket_read_timeout](#lua_socket_read_timeout). +Settings done by this method take priority over those specified via config directives (i.e. [lua_socket_connect_timeout](#lua_socket_connect_timeout), [lua_socket_send_timeout](#lua_socket_send_timeout), and [lua_socket_read_timeout](#lua_socket_read_timeout)). -You are recommended to use [settimeouts](#tcpsocksettimeouts) instead of [settimeout](#tcpsocksettimeout). +It is recommended to use [settimeouts](#tcpsocksettimeouts) instead of [settimeout](#tcpsocksettimeout). Note that this method does *not* affect the [lua_socket_keepalive_timeout](#lua_socket_keepalive_timeout) setting; the `timeout` argument to the [setkeepalive](#tcpsocksetkeepalive) method should be used for this purpose instead. diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index e158c1cc27..762a7c9b6a 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -254,10 +254,10 @@ patches]. Alternatively, ngx_lua can be manually compiled into Nginx: -# LuaJIT can be downloaded from the [https://github.com/openresty/luajit2/releases latest release of OpenResty's LuaJIT fork]. The official LuaJIT 2.x releases are also supported, although performance will be significantly lower for reasons elaborated above.. -# Download the latest version of the ngx_devel_kit (NDK) module [https://github.com/simplresty/ngx_devel_kit/tags HERE]. -# Download the latest version of ngx_lua [https://github.com/openresty/lua-nginx-module/tags HERE]. -# Download the latest version of Nginx [https://nginx.org/ HERE] (See [[#Nginx Compatibility|Nginx Compatibility]]) +# LuaJIT can be downloaded from the [https://github.com/openresty/luajit2/releases latest release of OpenResty's LuaJIT fork]. The official LuaJIT 2.x releases are also supported, although performance will be significantly lower for reasons elaborated above +# Download the latest version of the ngx_devel_kit (NDK) module [https://github.com/simplresty/ngx_devel_kit/tags HERE] +# Download the latest version of ngx_lua [https://github.com/openresty/lua-nginx-module/tags HERE] +# Download the latest supported version of Nginx [https://nginx.org/ HERE] (See [[#Nginx Compatibility|Nginx Compatibility]]) Build the source with this module: @@ -328,7 +328,8 @@ The [https://groups.google.com/group/openresty openresty] mailing list is for Ch = Code Repository = -The code repository of this project is hosted on github at [https://github.com/openresty/lua-nginx-module openresty/lua-nginx-module]. +The code repository of this project is hosted on GitHub at +[https://github.com/openresty/lua-nginx-module openresty/lua-nginx-module]. = Bugs and Patches = @@ -6481,7 +6482,7 @@ This feature was first introduced in the v0.5.0rc1 release. Set the timeout value in milliseconds for subsequent socket operations ([[#tcpsock:connect|connect]], [[#tcpsock:receive|receive]], and iterators returned from [[#tcpsock:receiveuntil|receiveuntil]]). -Settings done by this method takes priority over those config directives, i.e., [[#lua_socket_connect_timeout|lua_socket_connect_timeout]], [[#lua_socket_send_timeout|lua_socket_send_timeout]], and [[#lua_socket_read_timeout|lua_socket_read_timeout]]. +Settings done by this method take priority over those specified via config directives (i.e. [[#lua_socket_connect_timeout|lua_socket_connect_timeout]], [[#lua_socket_send_timeout|lua_socket_send_timeout]], and [[#lua_socket_read_timeout|lua_socket_read_timeout]]). Note that this method does ''not'' affect the [[#lua_socket_keepalive_timeout|lua_socket_keepalive_timeout]] setting; the timeout argument to the [[#tcpsock:setkeepalive|setkeepalive]] method should be used for this purpose instead. @@ -6493,12 +6494,12 @@ This feature was first introduced in the v0.5.0rc1 release. '''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*, ngx.timer.*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*'' -Sets the connect timeout thresold, send timeout threshold, and read timeout threshold, respetively, in milliseconds, for subsequent socket +Respectively sets the connect, send, and read timeout thresholds (in milliseconds) for subsequent socket operations ([[#tcpsock:connect|connect]], [[#tcpsock:send|send]], [[#tcpsock:receive|receive]], and iterators returned from [[#tcpsock:receiveuntil|receiveuntil]]). -Settings done by this method takes priority over those config directives, i.e., [[#lua_socket_connect_timeout|lua_socket_connect_timeout]], [[#lua_socket_send_timeout|lua_socket_send_timeout]], and [[#lua_socket_read_timeout|lua_socket_read_timeout]]. +Settings done by this method take priority over those specified via config directives (i.e. [[#lua_socket_connect_timeout|lua_socket_connect_timeout]], [[#lua_socket_send_timeout|lua_socket_send_timeout]], and [[#lua_socket_read_timeout|lua_socket_read_timeout]]). -You are recommended to use [[#tcpsock:settimeouts|settimeouts]] instead of [[#tcpsock:settimeout|settimeout]]. +It is recommended to use [[#tcpsock:settimeouts|settimeouts]] instead of [[#tcpsock:settimeout|settimeout]]. Note that this method does ''not'' affect the [[#lua_socket_keepalive_timeout|lua_socket_keepalive_timeout]] setting; the timeout argument to the [[#tcpsock:setkeepalive|setkeepalive]] method should be used for this purpose instead.