Skip to content

Commit f3c2021

Browse files
committed
tests: ported ngx_http_lua test cases for TCP and UDP cosockets sending boolean and nil values.
1 parent 2f061bf commit f3c2021

File tree

2 files changed

+97
-44
lines changed

2 files changed

+97
-44
lines changed

t/058-tcp-socket.t

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua::Stream;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 196;
7+
plan tests => repeat_each() * 199;
88

99
our $HtmlDir = html_dir;
1010

@@ -3148,7 +3148,63 @@ failed to connect: bad port number: 65536
31483148
31493149
31503150
3151-
=== TEST 60: TCP socket GC'ed in preread phase without Lua content phase
3151+
=== TEST 60: send boolean and nil
3152+
--- stream_server_config
3153+
content_by_lua_block {
3154+
local sock = ngx.socket.tcp()
3155+
local port = $TEST_NGINX_SERVER_PORT
3156+
3157+
local ok, err = sock:connect("127.0.0.1", port)
3158+
if not ok then
3159+
ngx.say("failed to connect: ", err)
3160+
return
3161+
end
3162+
3163+
local function send(data)
3164+
local bytes, err = sock:send(data)
3165+
if not bytes then
3166+
ngx.say("failed to send request: ", err)
3167+
return
3168+
end
3169+
end
3170+
3171+
local req = "GET /foo HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\nTest: "
3172+
send(req)
3173+
send(true)
3174+
send(false)
3175+
send(nil)
3176+
send("\r\n\r\n")
3177+
3178+
while true do
3179+
local line, err, part = sock:receive()
3180+
if line then
3181+
ngx.say("received: ", line)
3182+
else
3183+
break
3184+
end
3185+
end
3186+
3187+
ok, err = sock:close()
3188+
}
3189+
--- config
3190+
location /foo {
3191+
server_tokens off;
3192+
more_clear_headers Date;
3193+
echo $http_test;
3194+
}
3195+
--- stream_response
3196+
received: HTTP/1.1 200 OK
3197+
received: Server: nginx
3198+
received: Content-Type: text/plain
3199+
received: Connection: close
3200+
received:
3201+
received: truefalsenil
3202+
--- no_error_log
3203+
[error]
3204+
3205+
3206+
3207+
=== TEST 61: TCP socket GC'ed in preread phase without Lua content phase
31523208
--- stream_server_config
31533209
lua_socket_connect_timeout 1s;
31543210
resolver $TEST_NGINX_RESOLVER ipv6=off;

t/087-udp-socket.t

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,9 @@ qr/runtime error: content_by_lua\(nginx\.conf:\d+\):13: bad request/
792792
else
793793
ngx.say("peer set")
794794
end
795-
796795
local function f()
797796
local sock = test.get_sock()
798-
sock:send("a")
797+
sock:close()
799798
end
800799
ngx.timer.at(0, f)
801800
ngx.sleep(0.001)
@@ -818,73 +817,71 @@ end
818817
peer set
819818

820819
--- error_log eval
821-
qr/runtime error: content_by_lua\(nginx\.conf:\d+\):13: bad request/
820+
qr/runtime error: content_by_lua\(nginx\.conf:\d+\):12: bad request/
822821

823822
--- no_error_log
824823
[alert]
825824

826825

827826

828-
=== TEST 17: bad request tries to receive
827+
=== TEST 17: the upper bound of port range should be 2^16 - 1
829828
--- stream_config eval
830829
"lua_package_path '$::HtmlDir/?.lua;./?.lua;;';"
831830
--- stream_server_config
832831
content_by_lua_block {
833-
local test = require "test"
834-
local sock = test.new_sock()
835-
local ok, err = sock:setpeername("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
832+
local sock = ngx.socket.udp()
833+
local ok, err = sock:setpeername("127.0.0.1", 65536)
836834
if not ok then
837-
ngx.say("failed to set peer: ", err)
838-
else
839-
ngx.say("peer set")
840-
end
841-
function f()
842-
local sock = test.get_sock()
843-
sock:close()
835+
ngx.say("failed to connect: ", err)
844836
end
845-
ngx.timer.at(0, f)
846-
ngx.sleep(0.001)
847837
}
848-
--- user_files
849-
>>> test.lua
850-
module("test", package.seeall)
851-
852-
local sock
853-
854-
function new_sock()
855-
sock = ngx.socket.udp()
856-
return sock
857-
end
858-
859-
function get_sock()
860-
return sock
861-
end
862838
--- stream_response
863-
peer set
864-
865-
--- error_log eval
866-
qr/runtime error: content_by_lua\(nginx\.conf:\d+\):12: bad request/
867-
839+
failed to connect: bad port number: 65536
868840
--- no_error_log
869-
[alert]
841+
[error]
870842

871843

872844

873-
=== TEST 18: the upper bound of port range should be 2^16 - 1
874-
--- stream_config eval
875-
"lua_package_path '$::HtmlDir/?.lua;./?.lua;;';"
845+
=== TEST 18: send boolean and nil
876846
--- stream_server_config
877847
content_by_lua_block {
878-
local sock = ngx.socket.udp()
879-
local ok, err = sock:setpeername("127.0.0.1", 65536)
848+
local socket = ngx.socket
849+
local udp = socket.udp()
850+
local port = ngx.var.port
851+
udp:settimeout(1000) -- 1 sec
852+
853+
local ok, err = udp:setpeername("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
880854
if not ok then
881855
ngx.say("failed to connect: ", err)
856+
return
882857
end
858+
859+
local function send(data)
860+
local bytes, err = udp:send(data)
861+
if not bytes then
862+
ngx.say("failed to send: ", err)
863+
return
864+
end
865+
ngx.say("sent ok")
866+
end
867+
868+
send(true)
869+
send(false)
870+
send(nil)
883871
}
884872
--- stream_response
885-
failed to connect: bad port number: 65536
873+
sent ok
874+
sent ok
875+
sent ok
886876
--- no_error_log
887877
[error]
878+
--- grep_error_log eval
879+
qr/sendto: fd:\d+ \d+ of \d+/
880+
--- grep_error_log_out eval
881+
qr/sendto: fd:\d+ 4 of 4
882+
sendto: fd:\d+ 5 of 5
883+
sendto: fd:\d+ 3 of 3/
884+
--- log_level: debug
888885

889886

890887

0 commit comments

Comments
 (0)