From 060626edbf26101044d6ab74f7f1af2fd755dc13 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Dec 2023 22:34:59 +0800 Subject: test process timeout #4544 --- tests/actions/test/src/run_timeout.cpp | 14 ++++++++++++++ tests/actions/test/xmake.lua | 7 +++++++ xmake/actions/test/main.lua | 14 ++++++++++++-- xmake/core/base/os.lua | 8 ++++++-- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/actions/test/src/run_timeout.cpp diff --git a/tests/actions/test/src/run_timeout.cpp b/tests/actions/test/src/run_timeout.cpp new file mode 100644 index 000000000..b4746403f --- /dev/null +++ b/tests/actions/test/src/run_timeout.cpp @@ -0,0 +1,14 @@ +#ifdef _MSC_VER +# include +#else +# include +#endif + +int main(int argc, char** argv) { +#ifdef _MSC_VER + Sleep(10 * 1000); +#else + usleep(10 * 100 * 1000); +#endif + return 0; +} diff --git a/tests/actions/test/xmake.lua b/tests/actions/test/xmake.lua index a4f75036d..67223bc79 100644 --- a/tests/actions/test/xmake.lua +++ b/tests/actions/test/xmake.lua @@ -42,3 +42,10 @@ target("test_15") set_default(false) add_files("src/test_1.cpp") add_tests("stub_n", {files = "tests/stub_n*.cpp", defines = "STUB_N"}) + +target("test_timeout") + set_kind("binary") + set_default(false) + add_files("src/run_timeout.cpp") + add_tests("run_timeout", {run_timeout = 1000}) + diff --git a/xmake/actions/test/main.lua b/xmake/actions/test/main.lua index f61c6e1e0..a61e40966 100644 --- a/xmake/actions/test/main.lua +++ b/xmake/actions/test/main.lua @@ -55,7 +55,9 @@ function _do_test_target(target, opt) local runargs = table.wrap(opt.runargs or target:get("runargs")) local outfile = os.tmpfile() local errfile = os.tmpfile() - local ok, syserrors = os.execv(targetfile, runargs, {try = true, curdir = rundir, envs = envs, stdout = outfile, stderr = errfile}) + local run_timeout = opt.run_timeout + local ok, syserrors = os.execv(targetfile, runargs, {try = true, timeout = run_timeout, + curdir = rundir, envs = envs, stdout = outfile, stderr = errfile}) local outdata = os.isfile(outfile) and io.readfile(outfile) or "" if opt.trim_output then outdata = outdata:trim() @@ -65,7 +67,15 @@ function _do_test_target(target, opt) errors = errdata or errors if not errors or #errors == 0 then if ok ~= nil then - errors = string.format("%s\nrun failed, exit code: %d", outdata or "", ok) + errors = outdata or "" + if #errors > 0 then + errors = errors .. "\n" + end + if syserrors then + errors = errors .. string.format("run failed, exit code: %d, exit error: %s", ok, syserrors) + else + errors = errors .. string.format("run failed, exit code: %d", ok) + end else errors = string.format("run failed, exit error: %s", syserrors and syserrors or "unknown reason") end diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 9bf835456..47cfb98b3 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -860,6 +860,7 @@ function os.execv(program, argv, opt) -- open command local ok = -1 + local errors local proc = process.openv(filename, argv or {}, openopt) if proc ~= nil then @@ -871,9 +872,12 @@ function os.execv(program, argv, opt) -- wait process if not opt.detach then - local waitok, status = proc:wait(-1) + local waitok, status = proc:wait(opt.timeout or -1) if waitok > 0 then ok = status + elseif waitok == 0 and opt.timeout then + proc:kill() + errors = "wait process timeout" end else ok = 0 @@ -885,7 +889,7 @@ function os.execv(program, argv, opt) -- cannot execute process return nil, os.strerror() end - return ok + return ok, errors end -- run command and return output and error data -- cgit v1.3.1 From 22770be939cf1165cb41ca9119d55b07369b7405 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Dec 2023 22:49:49 +0800 Subject: update changelog --- CHANGELOG.md | 8 ++++++++ tests/actions/test/test.lua | 2 +- xmake/actions/test/main.lua | 3 +++ xmake/core/base/os.lua | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 165301c87..2fd0ed353 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### New features + +* [#4544](https://github.com/xmake-io/xmake/issues/4544): Support to wait process timeout for `xmake test` + ## v2.8.6 ### New features @@ -1702,6 +1706,10 @@ ## master (开发中) +### 新特性 + +* [#4544](https://github.com/xmake-io/xmake/issues/4544): 改进 `xmake test`,支持等待进程超时 + ## v2.8.6 ### 新特性 diff --git a/tests/actions/test/test.lua b/tests/actions/test/test.lua index 45ce0b914..3913c3d95 100644 --- a/tests/actions/test/test.lua +++ b/tests/actions/test/test.lua @@ -1,4 +1,4 @@ function main(t) - os.exec("xmake test") + os.exec("xmake test -vD") end diff --git a/xmake/actions/test/main.lua b/xmake/actions/test/main.lua index a61e40966..9313b381c 100644 --- a/xmake/actions/test/main.lua +++ b/xmake/actions/test/main.lua @@ -309,9 +309,12 @@ function _run_tests(tests) cprint("${color.success}%d%%${clear} tests passed, ${color.failure}%d${clear} tests failed out of ${bright}%d${clear}, spent ${bright}%0.3fs", passed_rate, report.total - report.passed, report.total, spent / 1000) local return_zero = project.policy("test.return_zero_on_failure") + print("return_zero", return_zero) if not return_zero and report.passed < report.total then + print("raise") raise() end + print("end") end -- try to build the given target diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 47cfb98b3..aca76db61 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -876,7 +876,11 @@ function os.execv(program, argv, opt) if waitok > 0 then ok = status elseif waitok == 0 and opt.timeout then + print("kill .. ") proc:kill() + print("kill end") + waitok, status = proc:wait(-1) + print("waitok", waitok, status) errors = "wait process timeout" end else -- cgit v1.3.1 From ceb4a08f1638492957db7ffdc97a43b7ec55ce64 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Dec 2023 14:22:16 +0800 Subject: Update test.lua --- tests/actions/test/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/test/test.lua b/tests/actions/test/test.lua index 3913c3d95..45ce0b914 100644 --- a/tests/actions/test/test.lua +++ b/tests/actions/test/test.lua @@ -1,4 +1,4 @@ function main(t) - os.exec("xmake test -vD") + os.exec("xmake test") end -- cgit v1.3.1 From 2a60ad80ea8c367ff111a281252cc202d2938af6 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Dec 2023 14:23:19 +0800 Subject: Update main.lua --- xmake/actions/test/main.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/xmake/actions/test/main.lua b/xmake/actions/test/main.lua index 9313b381c..a61e40966 100644 --- a/xmake/actions/test/main.lua +++ b/xmake/actions/test/main.lua @@ -309,12 +309,9 @@ function _run_tests(tests) cprint("${color.success}%d%%${clear} tests passed, ${color.failure}%d${clear} tests failed out of ${bright}%d${clear}, spent ${bright}%0.3fs", passed_rate, report.total - report.passed, report.total, spent / 1000) local return_zero = project.policy("test.return_zero_on_failure") - print("return_zero", return_zero) if not return_zero and report.passed < report.total then - print("raise") raise() end - print("end") end -- try to build the given target -- cgit v1.3.1 From 945fa0908fbfed1f83020658e6fd9791bbc569b0 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Dec 2023 14:25:49 +0800 Subject: Update os.lua --- xmake/core/base/os.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index aca76db61..0cc3b4361 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -876,11 +876,11 @@ function os.execv(program, argv, opt) if waitok > 0 then ok = status elseif waitok == 0 and opt.timeout then - print("kill .. ") proc:kill() - print("kill end") waitok, status = proc:wait(-1) - print("waitok", waitok, status) + if waitok > 0 then + ok = status + end errors = "wait process timeout" end else -- cgit v1.3.1