From f0ebf41a9b3bf856269ec3378006dcccea985588 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 1/9] tests: keep the files of the test runner below TMPDIR run_tests.uc creates the work directory of each test file as /tmp/test. and the files for a diff in /tmp. Two uloop tests create their own directories in /tmp. A run in an environment that provides another temporary directory in TMPDIR writes outside of it. Create the work directory and the diff files below the canonical path of TMPDIR if it is set, and let the two tests create their directories in the work directory. The messages of a test program hold the canonical path of a source file, which does not match a TMPDIR that ends in a slash, as on macOS, or that leads through a symbolic link. End the run with an error if TMPDIR names no directory. A syntax error shortens a source path of more than 48 bytes to "..." and its last 45 bytes. With a longer work directory the runner no longer found the directory in such a path, and the tests 04_modules/06_export_errors and 04_modules/14_circular_imports failed. Replace the visible end of the directory in a shortened path as well. Signed-off-by: Felix Fietkau --- tests/cram/test_runner.t | 42 +++++++++++++++++++ .../06_lib_uloop/09_handle_reopened_path | 4 +- tests/custom/06_lib_uloop/13_process_function | 2 +- tests/custom/run_tests.uc | 30 +++++++++++-- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 tests/cram/test_runner.t diff --git a/tests/cram/test_runner.t b/tests/cram/test_runner.t new file mode 100644 index 000000000..723bc1b2b --- /dev/null +++ b/tests/cram/test_runner.t @@ -0,0 +1,42 @@ +setup common environment: + + $ export UCODE_LIB="$BUILD_BIN_DIR" + $ custom="$TESTDIR/../custom" + $ run_tests() { + > "$BUILD_BIN_DIR/ucode" -L "$BUILD_BIN_DIR/*.so" -S "$@" + > } + +check that the test runner accepts a TMPDIR with a trailing slash: + + $ (export TMPDIR="$TMPDIR/"; run_tests "$custom/run_tests.uc" "$custom/04_modules/07_import_default") > out.txt + $ tail -n 1 out.txt + Ran 1 tests, 1 okay, 0 failures + +check that the test runner accepts a TMPDIR behind a symbolic link: + + $ mkdir tmp.real && ln -s tmp.real tmp.link + $ (export TMPDIR="$PWD/tmp.link"; run_tests "$custom/run_tests.uc" "$custom/04_modules/07_import_default") > out.txt + $ tail -n 1 out.txt + Ran 1 tests, 1 okay, 0 failures + +check that the test runner creates its work directory below TMPDIR: + + $ mkdir -p where/00_dir && cp "$custom/run_tests.uc" where/ + $ cat > where/00_dir/01_workdir < -- Testcase -- + > {% system('pwd > "\$MARKER"') %} + > -- End -- + > -- Expect stdout -- + > -- End -- + > EOF + $ (export TMPDIR="$PWD/tmp.link" MARKER="$PWD/marker"; run_tests where/run_tests.uc) > out.txt + $ tail -n 1 out.txt + Ran 1 tests, 1 okay, 0 failures + $ grep -c "^$(pwd -P)/tmp.real/test\.[0-9]*\$" marker + 1 + +check that the test runner refuses a TMPDIR that does not exist: + + $ (export TMPDIR="$PWD/missing"; run_tests "$custom/run_tests.uc" "$custom/04_modules/07_import_default") + Unable to use the temporary directory '*/missing': No such file or directory (glob) + [1] diff --git a/tests/custom/06_lib_uloop/09_handle_reopened_path b/tests/custom/06_lib_uloop/09_handle_reopened_path index 4844ca5f7..3fd8c484a 100644 --- a/tests/custom/06_lib_uloop/09_handle_reopened_path +++ b/tests/custom/06_lib_uloop/09_handle_reopened_path @@ -11,7 +11,7 @@ that watches the new file. uloop.init(); - let dir = fs.mkdtemp(); + let dir = fs.mkdtemp('./fifo-XXXXXX'); let path = dir + '/fifo'; system(['mkfifo', path]); @@ -73,7 +73,7 @@ old file to it. uloop.init(); - let dir = fs.mkdtemp(); + let dir = fs.mkdtemp('./fifo-XXXXXX'); let path = dir + '/fifo'; system(['mkfifo', path]); diff --git a/tests/custom/06_lib_uloop/13_process_function b/tests/custom/06_lib_uloop/13_process_function index e513f2cf5..869b35008 100644 --- a/tests/custom/06_lib_uloop/13_process_function +++ b/tests/custom/06_lib_uloop/13_process_function @@ -13,7 +13,7 @@ child. uloop.init(); - const tmpdir = fs.mkdtemp('/tmp/ucode-uloop-process-XXXXXX'); + const tmpdir = fs.mkdtemp('./ucode-uloop-process-XXXXXX'); const outfile = tmpdir + '/out'; function run(label, executable, args, env) { diff --git a/tests/custom/run_tests.uc b/tests/custom/run_tests.uc index b0cade506..08359482f 100755 --- a/tests/custom/run_tests.uc +++ b/tests/custom/run_tests.uc @@ -9,6 +9,14 @@ let line = '........................................'; let ucode_bin = getenv('UCODE_BIN') || `${topdir}/build/ucode`; let ucode_lib = getenv('UCODE_LIB') || `${topdir}/build`; let platform = trim(fs.popen('uname -s', 'r').read('all')); +// The messages of a test program hold the canonical path of a source file. +let tmpbase = getenv('TMPDIR') || '/tmp'; +let tmproot = fs.realpath(tmpbase); + +if (tmproot == null) { + warn(`Unable to use the temporary directory '${tmpbase}': ${fs.error()}\n`); + exit(1); +} function mkdir_p(path) { let parts = split(rtrim(path, '/') || '/', /\/+/); @@ -128,7 +136,7 @@ function diff(tag, ...ab) { for (let i, f in ab) { if (type(f) != 'resource') { - push(tmpfiles, fs.mkstemp()); + push(tmpfiles, fs.mkstemp(`${tmproot}/XXXXXX`)); tmpfiles[-1].write(f); f = tmpfiles[-1]; } @@ -140,6 +148,20 @@ function diff(tag, ...ab) { system(cmd); } +// A syntax error shortens a source path of more than 48 bytes to "..." and +// its last 45 bytes, which can leave only the end of the work directory. +function workdir_strip(text, dir) { + text = replace(text, dir, '.'); + + if (index(text, '...') < 0) + return text; + + for (let n = min(length(dir) - 1, 43); n > 0; n--) + text = replace(text, `...${substr(dir, -n)}/`, './'); + + return text; +} + function run_testcase(num, dir, testcase) { let fout = fs.mkstemp(`${dir}/stdout.XXXXXX`); let ferr = fs.mkstemp(`${dir}/stderr.XXXXXX`); @@ -175,7 +197,7 @@ function run_testcase(num, dir, testcase) { let ok = true; - if (replace(ferr.read('all'), dir, '.') != eerr) { + if (workdir_strip(ferr.read('all'), dir) != eerr) { if (ok) print('!\n'); printf("Testcase #%d: Expected stderr did not match:\n", num); diff('stderr', eerr, ferr); @@ -183,7 +205,7 @@ function run_testcase(num, dir, testcase) { ok = false; } - if (replace(fout.read('all'), dir, '.') != eout) { + if (workdir_strip(fout.read('all'), dir) != eout) { if (ok) print('!\n'); printf("Testcase #%d: Expected stdout did not match:\n", num); diff('stdout', eout, fout); @@ -206,7 +228,7 @@ function run_test(file) { let name = fs.basename(file); printf('%s %s ', name, substr(line, length(name))); - let tmpdir = sprintf('/tmp/test.%d', getpid()); + let tmpdir = sprintf('%s/test.%d', tmproot, getpid()); let testcases = parse_testcases(file, tmpdir); let failed = 0, skipped = 0; From a050bd708b38f4cb54442e350bc70922b660f2b2 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 2/9] tests: keep the runner alive if a test program does not read its code run_testcase() writes the code of a test case to the standard input of the test program. A program that ends without a read of its input, like the one of 99_bugs/35_vm_callframe_double_free, whose module that -l loads raises an error, can close the pipe before the runner writes. The runner then gets SIGPIPE and ends with the rest of the tests not run. A host under load shows this. Handle SIGPIPE in the runner, so that the write fails and the runner takes the exit code of the program as before. The runner does not ignore the signal, because the test programs inherit an ignored signal. Fixes: fbabec423498 ("tests: replace test runner shell script with ucode implementation") Signed-off-by: Felix Fietkau --- tests/custom/run_tests.uc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/custom/run_tests.uc b/tests/custom/run_tests.uc index 08359482f..702d73445 100755 --- a/tests/custom/run_tests.uc +++ b/tests/custom/run_tests.uc @@ -18,6 +18,10 @@ if (tmproot == null) { exit(1); } +// A test program can end before it read its code from the pipe: the write must +// fail, not end the runner. The test programs would inherit 'ignore'. +signal('SIGPIPE', () => null); + function mkdir_p(path) { let parts = split(rtrim(path, '/') || '/', /\/+/); let current = ''; From cd1a1519dfafdca8a435ddc865e602c95f3d0ef0 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 22:14:13 +0100 Subject: [PATCH 3/9] tests: count the failed files in the summary of the test runner run_test() returns the number of failed test cases of a file, and the summary adds that number to the count of failed files. A file with two failed cases gives the line "Ran 1 tests, -1 okay, 2 failures". Count a file once if any of its cases failed, like the shell script that run_tests.uc replaced did. Fixes: fbabec423498 ("tests: replace test runner shell script with ucode implementation") Signed-off-by: Felix Fietkau --- tests/cram/test_runner.t | 22 ++++++++++++++++++++++ tests/custom/run_tests.uc | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/cram/test_runner.t b/tests/cram/test_runner.t index 723bc1b2b..2c22ee98e 100644 --- a/tests/cram/test_runner.t +++ b/tests/cram/test_runner.t @@ -40,3 +40,25 @@ check that the test runner refuses a TMPDIR that does not exist: $ (export TMPDIR="$PWD/missing"; run_tests "$custom/run_tests.uc" "$custom/04_modules/07_import_default") Unable to use the temporary directory '*/missing': No such file or directory (glob) [1] + +check that the test runner counts a file with two failed cases as one failure: + + $ mkdir -p suite/00_fail && cp "$custom/run_tests.uc" suite/ + $ cat > suite/00_fail/01_two_failures < -- Expect stdout -- + > a + > -- End -- + > -- Testcase -- + > b + > -- End -- + > -- Expect stdout -- + > c + > -- End -- + > -- Testcase -- + > d + > -- End -- + > EOF + $ run_tests suite/run_tests.uc > out.txt + [1] + $ tail -n 1 out.txt + Ran 1 tests, 0 okay, 1 failures diff --git a/tests/custom/run_tests.uc b/tests/custom/run_tests.uc index 702d73445..c64d981af 100755 --- a/tests/custom/run_tests.uc +++ b/tests/custom/run_tests.uc @@ -311,7 +311,9 @@ for (let catdir in fs.glob(`${testdir}/[0-9][0-9]_*`)) { if (!use_test(testfile)) continue; n_tests++; - n_fails += run_test(testfile); + + if (run_test(testfile)) + n_fails++; } } From cbe09abaad31b4fb28b30c40c752a99fe6a6e740 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 4/9] tests: accept a late end of the sleep in the clock() test 03_stdlib/55_clock requires that 240 ms to 260 ms pass on both clocks around a sleep of 250 ms. A host under load ends the sleep later, and the test fails. Keep the lower limit for both clocks. Read the monotonic clock before and after each read of the realtime clock, and require that the realtime delta lies between the two monotonic deltas, with the tolerance of 10 ms. The upper limit of the monotonic delta itself becomes 60 s. Fixes: 171402fd6fc2 ("lib: add date and time related functions") Signed-off-by: Felix Fietkau --- tests/custom/03_stdlib/55_clock | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/custom/03_stdlib/55_clock b/tests/custom/03_stdlib/55_clock index 109ce218e..987afe742 100644 --- a/tests/custom/03_stdlib/55_clock +++ b/tests/custom/03_stdlib/55_clock @@ -4,23 +4,32 @@ default realtime one. -- Testcase -- {% + function delta(from, to) { + return (to[0] - from[0]) * 1000000000 + (to[1] - from[1]); + } + + let before = clock(true); let t1 = clock(); let t3 = clock(true); sleep(250); - let t2 = clock(); let t4 = clock(true); + let t2 = clock(); + let after = clock(true); - let delta1 = (t2[0] - t1[0]) * 1000000000 + (t2[1] - t1[1]); - let delta2 = (t4[0] - t3[0]) * 1000000000 + (t4[1] - t3[1]); + let delta1 = delta(t1, t2); + let delta2 = delta(t3, t4); + let outer = delta(before, after); assert(delta1 >= 0, "Realtime clock went backwards!"); assert(delta2 >= 0, "Monotonic clock went backwards!"); + // A host under load ends the sleep late, by an unknown time. The monotonic + // readings enclose the realtime ones and bound their delta on both sides. printf("%.J\n", [ - (delta1 >= 240000000 && delta1 <= 260000000) ? true : "unexpected delta: " + delta1, - (delta2 >= 240000000 && delta2 <= 260000000) ? true : "unexpected delta: " + delta2 + (delta1 >= 240000000 && delta1 >= delta2 - 10000000 && delta1 <= outer + 10000000) ? true : "unexpected delta: " + delta1, + (delta2 >= 240000000 && delta2 <= 60000000000) ? true : "unexpected delta: " + delta2 ]); %} -- End -- From 2189d0cbfa0db661365d27c23dd6cdcbacb7fd36 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 5/9] tests: signal the child of process() when it reports its function 06_lib_uloop/13_process_function sends a signal to the child 100 ms after the fork and lets the function of the child sleep for 300 ms. If the host does not run the parent for that time, the child ends first, and the test prints "TERM: 0". Let the function report through a pipe that it runs, and wait for the parent on a second pipe. The parent sends the signal after the report and lets the child go on afterwards, so a child that survived SIGTERM still ends with code 0. The SIGUSR1 case runs further passes of the loop before it prints the count, because a signal that reached the callbacks of the parent is an event that can follow the exit of the child. Fixes: 1ed0e1b405fe ("uloop: allow a function to prepare the child in process()") Signed-off-by: Felix Fietkau --- tests/custom/06_lib_uloop/13_process_function | 72 +++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/tests/custom/06_lib_uloop/13_process_function b/tests/custom/06_lib_uloop/13_process_function index 869b35008..1249541ac 100644 --- a/tests/custom/06_lib_uloop/13_process_function +++ b/tests/custom/06_lib_uloop/13_process_function @@ -59,16 +59,37 @@ child. return ['/bin/true']; }); + let ready = fs.pipe(), resume = fs.pipe(); + + function signal_wait() { + ready[1].write('x'); + ready[1].flush(); + resume[0].read(1); + + return ['/bin/true']; + } + + // The signal must find the child in its function: wait for its report. + // A child that the signal did not end goes on and exits with code 0. + function signal_send(child, signal) { + ready[1].close(); + + // a signal for the parent, like SIGCHLD, interrupts the read + for (let i = 0; i < 100 && ready[0].read(1) == null; i++) + continue; + + system(['kill', signal, `${child.pid()}`]); + resume[1].write('x'); + resume[1].flush(); + } + uloop.timer(1, () => { - let child = uloop.process(() => { - sleep(300); - return ['/bin/true']; - }, null, null, (status) => { + let child = uloop.process(signal_wait, null, null, (status) => { printf('TERM: %d\n', status); uloop.end(); }); - uloop.timer(100, () => system(['kill', '-TERM', `${child.pid()}`])); + signal_send(child, '-TERM'); }); uloop.run(); @@ -100,29 +121,56 @@ parent. -- Testcase -- {% import * as uloop from 'uloop'; + import * as fs from 'fs'; if (!uloop.signal) exit(77); uloop.init(); - let usr1_count = 0; + let usr1_count = 0, child_status; let usr1 = uloop.signal('SIGUSR1', () => usr1_count++); + let ready = fs.pipe(), resume = fs.pipe(); + + function signal_wait() { + ready[1].write('x'); + ready[1].flush(); + resume[0].read(1); + + return ['/bin/true']; + } + + // The signal must find the child in its function: wait for its report. + function signal_send(child, signal) { + ready[1].close(); + + // a signal for the parent, like SIGCHLD, interrupts the read + for (let i = 0; i < 100 && ready[0].read(1) == null; i++) + continue; + + system(['kill', signal, `${child.pid()}`]); + resume[1].write('x'); + resume[1].flush(); + } uloop.timer(1, () => { - let child = uloop.process(() => { - sleep(300); - return ['/bin/true']; - }, null, null, (status) => { - printf('USR1: %d, parent callbacks: %d\n', status, usr1_count); + let child = uloop.process(signal_wait, null, null, (status) => { + child_status = status; uloop.end(); }); - uloop.timer(100, () => system(['kill', '-USR1', `${child.pid()}`])); + signal_send(child, '-USR1'); }); uloop.run(); + // A signal that the child passed to the parent is an event of the loop, + // which can come after the exit of the child. + for (let i = 0; i < 3; i++) + uloop.run(0); + + printf('USR1: %d, parent callbacks: %d\n', child_status, usr1_count); + usr1.delete(); uloop.done(); %} From b1e68c3d2bccb04f4ce341c9846292c1e9702139 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 6/9] tests: wait for the timers in the uloop timer lifetime test Two cases of 06_lib_uloop/02_timer_lifetime end the loop with a timer of 20 ms or 30 ms and expect that a timer rearmed twice, or an interval of 5 ms, fired before. On a host under load the end timer is due before that, and the cases print "rearmed: 2" or "armed: false". Run the loop in periods of 10 ms until the expected callbacks ran, for 10 s at most. The rearm case then runs the loop for a further period, in which a timer that is still armed fires. The runs that expect no tick keep their delay. End each period from a timer. Without libubox commit 362951a2d96e ("uloop: fix uloop_run_timeout"), uloop.run() with a timeout returns only when the loop ends. Fixes: 115d308a8514 ("uloop: release timers and handles once they are finished") Fixes: 986390458007 ("uloop: disarm an interval on set() without a value") Signed-off-by: Felix Fietkau --- tests/custom/06_lib_uloop/02_timer_lifetime | 31 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/custom/06_lib_uloop/02_timer_lifetime b/tests/custom/06_lib_uloop/02_timer_lifetime index cd484cd11..b2695f1d7 100644 --- a/tests/custom/06_lib_uloop/02_timer_lifetime +++ b/tests/custom/06_lib_uloop/02_timer_lifetime @@ -92,6 +92,20 @@ freed once it expires without being rearmed. uloop.init(); + // With an old libubox, uloop.run() with a timeout returns only when the + // loop ends: end the loop from a timer. + function run(msecs) { + uloop.timer(msecs, () => uloop.end()); + uloop.run(); + } + + // A host under load needs an unknown time until its timers fired: run + // the loop until the state is there. + function loop_until(done) { + for (let limit = time() + 10; !done() && time() < limit; ) + run(10); + } + let rearmed = 0, deferred = 0; let base = gc('count'); @@ -104,8 +118,10 @@ freed once it expires without being rearmed. deferred++; }).set(2); - uloop.timer(20, () => uloop.end()); - uloop.run(); + loop_until(() => rearmed == 3 && deferred == 1); + + // a timer that is still armed fires in a further run + run(15); printf("rearmed: %d, deferred: %d, alive: %d\n", rearmed, deferred, gc('count') - base); @@ -222,10 +238,17 @@ is rejected by set() and leaves a new interval unarmed. uloop.run(); } + // A host under load needs an unknown time until an interval fired: run + // the loop until the state is there. + function loop_until(done) { + for (let limit = time() + 10; !done() && time() < limit; ) + run(10); + } + let ticks = 0; let interval = uloop.interval(5, () => ticks++); - run(30); + loop_until(() => ticks > 0); printf("armed: %s\n", ticks > 0); printf("set(): %s, remaining: %d\n", interval.set(), interval.remaining()); @@ -237,7 +260,7 @@ is rejected by set() and leaves a new interval unarmed. printf("set(0): %s, %s\n", interval.set(0), uloop.error()); interval.set(5); - run(30); + loop_until(() => ticks > 0); printf("rearmed: %s\n", ticks > 0); printf("set(null): %s, remaining: %d\n", interval.set(null), From 0523d8f3c6ce2ee8b5f6694b521c8c5fa017800e Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 7/9] tests: wait for the events in the uloop resource release test The cases of 06_lib_uloop/03_resource_release end the loop with a timer of 50 ms or 500 ms. They expect that a process and a task ended, that a closed pipe reported two hangups, or that an interval of 1 ms ticked three times before. On a host under load the cases print "hangups: 1" or "ticks: 2". Run the loop in periods of 10 ms until the events arrived and the descriptors of the handle and the task are closed, for 10 s at most. Send the signal after the third tick. Run the loop for a further period afterwards, in which a handle or an interval that is still registered calls its callback again. End each period from a timer. Without libubox commit 362951a2d96e ("uloop: fix uloop_run_timeout"), uloop.run() with a timeout returns only when the loop ends. Fixes: 115d308a8514 ("uloop: release timers and handles once they are finished") Signed-off-by: Felix Fietkau --- tests/custom/06_lib_uloop/03_resource_release | 72 ++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/tests/custom/06_lib_uloop/03_resource_release b/tests/custom/06_lib_uloop/03_resource_release index ebd30e9c0..76196aabf 100644 --- a/tests/custom/06_lib_uloop/03_resource_release +++ b/tests/custom/06_lib_uloop/03_resource_release @@ -10,7 +10,22 @@ after an error or hangup. uloop.init(); + // With an old libubox, uloop.run() with a timeout returns only when the + // loop ends: end the loop from a timer. + function run(msecs) { + uloop.timer(msecs, () => uloop.end()); + uloop.run(); + } + + // A host under load needs an unknown time until the process and the task + // ended: run the loop until the state is there. + function loop_until(done) { + for (let limit = time() + 10; !done() && time() < limit; ) + run(10); + } + let events = []; + let base_fds = length(fs.lsdir('/dev/fd')); let base = gc('count'); let fds = fs.pipe(); @@ -31,8 +46,9 @@ after an error or hangup. (msg) => push(events, sprintf("task output=%s", msg)) ); - uloop.timer(500, () => uloop.end()); - uloop.run(); + // the released handle and the finished task close their descriptors + loop_until(() => length(events) == 3 && + length(fs.lsdir('/dev/fd')) == base_fds); printf("%s\nalive: %d\n", join('\n', sort(events)), gc('count') - base); @@ -58,6 +74,20 @@ deleted, even without a script reference. uloop.init(); + // With an old libubox, uloop.run() with a timeout returns only when the + // loop ends: end the loop from a timer. + function run(msecs) { + uloop.timer(msecs, () => uloop.end()); + uloop.run(); + } + + // A run whose timer is due at its start ends before it reports a hangup: + // run the loop until the state is there. + function loop_until(done) { + for (let limit = time() + 10; !done() && time() < limit; ) + run(10); + } + let base = gc('count'); let fds = fs.pipe(); let hangups = 0; @@ -70,10 +100,13 @@ deleted, even without a script reference. fds[1].close(); fds = null; - uloop.timer(50, () => uloop.end()); - printf("registered: %s\n", gc('count') > base); - uloop.run(); + + loop_until(() => hangups >= 2); + + // a handle that is still registered reports more hangups + run(10); + printf("hangups: %d, alive: %d\n", hangups, gc('count') - base); uloop.done(); @@ -98,8 +131,23 @@ deleted, even without a script reference. uloop.init(); + // the signal callback ends a run before its timer + function run(msecs) { + let timer = uloop.timer(msecs, () => uloop.end()); + + uloop.run(); + timer.cancel(); + } + + // A host under load needs an unknown time for three ticks and for the + // signal: run the loop until the state is there. + function loop_until(done) { + for (let limit = time() + 10; !done() && time() < limit; ) + run(10); + } + let base = gc('count'); - let ticks = 0; + let ticks = 0, signalled = false; uloop.interval(1, function() { if (++ticks == 3) @@ -109,13 +157,19 @@ deleted, even without a script reference. uloop.signal('SIGUSR1', function() { printf("signal received\n"); this.delete(); + signalled = true; uloop.end(); }); - uloop.timer(50, () => system('kill -USR1 $PPID')); - printf("registered: %s\n", gc('count') > base); - uloop.run(); + + loop_until(() => ticks >= 3); + system('kill -USR1 $PPID'); + loop_until(() => signalled); + + // an interval that is still armed ticks in a further run + run(15); + printf("ticks: %d, alive: %d\n", ticks, gc('count') - base); uloop.done(); From e31515eb5617cf4dd44dc35cabf92d596d9836b4 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 8/9] tests: let the task wait for the ticks of the event loop The first case of 06_lib_uloop/11_task_messages lets the task sleep for 500 ms between two messages and expects ten ticks of a 20 ms timer in the parent in that time. A parent that the host does not run for a part of that time counts fewer ticks. Let the parent create a file after the tenth tick, and let the task send its second message when the file exists. The task gives up after 10 s, so a loop that blocks between the messages still fails the case. Also end the loops of all three cases after 60 s, and only if the last message did not arrive, so that a slow host has the time to send the large message of the second case. Fixes: 1f54bd285339 ("uloop: read task messages without blocking the event loop") Signed-off-by: Felix Fietkau --- tests/custom/06_lib_uloop/11_task_messages | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/custom/06_lib_uloop/11_task_messages b/tests/custom/06_lib_uloop/11_task_messages index 481ad225b..07c4e3132 100644 --- a/tests/custom/06_lib_uloop/11_task_messages +++ b/tests/custom/06_lib_uloop/11_task_messages @@ -3,20 +3,29 @@ The event loop keeps running while a task works between two messages. -- Testcase -- {% import * as uloop from 'uloop'; + import * as fs from 'fs'; uloop.init(); + const ticked = './ticked'; let ticks = 0, seen = []; uloop.timer(20, function() { ticks++; this.set(20); + + if (length(seen) == 1 && ticks - seen[0][1] == 10) + fs.writefile(ticked, ''); }); uloop.task( (pipe) => { pipe.send('first'); - sleep(500); + + // A host under load needs an unknown time for ten ticks. A loop + // that does not run never creates the file: give up after 10 s. + for (let i = 0; i < 1000 && !fs.access(ticked); i++) + sleep(10); return 'last'; }, @@ -28,12 +37,13 @@ The event loop keeps running while a task works between two messages. } ); - uloop.timer(2000, () => uloop.end()); + uloop.timer(60000, () => uloop.end()); uloop.run(); printf("%J, ticks between: %s\n", map(seen, (m) => m[0]), seen[1][1] - seen[0][1] >= 10); + fs.unlink(ticked); uloop.done(); %} -- End -- @@ -70,7 +80,7 @@ delivered complete and in order. } ); - uloop.timer(5000, () => uloop.end()); + uloop.timer(60000, () => uloop.end()); uloop.run(); printf("%J\n", seen); @@ -114,7 +124,7 @@ Input requests are answered in order with the output messages around them. () => `in ${n++}` ); - uloop.timer(2000, () => uloop.end()); + uloop.timer(60000, () => uloop.end()); uloop.run(); printf("%J\n", seen); From 6e5e4cd4a8bfefe1bf46885fd41301f1ccce96b9 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 19 Nov 2026 13:51:39 +0100 Subject: [PATCH 9/9] tests: let the test runner return 1 if a test file failed run_tests.uc passes the number of failed files to exit(). The exit status holds the low 8 bits of that number only, so a run with 256 failed files ends with the status 0. Return 1 for a run with failures. Fixes: 41d33d0b2b09 ("tests: custom: return exit code if tests fails") Signed-off-by: Felix Fietkau --- tests/cram/test_runner.t | 10 ++++++++++ tests/custom/run_tests.uc | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/cram/test_runner.t b/tests/cram/test_runner.t index 2c22ee98e..fcecddd38 100644 --- a/tests/cram/test_runner.t +++ b/tests/cram/test_runner.t @@ -62,3 +62,13 @@ check that the test runner counts a file with two failed cases as one failure: [1] $ tail -n 1 out.txt Ran 1 tests, 0 okay, 1 failures + +check that the test runner fails if 256 files failed, with a test program +that prints nothing: + + $ mkdir -p many/00_fail && cp "$custom/run_tests.uc" many/ + $ for i in $(seq 100 355); do cp suite/00_fail/01_two_failures many/00_fail/01_$i; done + $ (export UCODE_BIN=false; run_tests many/run_tests.uc) > out.txt + [1] + $ tail -n 1 out.txt + Ran 256 tests, 0 okay, 256 failures diff --git a/tests/custom/run_tests.uc b/tests/custom/run_tests.uc index c64d981af..650eca6df 100755 --- a/tests/custom/run_tests.uc +++ b/tests/custom/run_tests.uc @@ -318,4 +318,4 @@ for (let catdir in fs.glob(`${testdir}/[0-9][0-9]_*`)) { } printf('\nRan %d tests, %d okay, %d failures\n', n_tests, n_tests - n_fails, n_fails); -exit(n_fails); +exit(n_fails ? 1 : 0);