This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix dist/Time-HiRes/t/*.t that assumed '.' in @INC
[perl5.git] / dist / Time-HiRes / t / usleep.t
CommitLineData
90e44bf6
Z
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
c4a535af 11use Test::More tests => 6;
465db51d 12BEGIN { push @INC, '.' }
90e44bf6
Z
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
c4a535af 27or print("# slept too long, $one $two $three\n");
90e44bf6
Z
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;
c4a535af 35 ok $d > 0.4 && $d < 0.9 or print("# slept $d secs $f to $f2\n");
90e44bf6
Z
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;
c4a535af 43 ok $f > 0.4 && $f < 0.9 or print("# slept $f instead of 0.5 secs.\n");
90e44bf6
Z
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);
c4a535af 63 ok $a < $limit or print("# $msg\n");
90e44bf6
Z
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);
c4a535af 75 ok $a < $limit or print("# $msg\n");
90e44bf6
Z
76 }
77}
78
791;