diff --git a/tests/cram/test_runner.t b/tests/cram/test_runner.t new file mode 100644 index 000000000..fcecddd38 --- /dev/null +++ b/tests/cram/test_runner.t @@ -0,0 +1,74 @@ +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] + +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 + +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/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 -- 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), 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(); 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/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); diff --git a/tests/custom/06_lib_uloop/13_process_function b/tests/custom/06_lib_uloop/13_process_function index e513f2cf5..1249541ac 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) { @@ -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(); %} diff --git a/tests/custom/run_tests.uc b/tests/custom/run_tests.uc index b0cade506..650eca6df 100755 --- a/tests/custom/run_tests.uc +++ b/tests/custom/run_tests.uc @@ -9,6 +9,18 @@ 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); +} + +// 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, '/') || '/', /\/+/); @@ -128,7 +140,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 +152,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 +201,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 +209,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 +232,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; @@ -285,9 +311,11 @@ 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++; } } 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);