Skip to content

Commit

Permalink
ngx-releng fixes
Browse files Browse the repository at this point in the history
udp unit tests added
  • Loading branch information
alonbg committed Aug 1, 2016
1 parent ca6a928 commit 281e2fa
Show file tree
Hide file tree
Showing 7 changed files with 850 additions and 48 deletions.
44 changes: 22 additions & 22 deletions src/ngx_stream_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ enum {
static char ngx_stream_lua_req_socket_metatable_key;
#endif
static char ngx_stream_lua_raw_req_socket_metatable_key;
static char ngx_stream_lua_tcp_socket_metatable_key;
static char ngx_stream_lua_socket_tcp_metatable_key;
static char ngx_stream_lua_upstream_udata_metatable_key;
static char ngx_stream_lua_downstream_udata_metatable_key;
static char ngx_stream_lua_pool_udata_metatable_key;
Expand Down Expand Up @@ -276,7 +276,7 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
/* }}} */

/* {{{tcp object metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_tcp_socket_metatable_key);
lua_pushlightuserdata(L, &ngx_stream_lua_socket_tcp_metatable_key);
lua_createtable(L, 0 /* narr */, 11 /* nrec */);

lua_pushcfunction(L, ngx_stream_lua_socket_tcp_connect);
Expand Down Expand Up @@ -389,7 +389,7 @@ ngx_stream_lua_socket_tcp(lua_State *L)
| NGX_STREAM_LUA_CONTEXT_TIMER);

lua_createtable(L, 3 /* narr */, 1 /* nrec */);
lua_pushlightuserdata(L, &ngx_stream_lua_tcp_socket_metatable_key);
lua_pushlightuserdata(L, &ngx_stream_lua_socket_tcp_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);

Expand Down Expand Up @@ -3914,17 +3914,17 @@ ngx_stream_lua_socket_cleanup_compiled_pattern(lua_State *L)
void
ngx_stream_lua_inject_tcp_req_socket_api(lua_State *L)
{
lua_pushcfunction(L, ngx_stream_lua_tcp_req_socket);
lua_setfield(L, -2, "socket");
lua_pushcfunction(L, ngx_stream_lua_tcp_req_socket);
lua_setfield(L, -2, "socket");
}


static int
ngx_stream_lua_tcp_req_socket(lua_State *L)
{
int n, raw;
ngx_stream_session_t *s;
ngx_peer_connection_t *pc;
int n, raw;
ngx_stream_session_t *s;
ngx_peer_connection_t *pc;
ngx_stream_lua_srv_conf_t *lscf;
ngx_connection_t *c;
ngx_stream_lua_ctx_t *ctx;
Expand All @@ -3934,21 +3934,21 @@ ngx_stream_lua_tcp_req_socket(lua_State *L)
ngx_stream_lua_socket_tcp_upstream_t *u;

n = lua_gettop(L);
if (n == 0) {
raw = 0;
if (n == 0) {
raw = 0;

} else if (n == 1) {
raw = lua_toboolean(L, 1);
lua_pop(L, 1);
} else if (n == 1) {
raw = lua_toboolean(L, 1);
lua_pop(L, 1);

} else {
return luaL_error(L, "expecting zero arguments, but got %d",
lua_gettop(L));
}
} else {
return luaL_error(L, "expecting zero arguments, but got %d",
lua_gettop(L));
}

s = ngx_stream_lua_get_session(L);
s = ngx_stream_lua_get_session(L);

ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}
Expand All @@ -3957,9 +3957,9 @@ ngx_stream_lua_tcp_req_socket(lua_State *L)

c = s->connection;

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

#if !defined(nginx_version) || nginx_version < 1003013
Expand Down
50 changes: 25 additions & 25 deletions src/ngx_stream_lua_socket_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum {
static char ngx_stream_lua_req_socket_metatable_key;
#endif
static char ngx_stream_lua_raw_req_socket_metatable_key;
static char ngx_stream_lua_udp_socket_metatable_key;
static char ngx_stream_lua_socket_udp_metatable_key;
static char ngx_stream_lua_upstream_udata_metatable_key;
static char ngx_stream_lua_downstream_udata_metatable_key;
static u_char ngx_stream_lua_socket_udp_buffer[UDP_MAX_DATAGRAM_SIZE];
Expand All @@ -90,7 +90,7 @@ ngx_stream_lua_inject_socket_udp_api(ngx_log_t *log, lua_State *L)
lua_setfield(L, -2, "udp"); /* ngx socket */

/* udp socket object metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_udp_socket_metatable_key);
lua_pushlightuserdata(L, &ngx_stream_lua_socket_udp_metatable_key);
lua_createtable(L, 0 /* narr */, 6 /* nrec */);

