From 40eda93398ac3018226705c839848e6f7f1dbd68 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 14:11:34 +0300 Subject: [PATCH] update network test and fix coroutines errors handling --- dev/tests/network.lua | 49 +++++++++++++++++++++++++++++------------- res/scripts/stdlib.lua | 21 ++++++++++-------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/dev/tests/network.lua b/dev/tests/network.lua index 12d0fe93..36f41b62 100644 --- a/dev/tests/network.lua +++ b/dev/tests/network.lua @@ -1,19 +1,38 @@ -local data = "hello, world" -local complete = false +for i=1,3 do + local text = "" + local complete = false -network.tcp_open(7645, function (client) - start_coroutine(function() - local received = client:recv(1024) - if received then - assert (data, utf8.tostring(received)) + for j=1,100 do + text = text .. math.random(0, 9) + end + + local server = network.tcp_open(7645, function (client) + start_coroutine(function() + local received_text = "" + while client:is_alive() do + local received = client:recv(512) + if received then + received_text = received_text .. utf8.tostring(received) + end + coroutine.yield() + end + assert (received_text == text) complete = true - end - coroutine.yield() - end, "client-listener") -end) + end, "client-listener") + end) -network.tcp_connect("localhost", 7645, function (socket) - socket:send(data) -end) + network.tcp_connect("localhost", 7645, function (socket) + start_coroutine(function() + local ptr = 1 + while ptr < #text do + local n = math.random(1, 20) + socket:send(string.sub(text, ptr, ptr + n - 1)) + ptr = ptr + n + end + socket:close() + end, "data-sender") + end) -app.sleep_until(function () return complete end) + app.sleep_until(function () return complete end) + server:close() +end diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 2a0c0173..217723a9 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -531,15 +531,18 @@ function start_coroutine(chunk, name) local co = coroutine.create(function() local status, error = xpcall(chunk, function(err) local fullmsg = "error: "..string.match(err, ": (.+)").."\n"..debug.traceback() - gui.alert(fullmsg, function() - if world.is_open() then - __vc_app.close_world() - else - __vc_app.reset_content() - menu:reset() - menu.page = "main" - end - end) + + if hud then + gui.alert(fullmsg, function() + if world.is_open() then + __vc_app.close_world() + else + __vc_app.reset_content() + menu:reset() + menu.page = "main" + end + end) + end return fullmsg end) if not status then