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;
33 my $have_alarm = $Config{d_alarm};
34 my $have_fork = $Config{d_fork};
35 my $waitfor = 60; # 10 seconds is normal.
39 print "# Testing process $$\n";
40 print "# Starting the timer process\n";
41 if (defined ($pid = fork())) {
42 if ($pid == 0) { # We are the kid, set up the timer.
43 print "# Timer process $$\n";
45 warn "$0: Time's up!\n";
46 print "# Terminating the testing process\n";
47 kill('TERM', getppid());
48 print "# Timer process exiting\n";
52 warn "$0: fork failed: $!\n";
55 print "# No timer process\n";
60 if (open(XDEFINE, "xdefine")) {
61 chomp($xdefine = <XDEFINE>);
65 # Ideally, we'd like to test that the timers are rather precise.
66 # However, if the system is busy, there are no guarantees on how
67 # quickly we will return. This limit used to be 10%, but that
68 # was occasionally triggered falsely.
70 # Another possibility might be to print "ok" if the test completes fine
71 # with (say) 10% slosh, "skip - system may have been busy?" if the test
72 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
73 # consider changing over to test.pl at the same time.
74 # --A.D., Nov 27, 2001
75 my $limit = 0.20; # 20% is acceptable slosh for testing timers
78 map { print "ok $_ # skipped\n" } @_;
82 my ($n, $result, @info) = @_;
88 print "# @info\n" if @info;
92 if (!$have_gettimeofday) {
96 my @one = gettimeofday();
97 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
98 ok 3, $one[0] > 850_000_000, "@one too small";
102 my @two = gettimeofday();
103 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
104 "@two is not greater than @one";
106 my $f = Time::HiRes::time();
107 ok 5, $f > 850_000_000, "$f too small";
108 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
120 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
122 if (!$have_gettimeofday) {
126 my $f = Time::HiRes::time();
128 my $f2 = Time::HiRes::time();
130 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
134 # Two-arg tv_interval() is always available.
136 my $f = tv_interval [5, 100_000], [10, 500_000];
137 ok 9, abs($f - 5.4) < 0.001, $f;
140 if (!$have_gettimeofday) {
144 my $r = [gettimeofday()];
145 my $f = tv_interval $r;
149 if (!$have_usleep || !$have_gettimeofday) {
153 my $r = [gettimeofday()];
154 Time::HiRes::sleep( 0.5 );
155 my $f = tv_interval $r;
156 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
159 if (!$have_ualarm || !$have_alarm) {
164 local $SIG{ALRM} = sub { $tick++ };
166 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
167 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
169 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
172 ualarm(10_000, 10_000);
173 while ($tick < 3) { sleep }
178 # new test: did we even get close?
184 for my $i (1 .. 100) {
185 $s += Time::HiRes::time() - time();
188 # $s should be, at worst, equal to $n
189 # (time() may be rounding down, up, or closest)
190 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
191 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
194 my $has_ualarm = $Config{d_ualarm};
196 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
198 unless ( defined &Time::HiRes::gettimeofday
199 && defined &Time::HiRes::ualarm
200 && defined &Time::HiRes::usleep
203 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
206 use Time::HiRes qw (time alarm sleep);
208 my ($f, $r, $i, $not, $ok);
211 print "# time...$f\n";
214 $r = [Time::HiRes::gettimeofday()];
216 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
218 $r = [Time::HiRes::gettimeofday()];
224 select (undef, undef, undef, 3);
225 my $ival = Time::HiRes::tv_interval ($r);
226 print "# Select returned! $i $ival\n";
227 print "# ", abs($ival/3 - 1), "\n";
228 # Whether select() gets restarted after signals is
229 # implementation dependent. If it is restarted, we
230 # will get about 3.3 seconds: 3 from the select, 0.3
231 # from the alarm. If this happens, let's just skip
232 # this particular test. --jhi
233 if (abs($ival/3.3 - 1) < $limit) {
234 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
238 my $exp = 0.3 * (5 - $i);
239 # This test is more sensitive, so impose a softer limit.
240 if (abs($ival/$exp - 1) > 3*$limit) {
241 my $ratio = abs($ival/$exp);
242 $not = "while: $exp sleep took $ival ratio $ratio";
251 my $ival = Time::HiRes::tv_interval ($r);
252 print "# Tick! $i $ival\n";
253 my $exp = 0.3 * (5 - $i);
254 # This test is more sensitive, so impose a softer limit.
255 if (abs($ival/$exp - 1) > 3*$limit) {
256 my $ratio = abs($ival/$exp);
257 $not = "tick: $exp sleep took $ival ratio $ratio";
262 alarm(0); # can't cancel usig %SIG
264 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
267 unless ( defined &Time::HiRes::setitimer
268 && defined &Time::HiRes::getitimer
269 && eval 'Time::HiRes::ITIMER_VIRTUAL'
271 && $Config{sig_name} =~ m/\bVTALRM\b/) {
273 print "ok $_ # Skip: no virtual interval timers\n";
276 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
279 my $r = [Time::HiRes::gettimeofday()];
282 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
283 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
286 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
288 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
289 print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
292 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
294 while (getitimer(ITIMER_VIRTUAL)) {
296 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
299 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
301 print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
304 $SIG{VTALRM} = 'DEFAULT';
307 if ($have_gettimeofday) {
310 my $sleep = 1.5; # seconds
313 $t0 = gettimeofday();
314 $a = abs(sleep($sleep) / $sleep - 1.0);
315 $td = gettimeofday() - $t0;
316 my $ratio = 1.0 + $a;
318 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
320 if ($td < $sleep * (1 + $limit)) {
321 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
323 print "ok 20 # Skip: $msg";
326 $t0 = gettimeofday();
327 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
328 $td = gettimeofday() - $t0;
331 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
333 if ($td < $sleep * (1 + $limit)) {
334 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
336 print "ok 21 # Skip: $msg";
341 print "ok $_ # Skip: no gettimeofday\n";
346 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
347 "ok 22\n" : "not ok 22\n";
350 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
351 "ok 23\n" : "not ok 23\n";
355 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
356 "ok 24\n" : "not ok 24\n";
359 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
360 "ok 25\n" : "not ok 25\n";
367 print "# Terminating the timer process $pid\n";
368 kill('TERM', $pid); # We are done, the timer can go.