Upgrade to Time::HiRes 1.83
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / t / HiRes.t
index efa8ba6..ad4959e 100644 (file)
@@ -33,9 +33,10 @@ my $have_clock_getres        = &Time::HiRes::d_clock_getres;
 
 sub has_symbol {
     my $symbol = shift;
-    eval "import Time::HiRes qw($symbol)";
+    eval "use Time::HiRes qw($symbol)";
     return 0 unless $@ eq '';
-    return exists &{"Time::HiRes::$symbol"};
+    eval "my \$a = $symbol";
+    return $@ eq '';
 }
 
 printf "# have_gettimeofday  = %d\n", $have_gettimeofday;
@@ -54,6 +55,8 @@ import Time::HiRes 'clock_getres'     if $have_clock_getres;
 
 use Config;
 
+use Time::HiRes qw(gettimeofday);
+
 my $have_alarm = $Config{d_alarm};
 my $have_fork  = $Config{d_fork};
 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
@@ -140,6 +143,7 @@ unless ($have_usleep) {
     skip 7..8;
 }
 else {
+    use Time::HiRes qw(usleep);
     my $one = time;
     usleep(10_000);
     my $two = time;
@@ -178,7 +182,7 @@ unless ($have_usleep && $have_gettimeofday) {
     skip 11;
 }
 else {
-    my $r = [gettimeofday()];
+    my $r = [ gettimeofday() ];
     Time::HiRes::sleep( 0.5 );
     my $f = tv_interval $r;
     ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
@@ -333,7 +337,10 @@ unless (   defined &Time::HiRes::setitimer
     $SIG{VTALRM} = 'DEFAULT';
 }
 
-if ($have_gettimeofday) {
+if ($have_gettimeofday &&
+    $have_usleep) {
+    use Time::HiRes qw(usleep);
+
     my ($t0, $td);
 
     my $sleep = 1.5; # seconds
@@ -490,23 +497,36 @@ if ($have_clock_gettime &&
     # All implementations of clock_gettime() 
     # are SUPPOSED TO support CLOCK_REALTIME.
     has_symbol('CLOCK_REALTIME')) {
-    my $t0 = clock_gettime(&CLOCK_REALTIME);
-    use Time::HiRes qw(sleep);
-    my $T = 1.5;
-    sleep($T);
-    my $t1 = clock_gettime(&CLOCK_REALTIME);
-    if ($t0 > 0 && $t1 > $t0) {
-       print "# t1 = $t1, t0 = $t0\n";
-       my $dt = $t1 - $t0;
-       my $rt = abs(1 - $dt / $T);
-       if ($rt <= $limit) {
-           print "ok 30 # dt = $dt, r = $rt\n";
-       } else {
-           print "not ok 30 # dt = $dt, rt = $rt\n";
+    my $ok = 0;
+ TRY: {
+       for my $try (1..3) {
+           print "# CLOCK_REALTIME: try = $try\n";
+           my $t0 = clock_gettime(&CLOCK_REALTIME);
+           use Time::HiRes qw(sleep);
+           my $T = 1.5;
+           sleep($T);
+           my $t1 = clock_gettime(&CLOCK_REALTIME);
+           if ($t0 > 0 && $t1 > $t0) {
+               print "# t1 = $t1, t0 = $t0\n";
+               my $dt = $t1 - $t0;
+               my $rt = abs(1 - $dt / $T);
+               print "# dt = $dt, rt = $rt\n";
+               if ($rt <= 2 * $limit) {
+                   $ok = 1;
+                   last TRY;
+               }
+           } else {
+               print "# Error: t0 = $t0, t1 = $t1\n";
+           }
+           my $r = rand() + rand();
+           printf "# Sleeping for %.6f seconds...\n";
+           sleep($r);
        }
+    }
+    if ($ok) {
+       print "ok 30\n";
     } else {
-       print "# Error: t0 = $t0, t1 = $t1\n";
-       skip 30;
+       print "not ok 30\n";
     }
 } else {
     print "# No clock_gettime\n";