Commit | Line | Data |
---|---|---|
90e44bf6 Z |
1 | use strict; |
2 | ||
3 | BEGIN { | |
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 | 11 | use Test::More tests => 6; |
465db51d | 12 | BEGIN { push @INC, '.' } |
90e44bf6 Z |
13 | use t::Watchdog; |
14 | ||
15 | eval { Time::HiRes::usleep(-2) }; | |
16 | like $@, qr/::usleep\(-2\): negative time not invented yet/, | |
17 | "negative time error"; | |
18 | ||
19 | my $limit = 0.25; # 25% is acceptable slosh for testing timers | |
20 | ||
21 | my $one = CORE::time; | |
22 | Time::HiRes::usleep(10_000); | |
23 | my $two = CORE::time; | |
24 | Time::HiRes::usleep(10_000); | |
25 | my $three = CORE::time; | |
26 | ok $one == $two || $two == $three | |
c4a535af | 27 | or print("# slept too long, $one $two $three\n"); |
90e44bf6 Z |
28 | |
29 | SKIP: { | |
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 | ||
38 | SKIP: { | |
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 | ||
46 | SKIP: { | |
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 | ||
79 | 1; |