Skip to content
Open
74 changes: 74 additions & 0 deletions tests/cram/test_runner.t
Original file line number Diff line number Diff line change
@@ -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 <<EOF
> -- 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 <<EOF
> -- 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
19 changes: 14 additions & 5 deletions tests/custom/03_stdlib/55_clock
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down
31 changes: 27 additions & 4 deletions tests/custom/06_lib_uloop/02_timer_lifetime
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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),
Expand Down
72 changes: 63 additions & 9 deletions tests/custom/06_lib_uloop/03_resource_release
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/custom/06_lib_uloop/09_handle_reopened_path
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down
18 changes: 14 additions & 4 deletions tests/custom/06_lib_uloop/11_task_messages
Original file line number Diff line number Diff line change
Expand Up @@ -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';
},
Expand All @@ -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 --
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading
Loading