diff options
| author | ruki <[email protected]> | 2019-12-14 00:46:15 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-13 22:37:35 +0800 |
| commit | 26ea5a3e4a0a474366e879b529d6f0a2c918ef2d (patch) | |
| tree | bb1964bc50281bf09f83c1eab44827fe01756a65 /tests/modules/socket | |
| parent | 4f171e9e2b1eb119b43f08db23c9e6f271f9021d (diff) | |
add file_server and file_client tests for scheduler/socket
Diffstat (limited to 'tests/modules/socket')
| -rw-r--r-- | tests/modules/socket/sched_tcp/echo_client.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/sched_tcp/file_client.lua | 45 | ||||
| -rw-r--r-- | tests/modules/socket/sched_tcp/file_server.lua | 33 |
3 files changed, 81 insertions, 1 deletions
diff --git a/tests/modules/socket/sched_tcp/echo_client.lua b/tests/modules/socket/sched_tcp/echo_client.lua index 77b76a0f3..af40a085d 100644 --- a/tests/modules/socket/sched_tcp/echo_client.lua +++ b/tests/modules/socket/sched_tcp/echo_client.lua @@ -17,7 +17,9 @@ function _session(addr, port) end print("%s: send ok, count: %d!", sock, count) sock:close() - scheduler.stop() + if scheduler.co_count() == 1 then + scheduler.stop() + end end function main(count) diff --git a/tests/modules/socket/sched_tcp/file_client.lua b/tests/modules/socket/sched_tcp/file_client.lua new file mode 100644 index 000000000..5dda4be6d --- /dev/null +++ b/tests/modules/socket/sched_tcp/file_client.lua @@ -0,0 +1,45 @@ +import("core.base.socket") +import("core.base.scheduler") + +function _session(addr, port) + print("connect %s:%d ..", addr, port) + local sock = socket.connect(addr, port) + print("%s: connected!", sock) + local real = 0 + local recv = 0 + local data = nil + local wait = false + local results = {} + while true do + real, data = sock:recv(8192) + if real > 0 then + recv = recv + real + wait = false + table.insert(results, data) + elseif real == 0 and not wait then + if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then + wait = true + else + break + end + else + break + end + end + if #results > 0 then + data = bytes(results) + end + print("%s: recv ok, size: %d, #data: %d!", sock, recv, data and data:size() or 0) + sock:close() + if scheduler.co_count() == 1 then + scheduler.stop() + end +end + +function main(count) + count = count and tonumber(count) or 1 + for i = 1, count do + scheduler.co_start(_session, "127.0.0.1", 9090) + end + scheduler.runloop() +end diff --git a/tests/modules/socket/sched_tcp/file_server.lua b/tests/modules/socket/sched_tcp/file_server.lua new file mode 100644 index 000000000..904b3db8e --- /dev/null +++ b/tests/modules/socket/sched_tcp/file_server.lua @@ -0,0 +1,33 @@ +import("core.base.socket") +import("core.base.scheduler") + +function _session(sock, filepath) + + local file = io.open(filepath, 'rb') + if file then + local send = sock:sendfile(file, {block = true}) + print("%s: send %s %d bytes!", sock, filepath, send) + file:close() + end + sock:close() +end + +function _listen(addr, port, filepath) + + local sock = socket.bind(addr, port) + sock:listen(20) + print("%s: listening %s:%d ..", sock, addr, port) + while true do + local sock_client = sock:accept() + if sock_client then + print("%s: accepted", sock_client) + scheduler.co_start(_session, sock_client, filepath) + end + end + sock:close() +end + +function main(filepath) + scheduler.co_start(_listen, "127.0.0.1", 9090, filepath) + scheduler.runloop() +end |
