Rework Time::HiRes not to need HAS_NANOSLEEP from Configure.
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.t
1 #!./perl -w
2
3 BEGIN {
4     if ($ENV{PERL_CORE}) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8 }
9
10 BEGIN { $| = 1; print "1..25\n"; }
11
12 END {print "not ok 1\n" unless $loaded;}
13
14 use Time::HiRes qw(tv_interval);
15
16 $loaded = 1;
17
18 print "ok 1\n";
19
20 use strict;
21
22 my $have_gettimeofday   = defined &Time::HiRes::gettimeofday;
23 my $have_usleep         = defined &Time::HiRes::usleep;
24 my $have_ualarm         = defined &Time::HiRes::ualarm;
25 my $have_time           = defined &Time::HiRes::time;
26
27 import Time::HiRes 'gettimeofday'       if $have_gettimeofday;
28 import Time::HiRes 'usleep'             if $have_usleep;
29 import Time::HiRes 'ualarm'             if $have_ualarm;
30
31 use Config;
32
33 my $xdefine = ''; 
34
35 if (open(XDEFINE, "xdefine")) {
36     chomp($xdefine = <XDEFINE>);
37     close(XDEFINE);
38 }
39
40 # Ideally, we'd like to test that the timers are rather precise.
41 # However, if the system is busy, there are no guarantees on how
42 # quickly we will return.  This limit used to be 10%, but that
43 # was occasionally triggered falsely.  
44 # Try 20%.  
45 # Another possibility might be to print "ok" if the test completes fine
46 # with (say) 10% slosh, "skip - system may have been busy?" if the test
47 # completes fine with (say) 30% slosh, and fail otherwise.  If you do that,
48 # consider changing over to test.pl at the same time.
49 # --A.D., Nov 27, 2001
50 my $limit = 0.20; # 20% is acceptable slosh for testing timers
51
52 sub skip {
53     map { print "ok $_ # skipped\n" } @_;
54 }
55
56 sub ok {
57     my ($n, $result, @info) = @_;
58     if ($result) {
59         print "ok $n\n";
60     }
61     else {
62         print "not ok $n\n";
63         print "# @info\n" if @info;
64     }
65 }
66
67 if (!$have_gettimeofday) {
68     skip 2..6;
69 }
70 else {
71     my @one = gettimeofday();
72     ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
73     ok 3, $one[0] > 850_000_000, "@one too small";
74
75     sleep 1;
76
77     my @two = gettimeofday();
78     ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
79             "@two is not greater than @one";
80
81     my $f = Time::HiRes::time();
82     ok 5, $f > 850_000_000, "$f too small";
83     ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
84 }
85
86 if (!$have_usleep) {
87     skip 7..8;
88 }
89 else {
90     my $one = time;
91     usleep(10_000);
92     my $two = time;
93     usleep(10_000);
94     my $three = time;
95     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
96
97     if (!$have_gettimeofday) {
98         skip 8;
99     }
100     else {
101         my $f = Time::HiRes::time();
102         usleep(500_000);
103         my $f2 = Time::HiRes::time();
104         my $d = $f2 - $f;
105         ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
106     }
107 }
108
109 # Two-arg tv_interval() is always available.
110 {
111     my $f = tv_interval [5, 100_000], [10, 500_000];
112     ok 9, abs($f - 5.4) < 0.001, $f;
113 }
114
115 if (!$have_gettimeofday) {
116     skip 10;
117 }
118 else {
119     my $r = [gettimeofday()];
120     my $f = tv_interval $r;
121     ok 10, $f < 2, $f;
122 }
123
124 if (!$have_usleep || !$have_gettimeofday) {
125     skip 11;
126 }
127 else {
128     my $r = [gettimeofday()];
129     Time::HiRes::sleep( 0.5 );
130     my $f = tv_interval $r;
131     ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
132 }
133
134 if (!$have_ualarm) {
135     skip 12..13;
136 }
137 else {
138     my $tick = 0;
139     local $SIG{ALRM} = sub { $tick++ };
140
141     my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
142     my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
143     my $three = time;
144     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
145
146     $tick = 0;
147     ualarm(10_000, 10_000);
148     while ($tick < 3) { sleep }
149     ok 13, 1;
150     ualarm(0);
151 }
152
153 # new test: did we even get close?
154
155 if (!$have_time) {
156     skip 14
157 } else {
158  my ($s, $n);
159  for my $i (1 .. 100) {
160      $s += Time::HiRes::time() - time();
161      $n++;
162  }
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";
167 }
168
169 my $has_ualarm = $Config{d_ualarm};
170
171 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
172
173 unless (   defined &Time::HiRes::gettimeofday
174         && defined &Time::HiRes::ualarm
175         && defined &Time::HiRes::usleep
176         && $has_ualarm) {
177     for (15..17) {
178         print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
179     }
180 } else {
181     use Time::HiRes qw (time alarm sleep);
182
183     my ($f, $r, $i, $not, $ok);
184
185     $f = time; 
186     print "# time...$f\n";
187     print "ok 15\n";
188
189     $r = [Time::HiRes::gettimeofday()];
190     sleep (0.5);
191     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
192
193     $r = [Time::HiRes::gettimeofday()];
194     $i = 5;
195     $SIG{ALRM} = "tick";
196     while ($i > 0)
197     {
198         alarm(0.3);
199         select (undef, undef, undef, 3);
200         my $ival = Time::HiRes::tv_interval ($r);
201         print "# Select returned! $i $ival\n";
202         print "# ", abs($ival/3 - 1), "\n";
203         # Whether select() gets restarted after signals is
204         # implementation dependent.  If it is restarted, we
205         # will get about 3.3 seconds: 3 from the select, 0.3
206         # from the alarm.  If this happens, let's just skip
207         # this particular test.  --jhi
208         if (abs($ival/3.3 - 1) < $limit) {
209             $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
210             undef $not;
211             last;
212         }
213         my $exp = 0.3 * (5 - $i);
214         # This test is more sensitive, so impose a softer limit.
215         if (abs($ival/$exp - 1) > 3*$limit) {
216             my $ratio = abs($ival/$exp);
217             $not = "while: $exp sleep took $ival ratio $ratio";
218             last;
219         }
220         $ok = $i;
221     }
222
223     sub tick
224     {
225         $i--;
226         my $ival = Time::HiRes::tv_interval ($r);
227         print "# Tick! $i $ival\n";
228         my $exp = 0.3 * (5 - $i);
229         # This test is more sensitive, so impose a softer limit.
230         if (abs($ival/$exp - 1) > 3*$limit) {
231             my $ratio = abs($ival/$exp);
232             $not = "tick: $exp sleep took $ival ratio $ratio";
233             $i = 0;
234         }
235     }
236
237     alarm(0); # can't cancel usig %SIG
238
239     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
240 }
241
242 unless (   defined &Time::HiRes::setitimer
243         && defined &Time::HiRes::getitimer
244         && eval    'Time::HiRes::ITIMER_VIRTUAL'
245         && $Config{d_select}
246         && $Config{sig_name} =~ m/\bVTALRM\b/) {
247     for (18..19) {
248         print "ok $_ # Skip: no virtual interval timers\n";
249     }
250 } else {
251     use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
252
253     my $i = 3;
254     my $r = [Time::HiRes::gettimeofday()];
255
256     $SIG{VTALRM} = sub {
257         $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
258         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
259     };  
260
261     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
262
263     # Assume interval timer granularity of $limit * 0.5 seconds.  Too bold?
264     print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
265     print "ok 18\n";
266
267     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
268
269     while (getitimer(ITIMER_VIRTUAL)) {
270         my $j;
271         for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
272     }
273
274     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
275
276     print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
277     print "ok 19\n";
278
279     $SIG{VTALRM} = 'DEFAULT';
280 }
281
282 if ($have_gettimeofday) {
283     my ($t0, $td);
284
285     my $sleep = 1.5; # seconds
286     my $msg;
287
288     $t0 = gettimeofday();
289     $a = abs(sleep($sleep)        / $sleep         - 1.0);
290     $td = gettimeofday() - $t0;
291     my $ratio = 1.0 + $a;
292
293     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
294
295     if ($td < $sleep * (1 + $limit)) {
296         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
297     } else {
298         print "ok 20 # Skip: $msg";
299     }
300
301     $t0 = gettimeofday();
302     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
303     $td = gettimeofday() - $t0;
304     $ratio = 1.0 + $a;
305
306     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
307
308     if ($td < $sleep * (1 + $limit)) {
309         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
310     } else {
311         print "ok 21 # Skip: $msg";
312     }
313
314 } else {
315     for (20..21) {
316         print "ok $_ # Skip: no gettimeofday\n";
317     }
318 }
319
320 eval { sleep(-1) };
321 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
322     "ok 22\n" : "not ok 22\n";
323
324 eval { usleep(-2) };
325 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
326     "ok 23\n" : "not ok 23\n";
327
328 if ($have_ualarm) {
329     eval { alarm(-3) };
330     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
331         "ok 24\n" : "not ok 24\n";
332
333     eval { ualarm(-4) };
334     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
335     "ok 25\n" : "not ok 25\n";
336 } else {
337     skip 24;
338     skip 25;
339 }