Skip to content

Commit 281e2fa

Browse files
author
alonbg
committed
ngx-releng fixes
udp unit tests added
1 parent ca6a928 commit 281e2fa

7 files changed

+850
-48
lines changed

src/ngx_stream_lua_socket_tcp.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ enum {
193193
static char ngx_stream_lua_req_socket_metatable_key;
194194
#endif
195195
static char ngx_stream_lua_raw_req_socket_metatable_key;
196-
static char ngx_stream_lua_tcp_socket_metatable_key;
196+
static char ngx_stream_lua_socket_tcp_metatable_key;
197197
static char ngx_stream_lua_upstream_udata_metatable_key;
198198
static char ngx_stream_lua_downstream_udata_metatable_key;
199199
static char ngx_stream_lua_pool_udata_metatable_key;
@@ -276,7 +276,7 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
276276
/* }}} */
277277

278278
/* {{{tcp object metatable */
279-
lua_pushlightuserdata(L, &ngx_stream_lua_tcp_socket_metatable_key);
279+
lua_pushlightuserdata(L, &ngx_stream_lua_socket_tcp_metatable_key);
280280
lua_createtable(L, 0 /* narr */, 11 /* nrec */);
281281

282282
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_connect);
@@ -389,7 +389,7 @@ ngx_stream_lua_socket_tcp(lua_State *L)
389389
| NGX_STREAM_LUA_CONTEXT_TIMER);
390390

391391
lua_createtable(L, 3 /* narr */, 1 /* nrec */);
392-
lua_pushlightuserdata(L, &ngx_stream_lua_tcp_socket_metatable_key);
392+
lua_pushlightuserdata(L, &ngx_stream_lua_socket_tcp_metatable_key);
393393
lua_rawget(L, LUA_REGISTRYINDEX);
394394
lua_setmetatable(L, -2);
395395

@@ -3914,17 +3914,17 @@ ngx_stream_lua_socket_cleanup_compiled_pattern(lua_State *L)
39143914
void
39153915
ngx_stream_lua_inject_tcp_req_socket_api(lua_State *L)
39163916
{
3917-
lua_pushcfunction(L, ngx_stream_lua_tcp_req_socket);
3918-
lua_setfield(L, -2, "socket");
3917+
lua_pushcfunction(L, ngx_stream_lua_tcp_req_socket);
3918+
lua_setfield(L, -2, "socket");
39193919
}
39203920

39213921