lua_pushcfunction(L, ngx_stream_lua_socket_udp_setpeername);
Expand Down Expand Up @@ -172,39 +172,39 @@ ngx_stream_lua_inject_socket_udp_api(ngx_log_t *log, lua_State *L)
void
ngx_stream_lua_inject_udp_req_socket_api(lua_State *L)
{
lua_pushcfunction(L, ngx_stream_lua_udp_req_socket);
lua_setfield(L, -2, "udp_socket");
lua_pushcfunction(L, ngx_stream_lua_udp_req_socket);
lua_setfield(L, -2, "udp_socket");
}


static int
ngx_stream_lua_udp_req_socket(lua_State *L)
{
int n, raw;
ngx_stream_session_t *s;
int n, raw;
ngx_stream_session_t *s;
ngx_stream_lua_udp_connection_t *pc;
ngx_stream_lua_srv_conf_t *lscf;
ngx_connection_t *c;
ngx_stream_lua_ctx_t *ctx;
ngx_stream_lua_co_ctx_t *coctx;
ngx_stream_lua_cleanup_t *cln;
ngx_stream_lua_srv_conf_t *lscf;
ngx_connection_t *c;
ngx_stream_lua_ctx_t *ctx;
ngx_stream_lua_co_ctx_t *coctx;
ngx_stream_lua_cleanup_t *cln;

ngx_stream_lua_socket_udp_upstream_t *u;

n = lua_gettop(L);
if (n == 0) {
raw = 0;
if (n == 0) {
raw = 0;

} else if (n == 1) {
raw = lua_toboolean(L, 1);
lua_pop(L, 1);
} else if (n == 1) {
raw = lua_toboolean(L, 1);
lua_pop(L, 1);

} else {
return luaL_error(L, "expecting zero arguments, but got %d",
lua_gettop(L));
}
} else {
return luaL_error(L, "expecting zero arguments, but got %d",
lua_gettop(L));
}

s = ngx_stream_lua_get_session(L);
s = ngx_stream_lua_get_session(L);

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

c = s->connection;

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

#if !defined(nginx_version) || nginx_version < 1003013
Expand Down Expand Up @@ -338,7 +338,7 @@ ngx_stream_lua_socket_udp(lua_State *L)
| NGX_STREAM_LUA_CONTEXT_TIMER);

lua_createtable(L, 3 /* narr */, 1 /* nrec */);
lua_pushlightuserdata(L, &ngx_stream_lua_udp_socket_metatable_key);
lua_pushlightuserdata(L, &ngx_stream_lua_socket_udp_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);

Expand Down
2 changes: 1 addition & 1 deletion t/062-count.t
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ngx: 56
ngx.say("n = ", n)
}
--- stream_response
n = 1
n = 2
--- no_error_log
[error]

Expand Down
59 changes: 59 additions & 0 deletions t/137-udp-count.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua::Dgram;

#worker_connections(1014);
#master_on();
#workers(4);
#log_level('warn');
no_root_location();

#repeat_each(2);

plan tests => repeat_each() * (blocks() * 3);

our $HtmlDir = html_dir;

#$ENV{LUA_CPATH} = "/usr/local/openresty/lualib/?.so;" . $ENV{LUA_CPATH};

no_long_string();
run_tests();

__DATA__


=== TEST 1: entries under ngx._udp_meta
--- SKIP
--- dgram_server_config
content_by_lua_block {
local n = 0
for k, v in pairs(ngx._udp_meta) do
n = n + 1
end
ngx.say("n = ", n)
}
--- dgram_response
n = 5
--- no_error_log
[error]



=== TEST 2: entries under the metatable of req sockets
--- dgram_server_config
content_by_lua_block {
local n = 0
local sock, err = ngx.req.udp_socket()
if not sock then
ngx.say("failed to get the request socket: ", err)
end

for k, v in pairs(getmetatable(sock)) do
print("key: ", k)
n = n + 1
end
assert(ngx.say("n = ", n))
}
--- dgram_response
n = 3
--- no_error_log
[error]
Loading

0 comments on commit 281e2fa

Please sign in to comment.