diff options
| author | ruki <[email protected]> | 2025-11-09 08:28:01 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-09 08:28:01 +0800 |
| commit | a1b0ec856e2c22aac84022feb05b6687912e3d6e (patch) | |
| tree | a4679af3be46069a480be76bdc322eeeefcc4049 /tests | |
| parent | 0b91f564b00bf22d611a70c17088ba3a155b8a84 (diff) | |
| parent | 91ef5fa9376814db12d35a020008a0d692883945 (diff) | |
Merge pull request #6989 from xmake-io/async
Add async support in os APIs
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modules/os/async_copy.lua | 34 | ||||
| -rw-r--r-- | tests/modules/os/async_scheduler.lua | 32 | ||||
| -rw-r--r-- | tests/modules/os/test.lua | 22 | ||||
| -rw-r--r-- | tests/runner.lua | 11 |
4 files changed, 95 insertions, 4 deletions
diff --git a/tests/modules/os/async_copy.lua b/tests/modules/os/async_copy.lua new file mode 100644 index 000000000..751a940ae --- /dev/null +++ b/tests/modules/os/async_copy.lua @@ -0,0 +1,34 @@ +local function _prepare_source(dir) + os.mkdir(dir) + io.writefile(path.join(dir, "foo.txt"), "foo") + io.writefile(path.join(dir, "bar.txt"), "bar") +end + +function main() + local root = os.tmpfile() .. ".os_async_copy" + local srcdir = path.join(root, "src") + local dstdir = path.join(root, "dst") + + _prepare_source(srcdir) + print("source prepared: %s", srcdir) + + local files = os.files(path.join(srcdir, "*.txt"), {async = true}) + assert(files and #files == 2) + print("async enumerate: %d files", #files) + + os.cp(srcdir, dstdir, {async = true}) + assert(os.isdir(dstdir)) + print("async copy done: %s", dstdir) + + files = os.files(path.join(dstdir, "*.txt"), {async = true}) + assert(files and #files == 2) + + os.rm(dstdir, {async = true}) + assert(not os.isdir(dstdir)) + print("dst removed async") + + os.rm(srcdir, {async = true}) + os.tryrm(root) + print("async copy test finished") +end + diff --git a/tests/modules/os/async_scheduler.lua b/tests/modules/os/async_scheduler.lua new file mode 100644 index 000000000..0a201f8f7 --- /dev/null +++ b/tests/modules/os/async_scheduler.lua @@ -0,0 +1,32 @@ +import("core.base.scheduler") + +local function _prepare_workspace(root) + os.mkdir(root) + for idx = 1, 4 do + io.writefile(path.join(root, string.format("file%d.txt", idx)), "xmake") + end +end + +function main() + local root = os.tmpfile() .. ".os_async_sched" + _prepare_workspace(root) + print("workspace prepared: %s", root) + + local group = "os_async_scheduler" + scheduler.co_group_begin(group, function () + for idx = 1, 4 do + scheduler.co_start(function () + local matches = os.files(path.join(root, string.format("file%d.txt", idx)), {async = true}) + assert(matches and #matches == 1) + print("async match %d finished", idx) + end) + end + + scheduler.co_start(function () + print("async find all files in programdir...") + local files = os.files(path.join(os.programdir(), "**"), {async = true}) + print("files: %d", #files) + print("async find all finished") + end) + end) +end diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index 92890a234..1207cb615 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -149,3 +149,25 @@ function test_args(t) t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""') end +function test_async(t) + local tmpdir = os.tmpfile() .. ".dir" + local tmpdir2 = os.tmpfile() .. ".dir" + io.writefile(path.join(tmpdir, "foo.txt"), "foo") + io.writefile(path.join(tmpdir, "bar.txt"), "bar") + local files = os.files(path.join(tmpdir, "*.txt"), {async = true}) + t:require(files and #files == 2) + + os.cp(tmpdir, tmpdir2, {async = true, detach = true}) + t:require(not os.isdir(tmpdir2)) + + os.cp(tmpdir, tmpdir2, {async = true}) + t:require(os.isdir(tmpdir2)) + + t:require(os.isdir(tmpdir)) + os.rm(tmpdir, {async = true}) + t:require(not os.isdir(tmpdir)) + + t:require(os.isdir(tmpdir2)) + os.rm(tmpdir2, {async = true, detach = true}) + t:require(os.isdir(tmpdir2)) +end diff --git a/tests/runner.lua b/tests/runner.lua index 5b8b0f5f5..5d154674d 100644 --- a/tests/runner.lua +++ b/tests/runner.lua @@ -18,7 +18,6 @@ function main(script, opt) local context = test_context(script) local root = path.directory(script) - local verbose = option.get("verbose") or option.get("diagnosis") -- trace @@ -26,7 +25,6 @@ function main(script, opt) -- get test functions local data = import("test", { rootdir = root, anonymous = true }) - if data.main then -- ignore everthing when we found a main function data = { test_main = data.main } @@ -37,9 +35,12 @@ function main(script, opt) -- run test local succeed_count = 0 + local start_time = os.mclock() for k, v in pairs(data) do if k:startswith("test") and type(v) == "function" then - if verbose then print(">> running %s ...", k) end + if verbose then + print(">> running %s ...", k) + end context.func = v context.funcname = k local result = try @@ -69,7 +70,9 @@ function main(script, opt) succeed_count = succeed_count + 1 end end - if verbose then print(">> finished %d test method(s) ...", succeed_count) end + if verbose then + print(">> finished %d test method(s), spent %0.02fs", succeed_count, (os.mclock() - start_time) / 1000) + end -- leave script directory os.cd(old_dir) |
