(retracted by #14057)
[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     # 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))]
140         
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;
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     sleep 3 until $tick >= 3;
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 unless (defined &Time::HiRes::gettimeofday
170         && defined &Time::HiRes::ualarm
171         && defined &Time::HiRes::usleep
172         && $Config{d_ualarm}) {
173     for (15..17) {
174         print "ok $_ # skipped\n";
175     }
176 } else {
177     use Time::HiRes qw (time alarm sleep);
178
179     my ($f, $r, $i, $not, $ok);
180
181     $f = time; 
182     print "# time...$f\n";
183     print "ok 15\n";
184
185     $r = [Time::HiRes::gettimeofday()];
186     sleep (0.5);
187     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
188
189     $r = [Time::HiRes::gettimeofday()];
190     $i = 5;
191     $SIG{ALRM} = "tick";
192     while ($i > 0)
193     {
194         alarm(0.3);
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";
206             undef $not;
207             last;
208         }
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";
214             last;
215         }
216         $ok = $i;
217     }
218
219     sub tick
220     {
221         $i--;
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";
229             $i = 0;
230         }
231     }
232
233     alarm(0); # can't cancel usig %SIG
234
235     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
236 }
237
238 unless (defined &Time::HiRes::setitimer
239         && defined &Time::HiRes::getitimer
240         && exists &Time::HiRes::ITIMER_VIRTUAL
241         && $Config{d_select}
242         && $Config{sig_name} =~ m/\bVTALRM\b/) {
243     for (18..19) {
244         print "ok $_ # Skip: no virtual interval timers\n";
245     }
246 } else {
247     use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
248
249     my $i = 3;
250     my $r = [Time::HiRes::gettimeofday()];
251
252     $SIG{VTALRM} = sub {
253         $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
254         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
255     };  
256
257     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
258
259     # Assume interval timer granularity of $limit * 0.5 seconds.  Too bold?
260     print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
261     print "ok 18\n";
262
263     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
264
265     while (getitimer(ITIMER_VIRTUAL)) {
266         my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
267     }
268
269     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
270
271     print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
272     print "ok 19\n";
273
274     $SIG{VTALRM} = 'DEFAULT';
275 }
276
277 if ($have_gettimeofday) {
278     my ($t0, $td);
279
280     my $sleep = 1.5; # seconds
281     my $msg;
282
283     $t0 = gettimeofday();
284     $a = abs(sleep($sleep)        / $sleep         - 1.0);
285     $td = gettimeofday() - $t0;
286     my $ratio = 1.0 + $a;
287
288     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
289
290     if ($td < $sleep * (1 + $limit)) {
291         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
292     } else {
293         print "ok 20 # Skip: $msg";
294     }
295
296     $t0 = gettimeofday();
297     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
298     $td = gettimeofday() - $t0;
299     $ratio = 1.0 + $a;
300
301     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
302
303     if ($td < $sleep * (1 + $limit)) {
304         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
305     } else {
306         print "ok 21 # Skip: $msg";
307     }
308
309 } else {
310     for (20..21) {
311         print "ok $_ # Skip: no gettimeofday\n";
312     }
313 }
314
315 eval { sleep(-1) };
316 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
317     "ok 22\n" : "not ok 22\n";
318
319 eval { usleep(-2) };
320 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
321     "ok 23\n" : "not ok 23\n";
322
323 if ($have_ualarm) {
324     eval { alarm(-3) };
325     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
326         "ok 24\n" : "not ok 24\n";
327
328     eval { ualarm(-4) };
329     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
330     "ok 25\n" : "not ok 25\n";
331 } else {
332     skip 24;
333     skip 25;
334 }