10 BEGIN { $| = 1; print "1..25\n"; }
12 END {print "not ok 1\n" unless $loaded;}
14 use Time::HiRes qw(tv_interval);
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;
27 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
28 import Time::HiRes 'usleep' if $have_usleep;
29 import Time::HiRes 'ualarm' if $have_ualarm;
35 if (open(XDEFINE, "xdefine")) {
36 chomp($xdefine = <XDEFINE>);
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.
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
53 map { print "ok $_ # skipped\n" } @_;
57 my ($n, $result, @info) = @_;
63 print "# @info\n" if @info;
67 if (!$have_gettimeofday) {
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";
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";
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";
95 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
97 if (!$have_gettimeofday) {
101 my $f = Time::HiRes::time();
103 my $f2 = Time::HiRes::time();
105 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
109 # Two-arg tv_interval() is always available.
111 my $f = tv_interval [5, 100_000], [10, 500_000];
112 ok 9, abs($f - 5.4) < 0.001, $f;
115 if (!$have_gettimeofday) {
119 my $r = [gettimeofday()];
120 my $f = tv_interval $r;
124 if (!$have_usleep || !$have_gettimeofday) {
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.";
134 if (!$have_ualarm || !$Config{d_alarm}) {
139 local $SIG{ALRM} = sub { $tick++ };
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 }
144 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
147 ualarm(10_000, 10_000);
148 while ($tick < 3) { sleep }
153 # new test: did we even get close?
159 for my $i (1 .. 100) {
160 $s += Time::HiRes::time() - time();
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";
169 my $has_ualarm = $Config{d_ualarm};
171 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
173 unless ( defined &Time::HiRes::gettimeofday
174 && defined &Time::HiRes::ualarm
175 && defined &Time::HiRes::usleep
178 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
181 use Time::HiRes qw (time alarm sleep);
183 my ($f, $r, $i, $not, $ok);
186 print "# time...$f\n";
189 $r = [Time::HiRes::gettimeofday()];
191 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
193 $r = [Time::HiRes::gettimeofday()];
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)";
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";
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";
237 alarm(0); # can't cancel usig %SIG
239 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
242 unless ( defined &Time::HiRes::setitimer
243 && defined &Time::HiRes::getitimer
244 && eval 'Time::HiRes::ITIMER_VIRTUAL'
246 && $Config{sig_name} =~ m/\bVTALRM\b/) {
248 print "ok $_ # Skip: no virtual interval timers\n";
251 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
254 my $r = [Time::HiRes::gettimeofday()];
257 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
258 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
261 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
263 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
264 print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
267 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
269 while (getitimer(ITIMER_VIRTUAL)) {
271 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
274 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
276 print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
279 $SIG{VTALRM} = 'DEFAULT';
282 if ($have_gettimeofday) {
285 my $sleep = 1.5; # seconds
288 $t0 = gettimeofday();
289 $a = abs(sleep($sleep) / $sleep - 1.0);
290 $td = gettimeofday() - $t0;
291 my $ratio = 1.0 + $a;
293 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
295 if ($td < $sleep * (1 + $limit)) {
296 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
298 print "ok 20 # Skip: $msg";
301 $t0 = gettimeofday();
302 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
303 $td = gettimeofday() - $t0;
306 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
308 if ($td < $sleep * (1 + $limit)) {
309 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
311 print "ok 21 # Skip: $msg";
316 print "ok $_ # Skip: no gettimeofday\n";
321 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
322 "ok 22\n" : "not ok 22\n";
325 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
326 "ok 23\n" : "not ok 23\n";
330 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
331 "ok 24\n" : "not ok 24\n";
334 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
335 "ok 25\n" : "not ok 25\n";