39223922
static int
39233923
ngx_stream_lua_tcp_req_socket(lua_State *L)
39243924
{
3925-
int n, raw;
3926-
ngx_stream_session_t *s;
3927-
ngx_peer_connection_t *pc;
3925+
int n, raw;
3926+
ngx_stream_session_t *s;
3927+
ngx_peer_connection_t *pc;
39283928
ngx_stream_lua_srv_conf_t *lscf;
39293929
ngx_connection_t *c;
39303930
ngx_stream_lua_ctx_t *ctx;
@@ -3934,21 +3934,21 @@ ngx_stream_lua_tcp_req_socket(lua_State *L)
39343934
ngx_stream_lua_socket_tcp_upstream_t *u;
39353935

39363936
n = lua_gettop(L);
3937-
if (n == 0) {
3938-
raw = 0;
3937+
if (n == 0) {
3938+
raw = 0;
39393939

3940-
} else if (n == 1) {
3941-
raw = lua_toboolean(L, 1);
3942-
lua_pop(L, 1);
3940+
} else if (n == 1) {
3941+
raw = lua_toboolean(L, 1);
3942+
lua_pop(L, 1);
39433943

3944-
} else {
3945-
return luaL_error(L, "expecting zero arguments, but got %d",
3946-
lua_gettop(L));
3947-
}
3944+
} else {
3945+
return luaL_error(L, "expecting zero arguments, but got %d",
3946+
lua_gettop(L));
3947+
}
39483948

3949-
s = ngx_stream_lua_get_session(L);
3949+
s = ngx_stream_lua_get_session(L);
39503950

3951-
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
3951+
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
39523952
if (ctx == NULL) {
39533953
return luaL_error(L, "no ctx found");
39543954
}
@@ -3957,9 +3957,9 @@ ngx_stream_lua_tcp_req_socket(lua_State *L)
39573957

39583958
c = s->connection;
39593959

3960-
if ( SOCK_STREAM != c->type) {
3961-
return luaL_error(L, "socket api does not match connection transport",
3962-
lua_gettop(L));
3960+
if (c->type != SOCK_STREAM) {
3961+
return luaL_error(L, "socket api does not match connection transport",
3962+
lua_gettop(L));
39633963
}
39643964

39653965
#if !defined(nginx_version) || nginx_version < 1003013

src/ngx_stream_lua_socket_udp.c

+25-25
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ enum {
7575
static char ngx_stream_lua_req_socket_metatable_key;
7676
#endif
7777
static char ngx_stream_lua_raw_req_socket_metatable_key;
78-
static char ngx_stream_lua_udp_socket_metatable_key;
78+
static char ngx_stream_lua_socket_udp_metatable_key;
7979
static char ngx_stream_lua_upstream_udata_metatable_key;
8080
static char ngx_stream_lua_downstream_udata_metatable_key;
8181
static u_char ngx_stream_lua_socket_udp_buffer[UDP_MAX_DATAGRAM_SIZE];
@@ -90,7 +90,7 @@ ngx_stream_lua_inject_socket_udp_api(ngx_log_t *log, lua_State *L)
9090
lua_setfield(L, -2, "udp"); /* ngx socket */
9191

9292
/* udp socket object metatable */
93-
lua_pushlightuserdata(L, &ngx_stream_lua_udp_socket_metatable_key);
93+
lua_pushlightuserdata(L, &ngx_stream_lua_socket_udp_metatable_key);
9494
lua_createtable(L, 0 /* narr */, 6 /* nrec */);
9595

9696
lua_pushcfunction(L, ngx_stream_lua_socket_udp_setpeername);
@@ -172,39 +172,39 @@ ngx_stream_lua_inject_socket_udp_api(ngx_log_t *log, lua_State *L)
172172
void
173173
ngx_stream_lua_inject_udp_req_socket_api(lua_State *L)
174174
{
175-
lua_pushcfunction(L, ngx_stream_lua_udp_req_socket);
176-
lua_setfield(L, -2, "udp_socket");
175+
lua_pushcfunction(L, ngx_stream_lua_udp_req_socket);
176+
lua_setfield(L, -2, "udp_socket");
177177
}
178178

179179

180180
static int
181181
ngx_stream_lua_udp_req_socket(lua_State *L)
182182
{
183-
int n, raw;
184-
ngx_stream_session_t *s;
183+
int n, raw;
184+
ngx_stream_session_t *s;
185185
ngx_stream_lua_udp_connection_t *pc;
186-
ngx_stream_lua_srv_conf_t *lscf;
187-
ngx_connection_t *c;
188-
ngx_stream_lua_ctx_t *ctx;
189-
ngx_stream_lua_co_ctx_t *coctx;
190-
ngx_stream_lua_cleanup_t *cln;
186+
ngx_stream_lua_srv_conf_t *lscf;
187+
ngx_connection_t *c;
188+
ngx_stream_lua_ctx_t *ctx;
189+
ngx_stream_lua_co_ctx_t *coctx;
190+
ngx_stream_lua_cleanup_t *cln;
191191

192192
ngx_stream_lua_socket_udp_upstream_t *u;
193193

194194
n = lua_gettop(L);
195-
if (n == 0) {
196-
raw = 0;
195+
if (n == 0) {
196+
raw = 0;
197197

198-
} else if (n == 1) {
199-
raw = lua_toboolean(L, 1);
200-
lua_pop(L, 1);
198+
} else if (n == 1) {
199+
raw = lua_toboolean(L, 1);
200+
lua_pop(L, 1);
201201

202-
} else {
203-
return luaL_error(L, "expecting zero arguments, but got %d",
204-
lua_gettop(L));
205-
}
202+
} else {
203+
return luaL_error(L, "expecting zero arguments, but got %d",
204+
lua_gettop(L));
205+
}
206206

207-
s = ngx_stream_lua_get_session(L);
207+
s = ngx_stream_lua_get_session(L);
208208

209209
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
210210
if (ctx == NULL) {
@@ -215,9 +215,9 @@ ngx_stream_lua_udp_req_socket(lua_State *L)
215215

216216
c = s->connection;
217217

218-
if (SOCK_DGRAM != c->type) {
219-
return luaL_error(L, "socket api does not match connection transport",
220-
lua_gettop(L));
218+
if (c->type != SOCK_DGRAM) {
219+
return luaL_error(L, "socket api does not match connection transport",
220+
lua_gettop(L));
221221
}
222222

223223
#if !defined(nginx_version) || nginx_version < 1003013
@@ -338,7 +338,7 @@ ngx_stream_lua_socket_udp(lua_State *L)
338338
| NGX_STREAM_LUA_CONTEXT_TIMER);
339339

340340
lua_createtable(L, 3 /* narr */, 1 /* nrec */);
341-
lua_pushlightuserdata(L, &ngx_stream_lua_udp_socket_metatable_key);
341+
lua_pushlightuserdata(L, &ngx_stream_lua_socket_udp_metatable_key);
342342
lua_rawget(L, LUA_REGISTRYINDEX);
343343
lua_setmetatable(L, -2);
344344

t/062-count.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ngx: 56
4646
ngx.say("n = ", n)
4747
}
4848
--- stream_response
49-
n = 1
49+
n = 2
5050
--- no_error_log
5151
[error]
5252

t/137-udp-count.t

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
use Test::Nginx::Socket::Lua::Dgram;
3+
4+
#worker_connections(1014);
5+
#master_on();
6+
#workers(4);
7+
#log_level('warn');
8+
no_root_location();
9+
10+
#repeat_each(2);
11+
12+
plan tests => repeat_each() * (blocks() * 3);
13+
14+
our $HtmlDir = html_dir;
15+
16+
#$ENV{LUA_CPATH} = "/usr/local/openresty/lualib/?.so;" . $ENV{LUA_CPATH};
17+
18+
no_long_string();
19+
run_tests();
20+
21+
__DATA__
22+
23+
24+
=== TEST 1: entries under ngx._udp_meta
25+
--- SKIP
26+
--- dgram_server_config
27+
content_by_lua_block {
28+
local n = 0
29+
for k, v in pairs(ngx._udp_meta) do
30+
n = n + 1
31+
end
32+
ngx.say("n = ", n)
33+
}
34+
--- dgram_response
35+
n = 5
36+
--- no_error_log
37+
[error]
38+
39+
40+
41+
=== TEST 2: entries under the metatable of req sockets
42+
--- dgram_server_config
43+
content_by_lua_block {
44+
local n = 0
45+
local sock, err = ngx.req.udp_socket()
46+
if not sock then
47+
ngx.say("failed to get the request socket: ", err)
48+
end
49+
50+
for k, v in pairs(getmetatable(sock)) do
51+
print("key: ", k)
52+
n = n + 1
53+
end
54+
assert(ngx.say("n = ", n))
55+
}
56+
--- dgram_response
57+
n = 3
58+
--- no_error_log
59+
[error]

0 commit comments

Comments
 (0)