1N/A#!./perl -w
1N/A
1N/ABEGIN {
1N/A if ($ENV{PERL_CORE}) {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A }
1N/A}
1N/A
1N/ABEGIN { $| = 1; print "1..25\n"; }
1N/A
1N/AEND {print "not ok 1\n" unless $loaded;}
1N/A
1N/Ause Time::HiRes qw(tv_interval);
1N/A
1N/A$loaded = 1;
1N/A
1N/Aprint "ok 1\n";
1N/A
1N/Ause strict;
1N/A
1N/Amy $have_gettimeofday = defined &Time::HiRes::gettimeofday;
1N/Amy $have_usleep = defined &Time::HiRes::usleep;
1N/Amy $have_ualarm = defined &Time::HiRes::ualarm;
1N/Amy $have_time = defined &Time::HiRes::time;
1N/A
1N/Aimport Time::HiRes 'gettimeofday' if $have_gettimeofday;
1N/Aimport Time::HiRes 'usleep' if $have_usleep;
1N/Aimport Time::HiRes 'ualarm' if $have_ualarm;
1N/A
1N/Ause Config;
1N/A
1N/Amy $have_alarm = $Config{d_alarm};
1N/Amy $have_fork = $Config{d_fork};
1N/Amy $waitfor = 60; # 10 seconds is normal.
1N/Amy $pid;
1N/A
1N/Aif ($have_fork) {
1N/A print "# Testing process $$\n";
1N/A print "# Starting the timer process\n";
1N/A if (defined ($pid = fork())) {
1N/A if ($pid == 0) { # We are the kid, set up the timer.
1N/A print "# Timer process $$\n";
1N/A sleep($waitfor);
1N/A warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded\n";
1N/A print "# Terminating the testing process\n";
1N/A kill('TERM', getppid());
1N/A print "# Timer process exiting\n";
1N/A exit(0);
1N/A }
1N/A } else {
1N/A warn "$0: fork failed: $!\n";
1N/A }
1N/A} else {
1N/A print "# No timer process\n";
1N/A}
1N/A
1N/Amy $xdefine = '';
1N/A
1N/Aif (open(XDEFINE, "xdefine")) {
1N/A chomp($xdefine = <XDEFINE>);
1N/A close(XDEFINE);
1N/A}
1N/A
1N/A# Ideally, we'd like to test that the timers are rather precise.
1N/A# However, if the system is busy, there are no guarantees on how
1N/A# quickly we will return. This limit used to be 10%, but that
1N/A# was occasionally triggered falsely.
1N/A# Try 20%.
1N/A# Another possibility might be to print "ok" if the test completes fine
1N/A# with (say) 10% slosh, "skip - system may have been busy?" if the test
1N/A# completes fine with (say) 30% slosh, and fail otherwise. If you do that,
1N/A# consider changing over to test.pl at the same time.
1N/A# --A.D., Nov 27, 2001
1N/Amy $limit = 0.20; # 20% is acceptable slosh for testing timers
1N/A
1N/Asub skip {
1N/A map { print "ok $_ # skipped\n" } @_;
1N/A}
1N/A
1N/Asub ok {
1N/A my ($n, $result, @info) = @_;
1N/A if ($result) {
1N/A print "ok $n\n";
1N/A }
1N/A else {
1N/A print "not ok $n\n";
1N/A print "# @info\n" if @info;
1N/A }
1N/A}
1N/A
1N/Aif (!$have_gettimeofday) {
1N/A skip 2..6;
1N/A}
1N/Aelse {
1N/A my @one = gettimeofday();
1N/A ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
1N/A ok 3, $one[0] > 850_000_000, "@one too small";
1N/A
1N/A sleep 1;
1N/A
1N/A my @two = gettimeofday();
1N/A ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
1N/A "@two is not greater than @one";
1N/A
1N/A my $f = Time::HiRes::time();
1N/A ok 5, $f > 850_000_000, "$f too small";
1N/A ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
1N/A}
1N/A
1N/Aif (!$have_usleep) {
1N/A skip 7..8;
1N/A}
1N/Aelse {
1N/A my $one = time;
1N/A usleep(10_000);
1N/A my $two = time;
1N/A usleep(10_000);
1N/A my $three = time;
1N/A ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
1N/A
1N/A if (!$have_gettimeofday) {
1N/A skip 8;
1N/A }
1N/A else {
1N/A my $f = Time::HiRes::time();
1N/A usleep(500_000);
1N/A my $f2 = Time::HiRes::time();
1N/A my $d = $f2 - $f;
1N/A ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
1N/A }
1N/A}
1N/A
1N/A# Two-arg tv_interval() is always available.
1N/A{
1N/A my $f = tv_interval [5, 100_000], [10, 500_000];
1N/A ok 9, abs($f - 5.4) < 0.001, $f;
1N/A}
1N/A
1N/Aif (!$have_gettimeofday) {
1N/A skip 10;
1N/A}
1N/Aelse {
1N/A my $r = [gettimeofday()];
1N/A my $f = tv_interval $r;
1N/A ok 10, $f < 2, $f;
1N/A}
1N/A
1N/Aif (!$have_usleep || !$have_gettimeofday) {
1N/A skip 11;
1N/A}
1N/Aelse {
1N/A my $r = [gettimeofday()];
1N/A Time::HiRes::sleep( 0.5 );
1N/A my $f = tv_interval $r;
1N/A ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
1N/A}
1N/A
1N/Aif (!$have_ualarm || !$have_alarm) {
1N/A skip 12..13;
1N/A}
1N/Aelse {
1N/A my $tick = 0;
1N/A local $SIG{ALRM} = sub { $tick++ };
1N/A
1N/A my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
1N/A my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
1N/A my $three = time;
1N/A ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
1N/A
1N/A $tick = 0;
1N/A ualarm(10_000, 10_000);
1N/A while ($tick < 3) { sleep }
1N/A ok 13, 1;
1N/A ualarm(0);
1N/A}
1N/A
1N/A# new test: did we even get close?
1N/A
1N/Aif (!$have_time) {
1N/A skip 14
1N/A} else {
1N/A my ($s, $n);
1N/A for my $i (1 .. 100) {
1N/A $s += Time::HiRes::time() - time();
1N/A $n++;
1N/A }
1N/A # $s should be, at worst, equal to $n
1N/A # (time() may be rounding down, up, or closest)
1N/A ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
1N/A print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
1N/A}
1N/A
1N/Amy $has_ualarm = $Config{d_ualarm};
1N/A
1N/A$has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
1N/A
1N/Aunless ( defined &Time::HiRes::gettimeofday
1N/A && defined &Time::HiRes::ualarm
1N/A && defined &Time::HiRes::usleep
1N/A && $has_ualarm) {
1N/A for (15..17) {
1N/A print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
1N/A }
1N/A} else {
1N/A use Time::HiRes qw (time alarm sleep);
1N/A
1N/A my ($f, $r, $i, $not, $ok);
1N/A
1N/A $f = time;
1N/A print "# time...$f\n";
1N/A print "ok 15\n";
1N/A
1N/A $r = [Time::HiRes::gettimeofday()];
1N/A sleep (0.5);
1N/A print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
1N/A
1N/A $r = [Time::HiRes::gettimeofday()];
1N/A $i = 5;
1N/A $SIG{ALRM} = "tick";
1N/A while ($i > 0)
1N/A {
1N/A alarm(0.3);
1N/A select (undef, undef, undef, 3);
1N/A my $ival = Time::HiRes::tv_interval ($r);
1N/A print "# Select returned! $i $ival\n";
1N/A print "# ", abs($ival/3 - 1), "\n";
1N/A # Whether select() gets restarted after signals is
1N/A # implementation dependent. If it is restarted, we
1N/A # will get about 3.3 seconds: 3 from the select, 0.3
1N/A # from the alarm. If this happens, let's just skip
1N/A # this particular test. --jhi
1N/A if (abs($ival/3.3 - 1) < $limit) {
1N/A $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
1N/A undef $not;
1N/A last;
1N/A }
1N/A my $exp = 0.3 * (5 - $i);
1N/A # This test is more sensitive, so impose a softer limit.
1N/A if (abs($ival/$exp - 1) > 3*$limit) {
1N/A my $ratio = abs($ival/$exp);
1N/A $not = "while: $exp sleep took $ival ratio $ratio";
1N/A last;
1N/A }
1N/A $ok = $i;
1N/A }
1N/A
1N/A sub tick
1N/A {
1N/A $i--;
1N/A my $ival = Time::HiRes::tv_interval ($r);
1N/A print "# Tick! $i $ival\n";
1N/A my $exp = 0.3 * (5 - $i);
1N/A # This test is more sensitive, so impose a softer limit.
1N/A if (abs($ival/$exp - 1) > 3*$limit) {
1N/A my $ratio = abs($ival/$exp);
1N/A $not = "tick: $exp sleep took $ival ratio $ratio";
1N/A $i = 0;
1N/A }
1N/A }
1N/A
1N/A alarm(0); # can't cancel usig %SIG
1N/A
1N/A print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
1N/A}
1N/A
1N/Aunless ( defined &Time::HiRes::setitimer
1N/A && defined &Time::HiRes::getitimer
1N/A && eval 'Time::HiRes::ITIMER_VIRTUAL'
1N/A && $Config{d_select}
1N/A && $Config{sig_name} =~ m/\bVTALRM\b/) {
1N/A for (18..19) {
1N/A print "ok $_ # Skip: no virtual interval timers\n";
1N/A }
1N/A} else {
1N/A use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
1N/A
1N/A my $i = 3;
1N/A my $r = [Time::HiRes::gettimeofday()];
1N/A
1N/A $SIG{VTALRM} = sub {
1N/A $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
1N/A print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
1N/A };
1N/A
1N/A print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
1N/A
1N/A # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
1N/A print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
1N/A print "ok 18\n";
1N/A
1N/A print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
1N/A
1N/A while (getitimer(ITIMER_VIRTUAL)) {
1N/A my $j;
1N/A for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
1N/A }
1N/A
1N/A print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
1N/A
1N/A print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
1N/A print "ok 19\n";
1N/A
1N/A $SIG{VTALRM} = 'DEFAULT';
1N/A}
1N/A
1N/Aif ($have_gettimeofday) {
1N/A my ($t0, $td);
1N/A
1N/A my $sleep = 1.5; # seconds
1N/A my $msg;
1N/A
1N/A $t0 = gettimeofday();
1N/A $a = abs(sleep($sleep) / $sleep - 1.0);
1N/A $td = gettimeofday() - $t0;
1N/A my $ratio = 1.0 + $a;
1N/A
1N/A $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
1N/A
1N/A if ($td < $sleep * (1 + $limit)) {
1N/A print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
1N/A } else {
1N/A print "ok 20 # Skip: $msg";
1N/A }
1N/A
1N/A $t0 = gettimeofday();
1N/A $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
1N/A $td = gettimeofday() - $t0;
1N/A $ratio = 1.0 + $a;
1N/A
1N/A $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
1N/A
1N/A if ($td < $sleep * (1 + $limit)) {
1N/A print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
1N/A } else {
1N/A print "ok 21 # Skip: $msg";
1N/A }
1N/A
1N/A} else {
1N/A for (20..21) {
1N/A print "ok $_ # Skip: no gettimeofday\n";
1N/A }
1N/A}
1N/A
1N/Aeval { sleep(-1) };
1N/Aprint $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
1N/A "ok 22\n" : "not ok 22\n";
1N/A
1N/Aeval { usleep(-2) };
1N/Aprint $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
1N/A "ok 23\n" : "not ok 23\n";
1N/A
1N/Aif ($have_ualarm) {
1N/A eval { alarm(-3) };
1N/A print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
1N/A "ok 24\n" : "not ok 24\n";
1N/A
1N/A eval { ualarm(-4) };
1N/A print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
1N/A "ok 25\n" : "not ok 25\n";
1N/A} else {
1N/A skip 24;
1N/A skip 25;
1N/A}
1N/A
1N/Aif (defined $pid) {
1N/A print "# Terminating the timer process $pid\n";
1N/A kill('TERM', $pid); # We are done, the timer can go.
1N/A}
1N/A