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 | |
| parent | 4f171e9e2b1eb119b43f08db23c9e6f271f9021d (diff) | |
add file_server and file_client tests for scheduler/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 | ||||
| -rw-r--r-- | xmake/core/base/scheduler.lua | 9 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/scheduler.lua | 5 |
5 files changed, 95 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 diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 677b50409..5c916bdce 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -121,8 +121,12 @@ function scheduler:co_start_named(coname, cotask, ...) co = _coroutine.new(coname, coroutine.create(function(...) cotask(...) self:co_tasks()[co:thread()] = nil + if self:co_count() > 0 then + self._CO_COUNT = self:co_count() - 1 + end end)) self:co_tasks()[co:thread()] = co + self._CO_COUNT = self:co_count() + 1 local ok, errors = scheduler:co_resume(co, ...) if not ok then return nil, errors @@ -156,6 +160,11 @@ function scheduler:co_tasks() return cotasks end +-- get all coroutine count +function scheduler:co_count() + return self._CO_COUNT or 0 +end + -- wait socket events function scheduler:waitsock(sock, events, timeout) diff --git a/xmake/core/sandbox/modules/import/core/base/scheduler.lua b/xmake/core/sandbox/modules/import/core/base/scheduler.lua index 0a1189459..ed375eb4f 100644 --- a/xmake/core/sandbox/modules/import/core/base/scheduler.lua +++ b/xmake/core/sandbox/modules/import/core/base/scheduler.lua @@ -58,6 +58,11 @@ function sandbox_core_base_scheduler.co_running() return scheduler:co_running() end +-- get the all coroutine task count +function sandbox_core_base_scheduler.co_count() + return scheduler:co_count() +end + -- sleep some times (ms) function sandbox_core_base_scheduler.sleep(ms) local ok, errors = scheduler:sleep(ms) |
