[ID 20011201.170] Time::HiRes in devel-perl causes segfaults for xs users
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.t
1 BEGIN {
2     chdir 't' if -d 't';
3     @INC = '../lib';
4 }
5
6 BEGIN { $| = 1; print "1..25\n"; }
7
8 END {print "not ok 1\n" unless $loaded;}
9
10 use Time::HiRes qw(tv_interval);
11
12 $loaded = 1;
13
14 print "ok 1\n";
15
16 use strict;
17
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;
22
23 import Time::HiRes 'gettimeofday'       if $have_gettimeofday;
24 import Time::HiRes 'usleep'             if $have_usleep;
25 import Time::HiRes 'ualarm'             if $have_ualarm;
26
27 use Config;
28
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.  
33 # Try 20%.  
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
40
41 sub skip {
42     map { print "ok $_ (skipped)\n" } @_;
43 }
44
45 sub ok {
46     my ($n, $result, @info) = @_;
47     if ($result) {
48         print "ok $n\n";
49     }
50     else {
51         print "not ok $n\n";
52         print "# @info\n" if @info;
53     }
54 }
55
56 if (!$have_gettimeofday) {
57     skip 2..6;
58 }
59 else {
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";
63
64     sleep 1;
65
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";
69
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";
73 }
74
75 if (!$have_usleep) {
76     skip 7..8;
77 }
78 else {
79     my $one = time;
80     usleep(10_000);
81     my $two = time;
82     usleep(10_000);
83     my $three = time;
84     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
85
86     if (!$have_gettimeofday) {
87         skip 8;
88     }
89     else {
90         my $f = Time::HiRes::time();
91         usleep(500_000);
92         my $f2 = Time::HiRes::time();
93         my $d = $f2 - $f;
94         ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
95     }
96 }
97
98 # Two-arg tv_interval() is always available.
99 {
100     my $f = tv_interval [5, 100_000], [10, 500_000];
101     ok 9, abs($f - 5.4) < 0.001, $f;
102 }
103
104 if (!$have_gettimeofday) {
105     skip 10;
106 }
107 else {
108     my $r = [gettimeofday()];
109     my $f = tv_interval $r;
110     ok 10, $f < 2, $f;
111 }
112
113 if (!$have_usleep || !$have_gettimeofday) {
114     skip 11;
115 }
116 else {
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.";
122 }
123
124 if (!$have_ualarm) {
125     skip 12..13;
126 }
127 else {
128     my $tick = 0;
129     local $SIG{ALRM} = sub { $tick++ };
130
131     my $one = time; $tick = 0; ualarm(10_000); sleep until $tick;
132     my $two = time; $tick = 0; ualarm(10_000); sleep until $tick;
133     my $three = time;
134     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
135
136     $tick = 0;
137     ualarm(10_000, 10_000);
138     sleep until $tick >= 3;
139     ok 13, 1;
140     ualarm(0);
141 }
142
143 # new test: did we even get close?
144
145 if (!$have_time) {
146     skip 14
147 } else {
148  my ($s, $n);
149  for my $i (1 .. 100) {
150      $s += Time::HiRes::time() - time();
151      $n++;
152  }
153  # $s should be, at worst, equal to $n
154  # (time() may be rounding down, up, or closest)
155  ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
156  print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
157 }
158
159 unless (defined &Time::HiRes::gettimeofday
160         && defined &Time::HiRes::ualarm
161         && defined &Time::HiRes::usleep
162         && $Config{d_ualarm}) {
163     for (15..17) {
164         print "ok $_ # skipped\n";
165     }
166 } else {
167     use Time::HiRes qw (time alarm sleep);
168
169     my ($f, $r, $i, $not, $ok);
170
171     $f = time; 
172     print "# time...$f\n";
173     print "ok 15\n";
174
175     $r = [Time::HiRes::gettimeofday()];
176     sleep (0.5);
177     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
178
179     $r = [Time::HiRes::gettimeofday()];
180     $i = 5;
181     $SIG{ALRM} = "tick";
182     while ($i > 0)
183     {
184         alarm(0.3);
185         select (undef, undef, undef, 3);
186         my $ival = Time::HiRes::tv_interval ($r);
187         print "# Select returned! $i $ival\n";
188         print "# ", abs($ival/3 - 1), "\n";
189         # Whether select() gets restarted after signals is
190         # implementation dependent.  If it is restarted, we
191         # will get about 3.3 seconds: 3 from the select, 0.3
192         # from the alarm.  If this happens, let's just skip
193         # this particular test.  --jhi
194         if (abs($ival/3.3 - 1) < $limit) {
195             $ok = "Skip: your select() seems to get restarted by your SIGALRM";
196             undef $not;
197             last;
198         }
199         my $exp = 0.3 * (5 - $i);
200         # This test is more sensitive, so impose a softer limit.
201         if (abs($ival/$exp - 1) > 3*$limit) {
202             my $ratio = abs($ival/$exp);
203             $not = "while: $exp sleep took $ival ratio $ratio";
204             last;
205         }
206         $ok = $i;
207     }
208
209     sub tick
210     {
211         $i--;
212         my $ival = Time::HiRes::tv_interval ($r);
213         print "# Tick! $i $ival\n";
214         my $exp = 0.3 * (5 - $i);
215         # This test is more sensitive, so impose a softer limit.
216         if (abs($ival/$exp - 1) > 3*$limit) {
217             my $ratio = abs($ival/$exp);
218             $not = "tick: $exp sleep took $ival ratio $ratio";
219             $i = 0;
220         }
221     }
222
223     alarm(0); # can't cancel usig %SIG
224
225     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
226 }
227
228 unless (defined &Time::HiRes::setitimer
229         && defined &Time::HiRes::getitimer
230         && exists &Time::HiRes::ITIMER_VIRTUAL
231         && $Config{d_select}
232         && $Config{sig_name} =~ m/\bVTALRM\b/) {
233     for (18..19) {
234         print "ok $_ # Skip: no virtual interval timers\n";
235     }
236 } else {
237     use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
238
239     my $i = 3;
240     my $r = [Time::HiRes::gettimeofday()];
241
242     $SIG{VTALRM} = sub {
243         $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
244         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
245     };  
246
247     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
248
249     # Assume interval timer granularity of $limit * 0.5 seconds.  Too bold?
250     print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
251     print "ok 18\n";
252
253     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
254
255     while (getitimer(ITIMER_VIRTUAL)) {
256         my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
257     }
258
259     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
260
261     print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
262     print "ok 19\n";
263
264     $SIG{VTALRM} = 'DEFAULT';
265 }
266
267 if ($have_gettimeofday) {
268     my ($t0, $td);
269
270     my $sleep = 1.5; # seconds
271     my $msg;
272
273     $t0 = gettimeofday();
274     $a = abs(sleep($sleep)        / $sleep         - 1.0);
275     $td = gettimeofday() - $t0;
276     my $ratio = 1.0 + $a;
277
278     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
279
280     if ($td < $sleep * (1 + $limit)) {
281         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
282     } else {
283         print "ok 20 # Skip: $msg";
284     }
285
286     $t0 = gettimeofday();
287     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
288     $td = gettimeofday() - $t0;
289     $ratio = 1.0 + $a;
290
291     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
292
293     if ($td < $sleep * (1 + $limit)) {
294         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
295     } else {
296         print "ok 21 # Skip: $msg";
297     }
298
299 } else {
300     for (20..21) {
301         print "ok $_ # Skip: no gettimeofday\n";
302     }
303 }
304
305 eval { sleep(-1) };
306 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
307     "ok 22\n" : "not ok 22\n";
308
309 eval { usleep(-2) };
310 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
311     "ok 23\n" : "not ok 23\n";
312
313 if ($have_ualarm) {
314     eval { alarm(-3) };
315     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
316         "ok 24\n" : "not ok 24\n";
317
318     eval { ualarm(-4) };
319     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
320     "ok 25\n" : "not ok 25\n";
321 } else {
322     skip 24;
323     skip 25;
324 }