Upgrade to Time::HiRes 1.72
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / t / HiRes.t
1 #!./perl -w
2
3 BEGIN {
4     if ($ENV{PERL_CORE}) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
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         }
12     }
13 }
14
15 BEGIN { $| = 1; print "1..28\n"; }
16
17 END { print "not ok 1\n" unless $loaded }
18
19 use Time::HiRes qw(tv_interval);
20
21 $loaded = 1;
22
23 print "ok 1\n";
24
25 use strict;
26
27 my $have_gettimeofday   = defined &Time::HiRes::gettimeofday;
28 my $have_usleep         = defined &Time::HiRes::usleep;
29 my $have_nanosleep      = defined &Time::HiRes::nanosleep;
30 my $have_ualarm         = defined &Time::HiRes::ualarm;
31 my $have_time           = defined &Time::HiRes::time;
32
33 import Time::HiRes 'gettimeofday'       if $have_gettimeofday;
34 import Time::HiRes 'usleep'             if $have_usleep;
35 import Time::HiRes 'nanosleep'          if $have_nanosleep;
36 import Time::HiRes 'ualarm'             if $have_ualarm;
37
38 use Config;
39
40 my $have_alarm = $Config{d_alarm};
41 my $have_fork  = $Config{d_fork};
42 my $waitfor = 60; # 10 seconds is normal.
43 my $pid;
44
45 if ($have_fork) {
46     print "# I am process $$, starting the timer process\n";
47     if (defined ($pid = fork())) {
48         if ($pid == 0) { # We are the kid, set up the timer.
49             print "# I am timer process $$\n";
50             sleep($waitfor);
51             warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded\n";
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
64 my $xdefine = ''; 
65
66 if (open(XDEFINE, "xdefine")) {
67     chomp($xdefine = <XDEFINE>);
68     close(XDEFINE);
69 }
70
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
81 my $limit = 0.20; # 20% is acceptable slosh for testing timers
82
83 sub skip {
84     map { print "ok $_ # skipped\n" } @_;
85 }
86
87 sub 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
98 if (!$have_gettimeofday) {
99     skip 2..6;
100 }
101 else {
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
112     my $f = Time::HiRes::time();
113     ok 5, $f > 850_000_000, "$f too small";
114     ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
115 }
116
117 if (!$have_usleep) {
118     skip 7..8;
119 }
120 else {
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 {
132         my $f = Time::HiRes::time();
133         usleep(500_000);
134         my $f2 = Time::HiRes::time();
135         my $d = $f2 - $f;
136         ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
137     }
138 }
139
140 # Two-arg tv_interval() is always available.
141 {
142     my $f = tv_interval [5, 100_000], [10, 500_000];
143     ok 9, abs($f - 5.4) < 0.001, $f;
144 }
145
146 if (!$have_gettimeofday) {
147     skip 10;
148 }
149 else {
150     my $r = [gettimeofday()];
151     my $f = tv_interval $r;
152     ok 10, $f < 2, $f;
153 }
154
155 if (!$have_usleep || !$have_gettimeofday) {
156     skip 11;
157 }
158 else {
159     my $r = [gettimeofday()];
160     Time::HiRes::sleep( 0.5 );
161     my $f = tv_interval $r;
162     ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
163 }
164
165 if (!$have_ualarm || !$have_alarm) {
166     skip 12..13;
167 }
168 else {
169     my $tick = 0;
170     local $SIG{ ALRM } = sub { $tick++ };
171
172     my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
173     my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
174     my $three = time;
175     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
176     print "# tick = $tick, one = $one, two = $two, three = $three\n";
177
178     $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
179     ok 13, 1;
180     ualarm(0);
181     print "# tick = $tick, one = $one, two = $two, three = $three\n";
182 }
183
184 # Did we even get close?
185
186 if (!$have_time) {
187     skip 14;
188 } else {
189  my ($s, $n, $i) = (0);
190  for $i (1 .. 100) {
191      $s += Time::HiRes::time() - time();
192      $n++;
193  }
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";
198 }
199
200 my $has_ualarm = $Config{d_ualarm};
201
202 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
203
204 unless (   defined &Time::HiRes::gettimeofday
205         && defined &Time::HiRes::ualarm
206         && defined &Time::HiRes::usleep
207         && $has_ualarm) {
208     for (15..17) {
209         print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
210     }
211 } else {
212     use Time::HiRes qw (time alarm sleep);
213
214     my ($f, $r, $i, $not, $ok);
215
216     $f = time; 
217     print "# time...$f\n";
218     print "ok 15\n";
219
220     $r = [Time::HiRes::gettimeofday()];
221     sleep (0.5);
222     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
223
224     $r = [Time::HiRes::gettimeofday()];
225     $i = 5;
226     $SIG{ALRM} = "tick";
227     while ($i > 0)
228     {
229         alarm(0.3);
230         select (undef, undef, undef, 3);
231         my $ival = Time::HiRes::tv_interval ($r);
232         print "# Select returned! $i $ival\n";
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) {
240             $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
241             undef $not;
242             last;
243         }
244         my $exp = 0.3 * (5 - $i);
245         # This test is more sensitive, so impose a softer limit.
246         if (abs($ival/$exp - 1) > 3*$limit) {
247             my $ratio = abs($ival/$exp);
248             $not = "while: $exp sleep took $ival ratio $ratio";
249             last;
250         }
251         $ok = $i;
252     }
253
254     sub tick
255     {
256         $i--;
257         my $ival = Time::HiRes::tv_interval ($r);
258         print "# Tick! $i $ival\n";
259         my $exp = 0.3 * (5 - $i);
260         # This test is more sensitive, so impose a softer limit.
261         if (abs($ival/$exp - 1) > 3*$limit) {
262             my $ratio = abs($ival/$exp);
263             $not = "tick: $exp sleep took $ival ratio $ratio";
264             $i = 0;
265         }
266     }
267
268     alarm(0); # can't cancel usig %SIG
269
270     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
271 }
272
273 unless (   defined &Time::HiRes::setitimer
274         && defined &Time::HiRes::getitimer
275         && eval    'Time::HiRes::ITIMER_VIRTUAL'
276         && $Config{d_select}
277         && $Config{sig_name} =~ m/\bVTALRM\b/) {
278     for (18..19) {
279         print "ok $_ # Skip: no virtual interval timers\n";
280     }
281 } else {
282     use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
283
284     my $i = 3;
285     my $r = [Time::HiRes::gettimeofday()];
286
287     $SIG{VTALRM} = sub {
288         $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
289         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
290     };  
291
292     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
293
294     # Assume interval timer granularity of $limit * 0.5 seconds.  Too bold?
295     my $virt = getitimer(ITIMER_VIRTUAL);
296     print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
297     print "ok 18\n";
298
299     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
300
301     while (getitimer(ITIMER_VIRTUAL)) {
302         my $j;
303         for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
304     }
305
306     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
307
308     $virt = getitimer(ITIMER_VIRTUAL);
309     print "not " unless defined $virt && $virt == 0;
310     print "ok 19\n";
311
312     $SIG{VTALRM} = 'DEFAULT';
313 }
314
315 if ($have_gettimeofday) {
316     my ($t0, $td);
317
318     my $sleep = 1.5; # seconds
319     my $msg;
320
321     $t0 = gettimeofday();
322     $a = abs(sleep($sleep)        / $sleep         - 1.0);
323     $td = gettimeofday() - $t0;
324     my $ratio = 1.0 + $a;
325
326     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
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;
337     $ratio = 1.0 + $a;
338
339     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
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 }
352
353 if (!$have_nanosleep) {
354     skip 22..23;
355 }
356 else {
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
376 eval { sleep(-1) };
377 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
378     "ok 24\n" : "not ok 24\n";
379
380 eval { usleep(-2) };
381 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
382     "ok 25\n" : "not ok 25\n";
383
384 if ($have_ualarm) {
385     eval { alarm(-3) };
386     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
387         "ok 26\n" : "not ok 26\n";
388
389     eval { ualarm(-4) };
390     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
391     "ok 27\n" : "not ok 27\n";
392 } else {
393     skip 26;
394     skip 27;
395 }
396
397 if ($have_nanosleep) {
398     eval { nanosleep(-5) };
399     print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
400         "ok 28\n" : "not ok 28\n";
401 } else {
402     skip 28;
403 }
404
405 if (defined $pid) {
406     print "# I am process $$, terminating the timer process $pid\n";
407     kill('TERM', $pid); # We are done, the timer can go.
408     unlink("ktrace.out");
409 }
410