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 | |
dcf686c9 |
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 | |
c9ff6e92 |
58 | my $f = Time::HiRes::time(); |
dcf686c9 |
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 { |
c9ff6e92 |
78 | my $f = Time::HiRes::time(); |
dcf686c9 |
79 | usleep(500_000); |
c9ff6e92 |
80 | my $f2 = Time::HiRes::time(); |
dcf686c9 |
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]; |
6cf89afa |
89 | ok 9, abs($f - 5.4) < 0.001, $f; |
dcf686c9 |
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 | |
c9ff6e92 |
101 | if (!$have_usleep || !$have_gettimeofday) { |
dcf686c9 |
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 | |
c9ff6e92 |
133 | if (!$have_time) { |
134 | skip 14 |
135 | } else { |
64cc9c1c |
136 | my ($t1, $tf, $t2); |
cb11fff9 |
137 | for my $i (1 .. 20) { |
64cc9c1c |
138 | $t1 = time(); |
139 | $tf = Time::HiRes::time(); |
140 | $t2 = 1 + time(); |
cb11fff9 |
141 | last if (($t2 - $t1) <= 1) && $t1 <= $tf; |
64cc9c1c |
142 | } |
143 | ok 14, (($t1 <= $tf) && ($tf <= $t2)), |
cb11fff9 |
144 | "Time::HiRes::time $tf not bracketed by [$t1, $t2]"; |
64cc9c1c |
145 | |
dcf686c9 |
146 | } |
147 | |
148 | unless (defined &Time::HiRes::gettimeofday |
149 | && defined &Time::HiRes::ualarm |
150 | && defined &Time::HiRes::usleep) { |
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); |
158 | |
159 | print "# time..."; |
160 | $f = time; |
161 | print "$f\nok 15\n"; |
162 | |
163 | print "# sleep..."; |
c9ff6e92 |
164 | $r = [Time::HiRes::gettimeofday()]; |
dcf686c9 |
165 | sleep (0.5); |
166 | print Time::HiRes::tv_interval($r), "\nok 16\n"; |
167 | |
c9ff6e92 |
168 | $r = [Time::HiRes::gettimeofday()]; |
dcf686c9 |
169 | $i = 5; |
170 | $SIG{ALRM} = "tick"; |
171 | while ($i) |
172 | { |
0e172911 |
173 | alarm(0.3); |
dcf686c9 |
174 | select (undef, undef, undef, 10); |
3c72ec00 |
175 | print "# Select returned! $i ", Time::HiRes::tv_interval ($r), "\n"; |
dcf686c9 |
176 | } |
177 | |
178 | sub tick |
179 | { |
dcf686c9 |
180 | $i--; |
3c72ec00 |
181 | print "# Tick! $i ", Time::HiRes::tv_interval ($r), "\n"; |
dcf686c9 |
182 | } |
d0b83b2b |
183 | alarm(0); # can't cancel usig %SIG |
dcf686c9 |
184 | |
185 | print "ok 17\n"; |
186 | } |
187 | |
3c72ec00 |
188 | unless (defined &Time::HiRes::setitimer |
189 | && defined &Time::HiRes::getitimer |
190 | && exists &Time::HiRes::ITIMER_VIRTUAL |
d0b83b2b |
191 | && $Config{d_select} |
192 | && $Config{sig_name} =~ m/\bVTALRM\b/) { |
3c72ec00 |
193 | for (18..19) { |
0e172911 |
194 | print "ok $_ # Skip: no virtual interval timers\n"; |
3c72ec00 |
195 | } |
196 | } else { |
197 | use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL); |
198 | |
199 | my $i = 3; |
c9ff6e92 |
200 | my $r = [Time::HiRes::gettimeofday()]; |
3c72ec00 |
201 | |
202 | $SIG{VTALRM} = sub { |
203 | $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0); |
204 | print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n"; |
205 | }; |
206 | |
0e172911 |
207 | print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n"; |
208 | |
209 | # Assume interval timer granularity of 0.05 seconds. Too bold? |
210 | print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < 0.1; |
211 | print "ok 18\n"; |
3c72ec00 |
212 | |
213 | print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n"; |
214 | |
1c41c9bf |
215 | while (getitimer(ITIMER_VIRTUAL)) { |
216 | my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer(). |
3c72ec00 |
217 | } |
218 | |
219 | print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n"; |
220 | |
0e172911 |
221 | print "not " unless getitimer(ITIMER_VIRTUAL) == 0; |
222 | print "ok 19\n"; |
223 | |
3c72ec00 |
224 | $SIG{VTALRM} = 'DEFAULT'; |
225 | } |
226 | |
f7916ddb |
227 | $a = abs(sleep(1.5) / 1.5 - 1.0); |
91f02c16 |
228 | print $a < 0.1 ? "ok 20 # $a\n" : "not ok 20 # $a\n"; |
52d72fba |
229 | |
92bc48ca |
230 | $a = abs(usleep(1_500_000) / 1_500_000 - 1.0); |
91f02c16 |
231 | print $a < 0.1 ? "ok 21 # $a\n" : "not ok 21 # $a\n"; |
52d72fba |
232 | |
f7916ddb |
233 | eval { sleep(-1) }; |
234 | print $@ =~ /::sleep\(-1\): negative time not invented yet/ ? |
235 | "ok 22\n" : "not ok 22\n"; |
236 | |
237 | eval { usleep(-2) }; |
238 | print $@ =~ /::usleep\(-2\): negative time not invented yet/ ? |
239 | "ok 23\n" : "not ok 23\n"; |
240 | |
ec77158c |
241 | if ($have_ualarm) { |
c46d5038 |
242 | eval { alarm(-3) }; |
243 | print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ? |
244 | "ok 24\n" : "not ok 24\n"; |
245 | |
ec77158c |
246 | eval { ualarm(-4) }; |
247 | print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ? |
f7916ddb |
248 | "ok 25\n" : "not ok 25\n"; |
ec77158c |
249 | } else { |
c46d5038 |
250 | skip 24; |
ec77158c |
251 | skip 25; |
252 | } |