Make Archive::Tar clean up its test files on Win32
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / t / HiRes.t
CommitLineData
88cb3de1 1#!./perl -w
2
dcf686c9 3BEGIN {
3f2ee006 4 if ($ENV{PERL_CORE}) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
df2f6354 7 require Config; import Config;
8 if (" $Config{'extensions'} " !~ m[ Time/HiRes ]) {
9 print "1..0 # Skip -- Perl configured without Time::HiRes module\n";
10 exit 0;
11 }
3f2ee006 12 }
dcf686c9 13}
14
44d3ce20 15BEGIN { $| = 1; print "1..28\n"; }
dcf686c9 16
17END {print "not ok 1\n" unless $loaded;}
18
19use Time::HiRes qw(tv_interval);
20
21$loaded = 1;
22
23print "ok 1\n";
24
25use strict;
26
27my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
28my $have_usleep = defined &Time::HiRes::usleep;
44d3ce20 29my $have_nanosleep = defined &Time::HiRes::nanosleep;
dcf686c9 30my $have_ualarm = defined &Time::HiRes::ualarm;
c9ff6e92 31my $have_time = defined &Time::HiRes::time;
dcf686c9 32
33import Time::HiRes 'gettimeofday' if $have_gettimeofday;
34import Time::HiRes 'usleep' if $have_usleep;
44d3ce20 35import Time::HiRes 'nanosleep' if $have_nanosleep;
dcf686c9 36import Time::HiRes 'ualarm' if $have_ualarm;
37
3c72ec00 38use Config;
39
690f7c5f 40my $have_alarm = $Config{d_alarm};
41my $have_fork = $Config{d_fork};
42my $waitfor = 60; # 10 seconds is normal.
43my $pid;
44
45if ($have_fork) {
44d3ce20 46 print "# I am process $$, starting the timer process\n";
690f7c5f 47 if (defined ($pid = fork())) {
48 if ($pid == 0) { # We are the kid, set up the timer.
44d3ce20 49 print "# I am timer process $$\n";
690f7c5f 50 sleep($waitfor);
b4c5c611 51 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded\n";
690f7c5f 52 print "# Terminating the testing process\n";
53 kill('TERM', getppid());
54 print "# Timer process exiting\n";
55 exit(0);
56 }
57 } else {
58 warn "$0: fork failed: $!\n";
59 }
60} else {
61 print "# No timer process\n";
62}
63
12724655 64my $xdefine = '';
3f2ee006 65
66if (open(XDEFINE, "xdefine")) {
67 chomp($xdefine = <XDEFINE>);
68 close(XDEFINE);
69}
70
0740bb5b 71# Ideally, we'd like to test that the timers are rather precise.
72# However, if the system is busy, there are no guarantees on how
73# quickly we will return. This limit used to be 10%, but that
74# was occasionally triggered falsely.
75# Try 20%.
76# Another possibility might be to print "ok" if the test completes fine
77# with (say) 10% slosh, "skip - system may have been busy?" if the test
78# completes fine with (say) 30% slosh, and fail otherwise. If you do that,
79# consider changing over to test.pl at the same time.
80# --A.D., Nov 27, 2001
81my $limit = 0.20; # 20% is acceptable slosh for testing timers
82
dcf686c9 83sub skip {
3f2ee006 84 map { print "ok $_ # skipped\n" } @_;
dcf686c9 85}
86
87sub ok {
88 my ($n, $result, @info) = @_;
89 if ($result) {
90 print "ok $n\n";
91 }
92 else {
93 print "not ok $n\n";
94 print "# @info\n" if @info;
95 }
96}
97
98if (!$have_gettimeofday) {
99 skip 2..6;
100}
101else {
102 my @one = gettimeofday();
103 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
104 ok 3, $one[0] > 850_000_000, "@one too small";
105
106 sleep 1;
107
108 my @two = gettimeofday();
109 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
110 "@two is not greater than @one";
111
c9ff6e92 112 my $f = Time::HiRes::time();
dcf686c9 113 ok 5, $f > 850_000_000, "$f too small";
0740bb5b 114 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
dcf686c9 115}
116
117if (!$have_usleep) {
118 skip 7..8;
119}
120else {
121 my $one = time;
122 usleep(10_000);
123 my $two = time;
124 usleep(10_000);
125 my $three = time;
126 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
127
128 if (!$have_gettimeofday) {
129 skip 8;
130 }
131 else {
c9ff6e92 132 my $f = Time::HiRes::time();
dcf686c9 133 usleep(500_000);
c9ff6e92 134 my $f2 = Time::HiRes::time();
dcf686c9 135 my $d = $f2 - $f;
0740bb5b 136 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
dcf686c9 137 }
138}
139
140# Two-arg tv_interval() is always available.
141{
142 my $f = tv_interval [5, 100_000], [10, 500_000];
6cf89afa 143 ok 9, abs($f - 5.4) < 0.001, $f;
dcf686c9 144}
145
146if (!$have_gettimeofday) {
147 skip 10;
148}
149else {
150 my $r = [gettimeofday()];
151 my $f = tv_interval $r;
152 ok 10, $f < 2, $f;
153}
154
c9ff6e92 155if (!$have_usleep || !$have_gettimeofday) {
dcf686c9 156 skip 11;
157}
158else {
159 my $r = [gettimeofday()];
dcf686c9 160 Time::HiRes::sleep( 0.5 );
161 my $f = tv_interval $r;
0740bb5b 162 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
dcf686c9 163}
164
690f7c5f 165if (!$have_ualarm || !$have_alarm) {
dcf686c9 166 skip 12..13;
167}
168else {
169 my $tick = 0;
64a7a97c 170 local $SIG{ ALRM } = sub { $tick++ };
dcf686c9 171
64a7a97c 172 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
173 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
dcf686c9 174 my $three = time;
175 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
64a7a97c 176 print "# tick = $tick, one = $one, two = $two, three = $three\n";
dcf686c9 177
64a7a97c 178 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
dcf686c9 179 ok 13, 1;
180 ualarm(0);
64a7a97c 181 print "# tick = $tick, one = $one, two = $two, three = $three\n";
dcf686c9 182}
183
64a7a97c 184# Did we even get close?
dcf686c9 185
c9ff6e92 186if (!$have_time) {
64a7a97c 187 skip 14;
c9ff6e92 188} else {
1fbb4de4 189 my ($s, $n, $i) = (0);
190 for $i (1 .. 100) {
456811e8 191 $s += Time::HiRes::time() - time();
192 $n++;
64cc9c1c 193 }
456811e8 194 # $s should be, at worst, equal to $n
195 # (time() may be rounding down, up, or closest)
196 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
197 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
dcf686c9 198}
199
3f2ee006 200my $has_ualarm = $Config{d_ualarm};
201
202$has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
203
204unless ( defined &Time::HiRes::gettimeofday
dcf686c9 205 && defined &Time::HiRes::ualarm
c7f7de79 206 && defined &Time::HiRes::usleep
3f2ee006 207 && $has_ualarm) {
dcf686c9 208 for (15..17) {
3f2ee006 209 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
dcf686c9 210 }
211} else {
212 use Time::HiRes qw (time alarm sleep);
213
cb203acc 214 my ($f, $r, $i, $not, $ok);
dcf686c9 215
dcf686c9 216 $f = time;
add58217 217 print "# time...$f\n";
218 print "ok 15\n";
dcf686c9 219
c9ff6e92 220 $r = [Time::HiRes::gettimeofday()];
dcf686c9 221 sleep (0.5);
add58217 222 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
dcf686c9 223
c9ff6e92 224 $r = [Time::HiRes::gettimeofday()];
dcf686c9 225 $i = 5;
226 $SIG{ALRM} = "tick";
6ea166ef 227 while ($i > 0)
dcf686c9 228 {
0e172911 229 alarm(0.3);
c7f7de79 230 select (undef, undef, undef, 3);
231 my $ival = Time::HiRes::tv_interval ($r);
232 print "# Select returned! $i $ival\n";
cb203acc 233 print "# ", abs($ival/3 - 1), "\n";
234 # Whether select() gets restarted after signals is
235 # implementation dependent. If it is restarted, we
236 # will get about 3.3 seconds: 3 from the select, 0.3
237 # from the alarm. If this happens, let's just skip
238 # this particular test. --jhi
239 if (abs($ival/3.3 - 1) < $limit) {
3f2ee006 240 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
cb203acc 241 undef $not;
242 last;
243 }
c7f7de79 244 my $exp = 0.3 * (5 - $i);
0740bb5b 245 # This test is more sensitive, so impose a softer limit.
cb203acc 246 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 247 my $ratio = abs($ival/$exp);
248 $not = "while: $exp sleep took $ival ratio $ratio";
c7f7de79 249 last;
250 }
cb203acc 251 $ok = $i;
dcf686c9 252 }
253
254 sub tick
255 {
dcf686c9 256 $i--;
c7f7de79 257 my $ival = Time::HiRes::tv_interval ($r);
258 print "# Tick! $i $ival\n";
259 my $exp = 0.3 * (5 - $i);
0740bb5b 260 # This test is more sensitive, so impose a softer limit.
cb203acc 261 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 262 my $ratio = abs($ival/$exp);
263 $not = "tick: $exp sleep took $ival ratio $ratio";
c7f7de79 264 $i = 0;
265 }
dcf686c9 266 }
c7f7de79 267
d0b83b2b 268 alarm(0); # can't cancel usig %SIG
dcf686c9 269
cb203acc 270 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
dcf686c9 271}
272
3f2ee006 273unless ( defined &Time::HiRes::setitimer
3c72ec00 274 && defined &Time::HiRes::getitimer
3f2ee006 275 && eval 'Time::HiRes::ITIMER_VIRTUAL'
d0b83b2b 276 && $Config{d_select}
277 && $Config{sig_name} =~ m/\bVTALRM\b/) {
3c72ec00 278 for (18..19) {
0e172911 279 print "ok $_ # Skip: no virtual interval timers\n";
3c72ec00 280 }
281} else {
282 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
283
284 my $i = 3;
c9ff6e92 285 my $r = [Time::HiRes::gettimeofday()];
3c72ec00 286
287 $SIG{VTALRM} = sub {
288 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
289 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
290 };
291
0e172911 292 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
293
0740bb5b 294 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
4ed0e2d4 295 my $virt = getitimer(ITIMER_VIRTUAL);
296 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
0e172911 297 print "ok 18\n";
3c72ec00 298
299 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
300
1c41c9bf 301 while (getitimer(ITIMER_VIRTUAL)) {
3f2ee006 302 my $j;
303 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
3c72ec00 304 }
305
306 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
307
4ed0e2d4 308 $virt = getitimer(ITIMER_VIRTUAL);
309 print "not " unless defined $virt && $virt == 0;
0e172911 310 print "ok 19\n";
311
3c72ec00 312 $SIG{VTALRM} = 'DEFAULT';
313}
314
9c735d59 315if ($have_gettimeofday) {
316 my ($t0, $td);
52d72fba 317
9c735d59 318 my $sleep = 1.5; # seconds
9c735d59 319 my $msg;
320
321 $t0 = gettimeofday();
322 $a = abs(sleep($sleep) / $sleep - 1.0);
323 $td = gettimeofday() - $t0;
0740bb5b 324 my $ratio = 1.0 + $a;
9c735d59 325
0740bb5b 326 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 327
328 if ($td < $sleep * (1 + $limit)) {
329 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
330 } else {
331 print "ok 20 # Skip: $msg";
332 }
333
334 $t0 = gettimeofday();
335 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
336 $td = gettimeofday() - $t0;
0740bb5b 337 $ratio = 1.0 + $a;
9c735d59 338
0740bb5b 339 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 340
341 if ($td < $sleep * (1 + $limit)) {
342 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
343 } else {
344 print "ok 21 # Skip: $msg";
345 }
346
347} else {
348 for (20..21) {
349 print "ok $_ # Skip: no gettimeofday\n";
350 }
351}
52d72fba 352
44d3ce20 353if (!$have_nanosleep) {
354 skip 22..23;
355}
356else {
357 my $one = CORE::time;
358 nanosleep(10_000_000);
359 my $two = CORE::time;
360 nanosleep(10_000_000);
361 my $three = CORE::time;
362 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
363
364 if (!$have_gettimeofday) {
365 skip 23;
366 }
367 else {
368 my $f = Time::HiRes::time();
369 nanosleep(500_000_000);
370 my $f2 = Time::HiRes::time();
371 my $d = $f2 - $f;
372 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
373 }
374}
375
f7916ddb 376eval { sleep(-1) };
377print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
44d3ce20 378 "ok 24\n" : "not ok 24\n";
f7916ddb 379
380eval { usleep(-2) };
381print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
44d3ce20 382 "ok 25\n" : "not ok 25\n";
f7916ddb 383
ec77158c 384if ($have_ualarm) {
c46d5038 385 eval { alarm(-3) };
386 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
44d3ce20 387 "ok 26\n" : "not ok 26\n";
c46d5038 388
ec77158c 389 eval { ualarm(-4) };
390 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
44d3ce20 391 "ok 27\n" : "not ok 27\n";
392} else {
393 skip 26;
394 skip 27;
395}
396
397if ($have_nanosleep) {
398 eval { nanosleep(-5) };
399 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
400 "ok 28\n" : "not ok 28\n";
ec77158c 401} else {
44d3ce20 402 skip 28;
ec77158c 403}
690f7c5f 404
405if (defined $pid) {
44d3ce20 406 print "# I am process $$, terminating the timer process $pid\n";
690f7c5f 407 kill('TERM', $pid); # We are done, the timer can go.
64a7a97c 408 unlink("ktrace.out");
690f7c5f 409}
410