Can't do subsecond alarms without ualarm; and break out
[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 sub skip {
30     map { print "ok $_ (skipped)\n" } @_;
31 }
32
33 sub ok {
34     my ($n, $result, @info) = @_;
35     if ($result) {
36         print "ok $n\n";
37     }
38     else {
39         print "not ok $n\n";
40         print "# @info\n" if @info;
41     }
42 }
43
44 if (!$have_gettimeofday) {
45     skip 2..6;
46 }
47 else {
48     my @one = gettimeofday();
49     ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
50     ok 3, $one[0] > 850_000_000, "@one too small";
51
52     sleep 1;
53
54     my @two = gettimeofday();
55     ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
56             "@two is not greater than @one";
57
58     my $f = Time::HiRes::time();
59     ok 5, $f > 850_000_000, "$f too small";
60     ok 6, $f - $two[0] < 2, "$f - @two >= 2";
61 }
62
63 if (!$have_usleep) {
64     skip 7..8;
65 }
66 else {
67     my $one = time;
68     usleep(10_000);
69     my $two = time;
70     usleep(10_000);
71     my $three = time;
72     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
73
74     if (!$have_gettimeofday) {
75         skip 8;
76     }
77     else {
78         my $f = Time::HiRes::time();
79         usleep(500_000);
80         my $f2 = Time::HiRes::time();
81         my $d = $f2 - $f;
82         ok 8, $d > 0.4 && $d < 0.8, "slept $d secs $f to $f2";
83     }
84 }
85
86 # Two-arg tv_interval() is always available.
87 {
88     my $f = tv_interval [5, 100_000], [10, 500_000];
89     ok 9, abs($f - 5.4) < 0.001, $f;
90 }
91
92 if (!$have_gettimeofday) {
93     skip 10;
94 }
95 else {
96     my $r = [gettimeofday()];
97     my $f = tv_interval $r;
98     ok 10, $f < 2, $f;
99 }
100
101 if (!$have_usleep || !$have_gettimeofday) {
102     skip 11;
103 }
104 else {
105     my $r = [gettimeofday()];
106     #jTime::HiRes::sleep 0.5;
107     Time::HiRes::sleep( 0.5 );
108     my $f = tv_interval $r;
109     ok 11, $f > 0.4 && $f < 0.8, "slept $f secs";
110 }
111
112 if (!$have_ualarm) {
113     skip 12..13;
114 }
115 else {
116     my $tick = 0;
117     local $SIG{ALRM} = sub { $tick++ };
118
119     my $one = time; $tick = 0; ualarm(10_000); sleep until $tick;
120     my $two = time; $tick = 0; ualarm(10_000); sleep until $tick;
121     my $three = time;
122     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
123
124     $tick = 0;
125     ualarm(10_000, 10_000);
126     sleep until $tick >= 3;
127     ok 13, 1;
128     ualarm(0);
129 }
130
131 # new test: did we even get close?
132
133 if (!$have_time) {
134     skip 14
135 } else {
136  my ($s, $n);
137  for my $i (1 .. 100) {
138      $s += Time::HiRes::time() - time();
139      $n++;
140  }
141  # $s should be, at worst, equal to $n
142  # (time() may be rounding down, up, or closest)
143  ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
144  print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
145 }
146
147 unless (defined &Time::HiRes::gettimeofday
148         && defined &Time::HiRes::ualarm
149         && defined &Time::HiRes::usleep
150         && $Config{d_ualarm}) {
151     for (15..17) {
152         print "ok $_ # skipped\n";
153     }
154 } else {
155     use Time::HiRes qw (time alarm sleep);
156
157     my ($f, $r, $i, $not);
158
159     $f = time; 
160     print "# time...$f\n";
161     print "ok 15\n";
162
163     $r = [Time::HiRes::gettimeofday()];
164     sleep (0.5);
165     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
166
167     $r = [Time::HiRes::gettimeofday()];
168     $i = 5;
169     $SIG{ALRM} = "tick";
170     while ($i > 0)
171     {
172         alarm(0.3);
173         select (undef, undef, undef, 3);
174         my $ival = Time::HiRes::tv_interval ($r);
175         print "# Select returned! $i $ival\n";
176         my $exp = 0.3 * (5 - $i);
177         if (abs($ival/$exp) - 1 > 0.1) {
178             $not = "$exp sleep took $ival";
179             last;
180         }
181     }
182
183     sub tick
184     {
185         $i--;
186         my $ival = Time::HiRes::tv_interval ($r);
187         print "# Tick! $i $ival\n";
188         my $exp = 0.3 * (5 - $i);
189         if (abs($ival/$exp) - 1 > 0.1) {
190             $not = "$exp sleep took $ival";
191             $i = 0;
192         }
193     }
194
195     alarm(0); # can't cancel usig %SIG
196
197     print $not ? "not ok 17 # $not\n" : "ok 17\n";
198 }
199
200 unless (defined &Time::HiRes::setitimer
201         && defined &Time::HiRes::getitimer
202         && exists &Time::HiRes::ITIMER_VIRTUAL
203         && $Config{d_select}
204         && $Config{sig_name} =~ m/\bVTALRM\b/) {
205     for (18..19) {
206         print "ok $_ # Skip: no virtual interval timers\n";
207     }
208 } else {
209     use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
210
211     my $i = 3;
212     my $r = [Time::HiRes::gettimeofday()];
213
214     $SIG{VTALRM} = sub {
215         $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
216         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
217     };  
218
219     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
220
221     # Assume interval timer granularity of 0.05 seconds.  Too bold?
222     print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < 0.1;
223     print "ok 18\n";
224
225     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
226
227     while (getitimer(ITIMER_VIRTUAL)) {
228         my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
229     }
230
231     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
232
233     print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
234     print "ok 19\n";
235
236     $SIG{VTALRM} = 'DEFAULT';
237 }
238
239 if ($have_gettimeofday) {
240     my ($t0, $td);
241
242     my $sleep = 1.5; # seconds
243     my $limit = 0.1; # 10% is acceptable slosh for timers
244     my $msg;
245
246     $t0 = gettimeofday();
247     $a = abs(sleep($sleep)        / $sleep         - 1.0);
248     $td = gettimeofday() - $t0;
249
250     $msg = "$td went by while sleeping $sleep, ratio $a\n";
251
252     if ($td < $sleep * (1 + $limit)) {
253         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
254     } else {
255         print "ok 20 # Skip: $msg";
256     }
257
258     $t0 = gettimeofday();
259     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
260     $td = gettimeofday() - $t0;
261
262     $msg = "$td went by while sleeping $sleep, ratio $a\n";
263
264     if ($td < $sleep * (1 + $limit)) {
265         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
266     } else {
267         print "ok 21 # Skip: $msg";
268     }
269
270 } else {
271     for (20..21) {
272         print "ok $_ # Skip: no gettimeofday\n";
273     }
274 }
275
276 eval { sleep(-1) };
277 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
278     "ok 22\n" : "not ok 22\n";
279
280 eval { usleep(-2) };
281 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
282     "ok 23\n" : "not ok 23\n";
283
284 if ($have_ualarm) {
285     eval { alarm(-3) };
286     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
287         "ok 24\n" : "not ok 24\n";
288
289     eval { ualarm(-4) };
290     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
291     "ok 25\n" : "not ok 25\n";
292 } else {
293     skip 24;
294     skip 25;
295 }