Commit | Line | Data |
---|---|---|
90e44bf6 Z |
1 | use strict; |
2 | ||
3 | sub has_symbol { | |
4 | my $symbol = shift; | |
5 | eval "use Time::HiRes qw($symbol)"; | |
6 | return 0 unless $@ eq ''; | |
7 | eval "my \$a = $symbol"; | |
8 | return $@ eq ''; | |
9 | } | |
10 | ||
11 | use Config; | |
12 | ||
13 | BEGIN { | |
14 | require Time::HiRes; | |
15 | unless(defined &Time::HiRes::setitimer | |
16 | && defined &Time::HiRes::getitimer | |
17 | && has_symbol('ITIMER_VIRTUAL') | |
18 | && $Config{sig_name} =~ m/\bVTALRM\b/ | |
19 | && $^O ne 'nto' # nto: QNX 6 has the API but no implementation | |
20 | && $^O ne 'haiku' # haiku: has the API but no implementation | |
21 | ) { | |
22 | require Test::More; | |
23 | Test::More::plan(skip_all => "no itimer"); | |
24 | } | |
25 | } | |
26 | ||
27 | use Test::More 0.82 tests => 2; | |
28 | use t::Watchdog; | |
29 | ||
30 | my $limit = 0.25; # 25% is acceptable slosh for testing timers | |
31 | ||
32 | my $i = 3; | |
33 | my $r = [Time::HiRes::gettimeofday()]; | |
34 | ||
35 | $SIG{VTALRM} = sub { | |
36 | $i ? $i-- : Time::HiRes::setitimer(&Time::HiRes::ITIMER_VIRTUAL, 0); | |
37 | note "Tick! $i ", Time::HiRes::tv_interval($r); | |
38 | }; | |
39 | ||
40 | note "setitimer: ", join(" ", | |
41 | Time::HiRes::setitimer(&Time::HiRes::ITIMER_VIRTUAL, 0.5, 0.4)); | |
42 | ||
43 | # Assume interval timer granularity of $limit * 0.5 seconds. Too bold? | |
44 | my $virt = Time::HiRes::getitimer(&Time::HiRes::ITIMER_VIRTUAL); | |
45 | ok defined $virt && abs($virt / 0.5) - 1 < $limit; | |
46 | ||
47 | note "getitimer: ", join(" ", | |
48 | Time::HiRes::getitimer(&Time::HiRes::ITIMER_VIRTUAL)); | |
49 | ||
50 | while (Time::HiRes::getitimer(&Time::HiRes::ITIMER_VIRTUAL)) { | |
51 | my $j; | |
52 | for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer(). | |
53 | } | |
54 | ||
55 | note "getitimer: ", join(" ", | |
56 | Time::HiRes::getitimer(&Time::HiRes::ITIMER_VIRTUAL)); | |
57 | ||
58 | $virt = Time::HiRes::getitimer(&Time::HiRes::ITIMER_VIRTUAL); | |
59 | ok defined $virt && $virt == 0; | |
60 | ||
61 | $SIG{VTALRM} = 'DEFAULT'; | |
62 | ||
63 | 1; |