Commit | Line | Data |
dcf686c9 |
1 | BEGIN { |
2 | chdir 't' if -d 't'; |
3 | @INC = '../lib'; |
4 | } |
5 | |
f7916ddb |
6 | BEGIN { $| = 1; print "1..25\n"; } |
dcf686c9 |
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; |
c9ff6e92 |
21 | my $have_time = defined &Time::HiRes::time; |
dcf686c9 |
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 | |
3c72ec00 |
27 | use Config; |
28 | |
0740bb5b |
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 | |
dcf686c9 |
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 | |
c9ff6e92 |
70 | my $f = Time::HiRes::time(); |
dcf686c9 |
71 | ok 5, $f > 850_000_000, "$f too small"; |
0740bb5b |
72 | ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2"; |
dcf686c9 |
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 { |
c9ff6e92 |
90 | my $f = Time::HiRes::time(); |
dcf686c9 |
91 | usleep(500_000); |
c9ff6e92 |
92 | my $f2 = Time::HiRes::time(); |
dcf686c9 |
93 | my $d = $f2 - $f; |
0740bb5b |
94 | ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2"; |
dcf686c9 |
95 | } |
96 | } |
97 | |
98 | # Two-arg tv_interval() is always available. |
99 | { |
100 | my $f = tv_interval [5, 100_000], [10, 500_000]; |
6cf89afa |
101 | ok 9, abs($f - 5.4) < 0.001, $f; |
dcf686c9 |
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 | |
c9ff6e92 |
113 | if (!$have_usleep || !$have_gettimeofday) { |
dcf686c9 |
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; |
0740bb5b |
121 | ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs."; |
dcf686c9 |
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 | |
c9ff6e92 |
145 | if (!$have_time) { |
146 | skip 14 |
147 | } else { |
456811e8 |
148 | my ($s, $n); |
149 | for my $i (1 .. 100) { |
150 | $s += Time::HiRes::time() - time(); |
151 | $n++; |
64cc9c1c |
152 | } |
456811e8 |
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"; |
dcf686c9 |
157 | } |
158 | |
159 | unless (defined &Time::HiRes::gettimeofday |
160 | && defined &Time::HiRes::ualarm |
c7f7de79 |
161 | && defined &Time::HiRes::usleep |
162 | && $Config{d_ualarm}) { |
dcf686c9 |
163 | for (15..17) { |
164 | print "ok $_ # skipped\n"; |
165 | } |
166 | } else { |
167 | use Time::HiRes qw (time alarm sleep); |
168 | |
cb203acc |
169 | my ($f, $r, $i, $not, $ok); |
dcf686c9 |
170 | |
dcf686c9 |
171 | $f = time; |
add58217 |
172 | print "# time...$f\n"; |
173 | print "ok 15\n"; |
dcf686c9 |
174 | |
c9ff6e92 |
175 | $r = [Time::HiRes::gettimeofday()]; |
dcf686c9 |
176 | sleep (0.5); |
add58217 |
177 | print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n"; |
dcf686c9 |
178 | |
c9ff6e92 |
179 | $r = [Time::HiRes::gettimeofday()]; |
dcf686c9 |
180 | $i = 5; |
181 | $SIG{ALRM} = "tick"; |
6ea166ef |
182 | while ($i > 0) |
dcf686c9 |
183 | { |
0e172911 |
184 | alarm(0.3); |
c7f7de79 |
185 | select (undef, undef, undef, 3); |
186 | my $ival = Time::HiRes::tv_interval ($r); |
187 | print "# Select returned! $i $ival\n"; |
cb203acc |
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 | } |
c7f7de79 |
199 | my $exp = 0.3 * (5 - $i); |
0740bb5b |
200 | # This test is more sensitive, so impose a softer limit. |
cb203acc |
201 | if (abs($ival/$exp - 1) > 3*$limit) { |
0740bb5b |
202 | my $ratio = abs($ival/$exp); |
203 | $not = "while: $exp sleep took $ival ratio $ratio"; |
c7f7de79 |
204 | last; |
205 | } |
cb203acc |
206 | $ok = $i; |
dcf686c9 |
207 | } |
208 | |
209 | sub tick |
210 | { |
dcf686c9 |
211 | $i--; |
c7f7de79 |
212 | my $ival = Time::HiRes::tv_interval ($r); |
213 | print "# Tick! $i $ival\n"; |
214 | my $exp = 0.3 * (5 - $i); |
0740bb5b |
215 | # This test is more sensitive, so impose a softer limit. |
cb203acc |
216 | if (abs($ival/$exp - 1) > 3*$limit) { |
0740bb5b |
217 | my $ratio = abs($ival/$exp); |
218 | $not = "tick: $exp sleep took $ival ratio $ratio"; |
c7f7de79 |
219 | $i = 0; |
220 | } |
dcf686c9 |
221 | } |
c7f7de79 |
222 | |
d0b83b2b |
223 | alarm(0); # can't cancel usig %SIG |
dcf686c9 |
224 | |
cb203acc |
225 | print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n"; |
dcf686c9 |
226 | } |
227 | |
3c72ec00 |
228 | unless (defined &Time::HiRes::setitimer |
229 | && defined &Time::HiRes::getitimer |
230 | && exists &Time::HiRes::ITIMER_VIRTUAL |
d0b83b2b |
231 | && $Config{d_select} |
232 | && $Config{sig_name} =~ m/\bVTALRM\b/) { |
3c72ec00 |
233 | for (18..19) { |
0e172911 |
234 | print "ok $_ # Skip: no virtual interval timers\n"; |
3c72ec00 |
235 | } |
236 | } else { |
237 | use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL); |
238 | |
239 | my $i = 3; |
c9ff6e92 |
240 | my $r = [Time::HiRes::gettimeofday()]; |
3c72ec00 |
241 | |
242 | $SIG{VTALRM} = sub { |
243 | $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0); |
244 | print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n"; |
245 | }; |
246 | |
0e172911 |
247 | print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n"; |
248 | |
0740bb5b |
249 | # Assume interval timer granularity of $limit * 0.5 seconds. Too bold? |
250 | print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit; |
0e172911 |
251 | print "ok 18\n"; |
3c72ec00 |
252 | |
253 | print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n"; |
254 | |
1c41c9bf |
255 | while (getitimer(ITIMER_VIRTUAL)) { |
256 | my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer(). |
3c72ec00 |
257 | } |
258 | |
259 | print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n"; |
260 | |
0e172911 |
261 | print "not " unless getitimer(ITIMER_VIRTUAL) == 0; |
262 | print "ok 19\n"; |
263 | |
3c72ec00 |
264 | $SIG{VTALRM} = 'DEFAULT'; |
265 | } |
266 | |
9c735d59 |
267 | if ($have_gettimeofday) { |
268 | my ($t0, $td); |
52d72fba |
269 | |
9c735d59 |
270 | my $sleep = 1.5; # seconds |
9c735d59 |
271 | my $msg; |
272 | |
273 | $t0 = gettimeofday(); |
274 | $a = abs(sleep($sleep) / $sleep - 1.0); |
275 | $td = gettimeofday() - $t0; |
0740bb5b |
276 | my $ratio = 1.0 + $a; |
9c735d59 |
277 | |
0740bb5b |
278 | $msg = "$td went by while sleeping $sleep, ratio $ratio.\n"; |
9c735d59 |
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; |
0740bb5b |
289 | $ratio = 1.0 + $a; |
9c735d59 |
290 | |
0740bb5b |
291 | $msg = "$td went by while sleeping $sleep, ratio $ratio.\n"; |
9c735d59 |
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 | } |
52d72fba |
304 | |
f7916ddb |
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 | |
ec77158c |
313 | if ($have_ualarm) { |
c46d5038 |
314 | eval { alarm(-3) }; |
315 | print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ? |
316 | "ok 24\n" : "not ok 24\n"; |
317 | |
ec77158c |
318 | eval { ualarm(-4) }; |
319 | print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ? |
f7916ddb |
320 | "ok 25\n" : "not ok 25\n"; |
ec77158c |
321 | } else { |
c46d5038 |
322 | skip 24; |
ec77158c |
323 | skip 25; |
324 | } |