1use strict; 2 3BEGIN { 4 require Time::HiRes; 5 unless(&Time::HiRes::d_usleep) { 6 require Test::More; 7 Test::More::plan(skip_all => "no usleep()"); 8 } 9} 10 11use Test::More tests => 6; 12BEGIN { push @INC, '.' } 13use t::Watchdog; 14 15eval { Time::HiRes::usleep(-2) }; 16like $@, qr/::usleep\(-2\): negative time not invented yet/, 17 "negative time error"; 18 19my $limit = 0.25; # 25% is acceptable slosh for testing timers 20 21my $one = CORE::time; 22Time::HiRes::usleep(10_000); 23my $two = CORE::time; 24Time::HiRes::usleep(10_000); 25my $three = CORE::time; 26ok $one == $two || $two == $three 27or print("# slept too long, $one $two $three\n"); 28 29SKIP: { 30 skip "no gettimeofday", 1 unless &Time::HiRes::d_gettimeofday; 31 my $f = Time::HiRes::time(); 32 Time::HiRes::usleep(500_000); 33 my $f2 = Time::HiRes::time(); 34 my $d = $f2 - $f; 35 ok $d > 0.49 or print("# slept $d secs $f to $f2\n"); 36} 37 38SKIP: { 39 skip "no gettimeofday", 1 unless &Time::HiRes::d_gettimeofday; 40 my $r = [ Time::HiRes::gettimeofday() ]; 41 Time::HiRes::sleep( 0.5 ); 42 my $f = Time::HiRes::tv_interval $r; 43 ok $f > 0.49 or print("# slept $f instead of 0.5 secs.\n"); 44} 45 46SKIP: { 47 skip "no gettimeofday", 2 unless &Time::HiRes::d_gettimeofday; 48 49 my ($t0, $td); 50 51 my $sleep = 1.5; # seconds 52 my $msg; 53 54 $t0 = Time::HiRes::gettimeofday(); 55 $a = abs(Time::HiRes::sleep($sleep) / $sleep - 1.0); 56 $td = Time::HiRes::gettimeofday() - $t0; 57 my $ratio = 1.0 + $a; 58 59 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n"; 60 61 SKIP: { 62 skip $msg, 1 unless $td < $sleep * (1 + $limit); 63 ok $a < $limit or print("# $msg\n"); 64 } 65 66 $t0 = Time::HiRes::gettimeofday(); 67 $a = abs(Time::HiRes::usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0); 68 $td = Time::HiRes::gettimeofday() - $t0; 69 $ratio = 1.0 + $a; 70 71 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n"; 72 73 SKIP: { 74 skip $msg, 1 unless $td < $sleep * (1 + $limit); 75 ok $a < $limit or print("# $msg\n"); 76 } 77} 78 791; 80