6 BEGIN { $| = 1; print "1..25\n"; }
8 END {print "not ok 1\n" unless $loaded;}
10 use Time::HiRes qw(tv_interval);
18 my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
19 my $have_usleep = defined &Time::HiRes::usleep;
20 my $have_ualarm = defined &Time::HiRes::ualarm;
21 my $have_time = defined &Time::HiRes::time;
23 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
24 import Time::HiRes 'usleep' if $have_usleep;
25 import Time::HiRes 'ualarm' if $have_ualarm;
29 # Ideally, we'd like to test that the timers are rather precise.
30 # However, if the system is busy, there are no guarantees on how
31 # quickly we will return. This limit used to be 10%, but that
32 # was occasionally triggered falsely.
34 # Another possibility might be to print "ok" if the test completes fine
35 # with (say) 10% slosh, "skip - system may have been busy?" if the test
36 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
37 # consider changing over to test.pl at the same time.
38 # --A.D., Nov 27, 2001
39 my $limit = 0.20; # 20% is acceptable slosh for testing timers
42 map { print "ok $_ (skipped)\n" } @_;
46 my ($n, $result, @info) = @_;
52 print "# @info\n" if @info;
56 if (!$have_gettimeofday) {
60 my @one = gettimeofday();
61 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
62 ok 3, $one[0] > 850_000_000, "@one too small";
66 my @two = gettimeofday();
67 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
68 "@two is not greater than @one";
70 my $f = Time::HiRes::time();
71 ok 5, $f > 850_000_000, "$f too small";
72 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
84 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
86 if (!$have_gettimeofday) {
90 my $f = Time::HiRes::time();
92 my $f2 = Time::HiRes::time();
94 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
98 # Two-arg tv_interval() is always available.
100 my $f = tv_interval [5, 100_000], [10, 500_000];
101 ok 9, abs($f - 5.4) < 0.001, $f;
104 if (!$have_gettimeofday) {
108 my $r = [gettimeofday()];
109 my $f = tv_interval $r;
113 if (!$have_usleep || !$have_gettimeofday) {
117 my $r = [gettimeofday()];
118 #jTime::HiRes::sleep 0.5;
119 Time::HiRes::sleep( 0.5 );
120 my $f = tv_interval $r;
121 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
129 local $SIG{ALRM} = sub { $tick++ };
131 # This was previously written sleep 3 until $tick;
132 # But there is a small race condition here: the alarm may go off
133 # until ($tick) { Here!; sleep }
134 # In which case the sleep is forever.
135 # sleeping for 3 seconds will cause the test to fail but it's better than
136 # infinite hang. [Until someone produces a platform where sleep interferes
137 # with ualarm, in which case a more sophisticated self destruct will need
138 # to be written (eg fork, child sleeps for a long time, child kills parent
139 # if parent doesn't finish first (killing child))]
141 my $one = time; $tick = 0; ualarm(10_000); sleep 3 until $tick;
142 my $two = time; $tick = 0; ualarm(10_000); sleep 3 until $tick;
144 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
147 ualarm(10_000, 10_000);
148 sleep 3 until $tick >= 3;
153 # new test: did we even get close?
159 for my $i (1 .. 100) {
160 $s += Time::HiRes::time() - time();
163 # $s should be, at worst, equal to $n
164 # (time() may be rounding down, up, or closest)
165 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
166 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
169 unless (defined &Time::HiRes::gettimeofday
170 && defined &Time::HiRes::ualarm
171 && defined &Time::HiRes::usleep
172 && $Config{d_ualarm}) {
174 print "ok $_ # skipped\n";
177 use Time::HiRes qw (time alarm sleep);
179 my ($f, $r, $i, $not, $ok);
182 print "# time...$f\n";
185 $r = [Time::HiRes::gettimeofday()];
187 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
189 $r = [Time::HiRes::gettimeofday()];
195 select (undef, undef, undef, 3);
196 my $ival = Time::HiRes::tv_interval ($r);
197 print "# Select returned! $i $ival\n";
198 print "# ", abs($ival/3 - 1), "\n";
199 # Whether select() gets restarted after signals is
200 # implementation dependent. If it is restarted, we
201 # will get about 3.3 seconds: 3 from the select, 0.3
202 # from the alarm. If this happens, let's just skip
203 # this particular test. --jhi
204 if (abs($ival/3.3 - 1) < $limit) {
205 $ok = "Skip: your select() seems to get restarted by your SIGALRM";
209 my $exp = 0.3 * (5 - $i);
210 # This test is more sensitive, so impose a softer limit.
211 if (abs($ival/$exp - 1) > 3*$limit) {
212 my $ratio = abs($ival/$exp);
213 $not = "while: $exp sleep took $ival ratio $ratio";
222 my $ival = Time::HiRes::tv_interval ($r);
223 print "# Tick! $i $ival\n";
224 my $exp = 0.3 * (5 - $i);
225 # This test is more sensitive, so impose a softer limit.
226 if (abs($ival/$exp - 1) > 3*$limit) {
227 my $ratio = abs($ival/$exp);
228 $not = "tick: $exp sleep took $ival ratio $ratio";
233 alarm(0); # can't cancel usig %SIG
235 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
238 unless (defined &Time::HiRes::setitimer
239 && defined &Time::HiRes::getitimer
240 && exists &Time::HiRes::ITIMER_VIRTUAL
242 && $Config{sig_name} =~ m/\bVTALRM\b/) {
244 print "ok $_ # Skip: no virtual interval timers\n";
247 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
250 my $r = [Time::HiRes::gettimeofday()];
253 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
254 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
257 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
259 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
260 print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
263 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
265 while (getitimer(ITIMER_VIRTUAL)) {
266 my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
269 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
271 print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
274 $SIG{VTALRM} = 'DEFAULT';
277 if ($have_gettimeofday) {
280 my $sleep = 1.5; # seconds
283 $t0 = gettimeofday();
284 $a = abs(sleep($sleep) / $sleep - 1.0);
285 $td = gettimeofday() - $t0;
286 my $ratio = 1.0 + $a;
288 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
290 if ($td < $sleep * (1 + $limit)) {
291 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
293 print "ok 20 # Skip: $msg";
296 $t0 = gettimeofday();
297 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
298 $td = gettimeofday() - $t0;
301 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
303 if ($td < $sleep * (1 + $limit)) {
304 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
306 print "ok 21 # Skip: $msg";
311 print "ok $_ # Skip: no gettimeofday\n";
316 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
317 "ok 22\n" : "not ok 22\n";
320 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
321 "ok 23\n" : "not ok 23\n";
325 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
326 "ok 24\n" : "not ok 24\n";
329 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
330 "ok 25\n" : "not ok 25\n";