blib/lib/IO/Async/LoopTests.pm

filter: Statementstmt 92.8% 429 / 462 Branchbran 51.3% 39 / 76 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 91.2% 83 / 91 Podpod 9.0% 1 / 11 totaltotal 85.7% 553 / 645 CC 55 SCAR 20.2
CC
55
Cov
87%
run_tests_io
29.8
run_tests_timer
6.1
__ANON__
5.6
CRAP 7.6
« blib/lib/IO/Async/Listener.pm ↑ Summary blib/lib/IO/Async/Timer/Periodic.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2009-2025 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::LoopTests 0.805;
7  
8 24 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 our @EXPORT = qw(
13 run_tests
14 );
15  
16 24 ▶use Test2::V0 0.000149;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 ▶use Test::Metrics::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 24 ▶use IO::Async::Test qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 24 ▶use IO::Async::OS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 24 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 24 ▶use Fcntl qw( SEEK_SET );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 24 ▶use POSIX qw( SIGTERM );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 24 ▶use Socket qw( sockaddr_family AF_UNIX );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 24 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Abstract Units of Time
30 24 ▶use constant AUT => $ENV{TEST_QUICK_TIMERS} ? 0.1 : 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TEST_QUICK_TIMERS'} ? : 0 24 24
31  
32 # The loop under test. We keep it in a single lexical here, so we can use
33 # is_oneref tests in the individual test suite functions
34 24 my $loop;
35 24 ▶END { undef $loop }
Subroutine END: called CC=1 SCAR=0.0
36  
37 =head1 NAME
38  
39 C<IO::Async::LoopTests> - acceptance testing for L<IO::Async::Loop> subclasses
40  
41 =head1 SYNOPSIS
42  
43 =for highlighter language=perl
44  
45 use IO::Async::LoopTests;
46 run_tests( 'IO::Async::Loop::Shiney', 'io' );
47  
48 =head1 DESCRIPTION
49  
50 This module contains a collection of test functions for running acceptance
51 tests on L<IO::Async::Loop> subclasses. It is provided as a facility for
52 authors of such subclasses to ensure that the code conforms to the Loop API
53 required by L<IO::Async>.
54  
55 =head1 TIMING
56  
57 Certain tests require the use of timers or timed delays. Normally these are
58 counted in units of seconds. By setting the environment variable
59 C<TEST_QUICK_TIMERS> to some true value, these timers run 10 times quicker,
60 being measured in units of 0.1 seconds instead. This value may be useful when
61 running the tests interactively, to avoid them taking too long. The slower
62 timers are preferred on automated smoke-testing machines, to help guard
63 against false negatives reported simply because of scheduling delays or high
64 system load while testing.
65  
66 $ TEST_QUICK_TIMERS=1 ./Build test
67  
68 =cut
69  
70 =head1 FUNCTIONS
71  
72 =cut
73  
74 =head2 run_tests
75  
76 run_tests( $class, @tests );
77  
78 Runs a test or collection of tests against the loop subclass given. The class
79 being tested is loaded by this function; the containing script does not need
80 to C<require> or C<use> it first.
81  
82 This function runs C<plan> to output its expected test count; the containing
83 script should not do this.
84  
85 =cut
86  
87 sub run_tests
88 {
89 24 ▶ my ( $testclass, @tests ) = @_;
Subroutine run_tests: called CC=3 SCAR=11.0
Pod documented
90  
91 24 ( my $file = "$testclass.pm" ) =~ s{::}{/}g;
92  
93 24 eval { require $file };
94 24 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
95 0 bail_out( "Unable to load $testclass - $@" );
96 }
97  
98 24 foreach my $test ( @tests ) {
99 24 $loop = $testclass->new;
100  
101 24 isa_ok( $loop, [ $testclass ], '$loop' );
102  
103 24 is( IO::Async::Loop->new, $loop, 'magic constructor yields $loop' );
104  
105 # Kill the reference in $ONE_TRUE_LOOP so as not to upset the refcounts
106 # and to ensure we get a new one each time
107 24 undef $IO::Async::Loop::ONE_TRUE_LOOP;
108  
109 24 is_oneref( $loop, '$loop has refcount 1' );
110  
111 24 __PACKAGE__->can( "run_tests_$test" )->();
112  
113 14 is_oneref( $loop, '$loop has refcount 1 finally' );
114 }
115  
116 14 done_testing;
117 }
118  
119 sub wait_for(&)
120 {
121 # Bounce via here so we don't upset refcount tests by having loop
122 # permanently set in IO::Async::Test
123 28 ▶ IO::Async::Test::testing_loop( $loop );
Subroutine wait_for: called CC=1 SCAR=0.0
Pod undocumented
124  
125 # Override prototype - I know what I'm doing
126 28 &IO::Async::Test::wait_for( @_ );
127  
128 28 IO::Async::Test::testing_loop( undef );
129 }
130  
131 sub time_between(&$$$)
132 {
133 16 ▶ my ( $code, $lower, $upper, $name ) = @_;
Subroutine time_between: called CC=1 SCAR=0.2
Pod undocumented
134  
135 Test2::API::context_do {
136 16 ▶ my $ctx = shift;;
Subroutine __ANON__: called CC=5 SCAR=17.3
137  
138 16 my $start = time;
139 16 $code->();
140 16 my $took = ( time - $start ) / AUT;
141  
142 16 ▶ $ctx->ok( $took >= $lower, "$name took at least $lower seconds" ) if defined $lower;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lower 14 2 16
143 16 ▶ $ctx->ok( $took <= $upper * 3, "$name took no more than $upper seconds" ) if defined $upper;
Branch: 1 branch
BranchTrueFalseTotal
if defined $upper 16 0 16
144 16 ▶ if( $took > $upper and $took <= $upper * 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($took > $upper and $took <= $upper * 3) 0 16 16
Condition33%1/3
$took > $upper and $took <= $upper * 3
!ll&&!rl&&r
1600
MC/DC0%0/2
$took > $upper and $took <= $upper * 3
$took > $upper $took <= $upper * 3
Truth table33%1/3
A and B
A= $took > $upperB= $took <= $upper * 3
ABresult
0X0
100
111
145 0 $ctx->note( "$name took longer than $upper seconds - this may just be an indication of a busy testing machine rather than a bug" );
146 }
147 16 };
148 }
149  
150 =head1 TEST SUITES
151  
152 The following test suite names exist, to be passed as a name in the C<@tests>
153 argument to C<run_tests>:
154  
155 =cut
156  
157 =head2 io
158  
159 Tests the Loop's ability to watch filehandles for IO readiness
160  
161 =cut
162  
163 sub run_tests_io
164 {
165 {
166 2 ▶ my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot create socket pair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair or die "Cannot create socket pair - $!" 0 2 2
167 2 $_->blocking( 0 ) for $S1, $S2;
168  
169 2 my $readready = 0;
170 2 my $writeready = 0;
171 $loop->watch_io(
172 handle => $S1,
173 2 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
174 2 );
175  
176 2 is_oneref( $loop, '$loop has refcount 1 after watch_io on_read_ready' );
177 2 is( $readready, 0, '$readready still 0 before ->loop_once' );
178  
179 2 $loop->loop_once( 0.1 );
180  
181 2 is( $readready, 0, '$readready when idle' );
182  
183 2 $S2->syswrite( "data\n" );
184  
185 # We should still wait a little while even thought we expect to be ready
186 # immediately, because talking to ourself with 0 poll timeout is a race
187 # condition - we can still race with the kernel.
188  
189 2 $loop->loop_once( 0.1 );
190  
191 2 is( $readready, 1, '$readready after loop_once' );
192  
193 # Ready $S1 to clear the data
194 2 $S1->getline; # ignore return
195  
196 2 $loop->unwatch_io(
197 handle => $S1,
198 on_read_ready => 1,
199 );
200  
201 $loop->watch_io(
202 handle => $S1,
203 4 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
204 2 );
205  
206 2 $readready = 0;
207 2 $S2->syswrite( "more data\n" );
208  
209 2 $loop->loop_once( 0.1 );
210  
211 2 is( $readready, 1, '$readready after ->unwatch_io/->watch_io' );
212  
213 2 $S1->getline; # ignore return
214  
215 $loop->watch_io(
216 handle => $S1,
217 2 ▶ on_write_ready => sub { $writeready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
218 2 );
219  
220 2 is_oneref( $loop, '$loop has refcount 1 after watch_io on_write_ready' );
221  
222 2 $loop->loop_once( 0.1 );
223  
224 2 is( $writeready, 1, '$writeready after loop_once' );
225  
226 2 $loop->unwatch_io(
227 handle => $S1,
228 on_write_ready => 1,
229 );
230  
231 2 $readready = 0;
232 2 $loop->loop_once( 0.1 );
233  
234 2 is( $readready, 0, '$readready before HUP' );
235  
236 2 $S2->close;
237  
238 2 $readready = 0;
239 2 $loop->loop_once( 0.1 );
240  
241 2 is( $readready, 1, '$readready after HUP' );
242  
243 2 $loop->unwatch_io(
244 handle => $S1,
245 on_read_ready => 1,
246 );
247 }
248  
249 # Check that read- and write-readiness can be watched via two separate
250 # ->watch_io calls on the same handle
251 {
252 2 ▶ my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot create socket pair - $!";
Subroutine run_tests_io: called CC=29 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair or die "Cannot create socket pair - $!" 0 2 2
253 2 $_->blocking( 0 ) for $S1, $S2;
254  
255 2 foreach my $first (qw( read write )) {
256 4 my $readready = 0;
257 4 my $writeready = 0;
258  
259 $loop->watch_io(
260 handle => $S1,
261 4 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
262 4 ▶ ) if $first eq "read";
Branch: 1 branch
BranchTrueFalseTotal
if $first eq "read" 2 2 4
263 $loop->watch_io(
264 handle => $S1,
265 8 ▶ on_write_ready => sub { $writeready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
266 4 );
267 $loop->watch_io(
268 handle => $S1,
269 4 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
270 4 ▶ ) if $first eq "write";
Branch: 1 branch
BranchTrueFalseTotal
if $first eq "write" 2 2 4
271  
272 4 $S2->syswrite( "data\n" );
273  
274 4 $loop->loop_once( 0.1 ) for 1..2;
275  
276 4 ok( $readready, '$readready after '.$first.' first split read+write watch' );
277 4 ok( $writeready, '$writeready after '.$first.' first split read+write watch' );
278  
279 4 $loop->unwatch_io(
280 handle => $S1,
281 on_read_ready => 1,
282 on_write_ready => 1,
283 );
284 }
285 }
286  
287 # HUP of pipe - can be different to sockets on some architectures
288 {
289 2 ▶ my ( $Prd, $Pwr ) = IO::Async::OS->pipepair or die "Cannot pipepair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->pipepair or die "Cannot pipepair - $!" 0 2 2
290 2 $_->blocking( 0 ) for $Prd, $Pwr;
291  
292 2 my $readready = 0;
293 $loop->watch_io(
294 handle => $Prd,
295 2 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
296 2 );
297  
298 2 $loop->loop_once( 0.1 );
299  
300 2 is( $readready, 0, '$readready before pipe HUP' );
301  
302 2 $Pwr->close;
303  
304 2 $readready = 0;
305 2 $loop->loop_once( 0.1 );
306  
307 2 is( $readready, 1, '$readready after pipe HUP' );
308  
309 2 $loop->unwatch_io(
310 handle => $Prd,
311 on_read_ready => 1,
312 );
313 }
314  
315 SKIP: {
316 2 ▶ $loop->_CAN_ON_HANGUP or skip "Loop cannot watch_io for on_hangup", 2;
Branch: 1 branch
BranchTrueFalseTotal
$loop->_CAN_ON_HANGUP or skip("Loop cannot watch_io for on_hangup", 2) 1 1 2
317  
318 SKIP: {
319 1 ▶ my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot socketpair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair or die "Cannot socketpair - $!" 0 1 1
320 1 $_->blocking( 0 ) for $S1, $S2;
321  
322 1 ▶ sockaddr_family( $S1->sockname ) == AF_UNIX or skip "Cannot reliably detect hangup condition on non AF_UNIX sockets", 1;
Branch: 1 branch
BranchTrueFalseTotal
sockaddr_family($S1->sockname) == 1 or skip("Cannot reliably detect hangup condition on non AF_UNIX sockets", 1) 0 1 1
323  
324 1 my $hangup = 0;
325 $loop->watch_io(
326 handle => $S1,
327 1 ▶ on_hangup => sub { $hangup = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
328 1 );
329  
330 1 $S2->close;
331  
332 1 $loop->loop_once( 0.1 );
333  
334 1 is( $hangup, 1, '$hangup after socket close' );
335  
336 1 $loop->unwatch_io(
337 handle => $S1,
338 on_hangup => 1,
339 );
340 }
341  
342 1 ▶ my ( $Prd, $Pwr ) = IO::Async::OS->pipepair or die "Cannot pipepair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->pipepair or die "Cannot pipepair - $!" 0 1 1
343 1 $_->blocking( 0 ) for $Prd, $Pwr;
344  
345 1 my $hangup = 0;
346 $loop->watch_io(
347 handle => $Pwr,
348 1 ▶ on_hangup => sub { $hangup = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
349 1 );
350  
351 1 $Prd->close;
352  
353 1 $loop->loop_once( 0.1 );
354  
355 1 is( $hangup, 1, '$hangup after pipe close for writing' );
356  
357 1 $loop->unwatch_io(
358 handle => $Pwr,
359 on_hangup => 1,
360 );
361 }
362  
363 # Check that combined read/write handlers can cancel each other
364 {
365 2 ▶ my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot socketpair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair or die "Cannot socketpair - $!" 0 2 2
366 2 $_->blocking( 0 ) for $S1, $S2;
367  
368 2 my $callcount = 0;
369 $loop->watch_io(
370 handle => $S1,
371 on_read_ready => sub {
372 2 ▶ $callcount++;
Subroutine __ANON__: called CC=1 SCAR=0.0
373 2 $loop->unwatch_io( handle => $S1, on_read_ready => 1, on_write_ready => 1 );
374 },
375 on_write_ready => sub {
376 0 ▶ $callcount++;
Subroutine __ANON__: not called CC=1 SCAR=6.9
377 0 $loop->unwatch_io( handle => $S1, on_read_ready => 1, on_write_ready => 1 );
378 },
379 2 );
380  
381 2 $S2->close;
382  
383 2 $loop->loop_once( 0.1 );
384  
385 2 is( $callcount, 1, 'read/write_ready can cancel each other' );
386 }
387  
388 # Check that cross-connected handlers can cancel each other
389 {
390 2 ▶ my ( $SA1, $SA2 ) = IO::Async::OS->socketpair or die "Cannot socketpair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair or die "Cannot socketpair - $!" 0 2 2
391 2 ▶ my ( $SB1, $SB2 ) = IO::Async::OS->socketpair or die "Cannot socketpair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair or die "Cannot socketpair - $!" 0 2 2
392 2 $_->blocking( 0 ) for $SA1, $SA2, $SB1, $SB2;
393  
394 2 my @handles = ( $SA1, $SB1 );
395  
396 2 my $callcount = 0;
397 $loop->watch_io(
398 handle => $_,
399 on_write_ready => sub {
400 2 ▶ $callcount++;
Subroutine __ANON__: called CC=2 SCAR=6.9
401 2 $loop->unwatch_io( handle => $_, on_write_ready => 1 ) for @handles;
402 },
403 2 ) for @handles;
404  
405 2 $loop->loop_once( 0.1 );
406  
407 2 is( $callcount, 1, 'write_ready on crosslinked handles can cancel each other' );
408 }
409  
410 # Check that error conditions that aren't true read/write-ability are still
411 # invoked
412 SKIP: {
413 2 ▶ skip "cygwin does not indicate read-ready on exceptional sockets", 1 if $^O eq "cygwin";
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "cygwin" 0 2 2
414  
415 2 ▶ my ( $S1, $S2 ) = IO::Async::OS->socketpair( 'inet', 'dgram' ) or die "Cannot create AF_INET/SOCK_DGRAM connected pair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair("inet", "dgram") or die "Cannot create AF_INET/SOCK_DGRAM connected pair - $!" 0 2 2
416 2 $_->blocking( 0 ) for $S1, $S2;
417 2 $S2->close;
418  
419 2 my $readready = 0;
420 $loop->watch_io(
421 handle => $S1,
422 2 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
423 2 );
424  
425 2 $S1->syswrite( "Boo!" );
426  
427 2 $loop->loop_once( 0.1 );
428  
429 2 is( $readready, 1, 'exceptional socket invokes on_read_ready' );
430  
431 2 $loop->unwatch_io(
432 handle => $S1,
433 on_read_ready => 1,
434 );
435 }
436  
437 # Check that regular files still report read/writereadiness
438 {
439 2 ▶ my $F = IO::File->new_tmpfile or die "Cannot create temporary file - $!";
Branch: 1 branch
BranchTrueFalseTotal
my $F = "IO::File"->new_tmpfile or die "Cannot create temporary file - $!" 0 2 2
440  
441 2 $F->print( "Here's some content\n" );
442 2 $F->seek( 0, SEEK_SET );
443  
444 2 my $readready = 0;
445 2 my $writeready = 0;
446 $loop->watch_io(
447 handle => $F,
448 2 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
449 2 ▶ on_write_ready => sub { $writeready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
450 2 );
451  
452 2 $loop->loop_once( 0.1 );
453  
454 2 is( $readready, 1, 'regular file is readready' );
455 2 is( $writeready, 1, 'regular file is writeready' );
456  
457 2 $loop->unwatch_io(
458 handle => $F,
459 on_read_ready => 1,
460 on_write_ready => 1,
461 );
462 }
463  
464 {
465 2 ▶ my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot create AF_INET/SOCK_DGRAM connected pair - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair or die "Cannot create AF_INET/SOCK_DGRAM connected pair - $!" 0 2 2
466 2 $_->blocking( 0 ) for $S1, $S2;
467  
468 2 my $readready = 0;
469 $loop->watch_io(
470 handle => $S1,
471 2 ▶ on_read_ready => sub { $readready = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
472 2 );
473  
474 2 $loop->post_fork;
475  
476 2 $S2->syswrite( "Boo!" );
477  
478 2 $loop->loop_once( 0.1 );
479  
480 2 is( $readready, 1, 'socket invokes on_read_ready' );
481 }
482 }
483  
484 =head2 timer
485  
486 Tests the Loop's ability to handle timer events
487  
488 =cut
489  
490 sub run_tests_timer
491 {
492 # New watch/unwatch API
493  
494 2 ▶ cmp_ok( abs( $loop->time - time ), "<", 0.1, '$loop->time gives the current time' );
Subroutine run_tests_timer: called CC=5 SCAR=18.1
Pod undocumented
495  
496 # ->watch_time after
497 {
498 2 my $done;
499 2 ▶ $loop->watch_time( after => 2 * AUT, code => sub { $done = 1; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
500  
501 2 is_oneref( $loop, '$loop has refcount 1 after watch_time' );
502  
503 time_between {
504 2 ▶ my $now = time;
Subroutine __ANON__: called CC=3 SCAR=15.1
505 2 $loop->loop_once( 5 * AUT );
506  
507 # poll might have returned just a little early, such that the TimerQueue
508 # doesn't think anything is ready yet. We need to handle that case.
509 2 ▶ while( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done) 0 2 2
510 0 ▶ die "It should have been ready by now" if( time - $now > 5 * AUT );
Branch: 1 branch
BranchTrueFalseTotal
if &IO::Async::LoopTests::time() - $now > 5 0 0 0
511 0 $loop->loop_once( 0.1 * AUT );
512 }
513 2 } 1.5, 2.5, 'loop_once(5) while waiting for watch_time after';
514 }
515  
516 # ->watch_time at
517 {
518 2 my $done;
519 2 ▶ $loop->watch_time( at => time + 2 * AUT, code => sub { $done = 1; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
520  
521 time_between {
522 2 ▶ my $now = time;
Subroutine __ANON__: called CC=3 SCAR=15.1
523 2 $loop->loop_once( 5 * AUT );
524  
525 # poll might have returned just a little early, such that the TimerQueue
526 # doesn't think anything is ready yet. We need to handle that case.
527 2 ▶ while( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done) 0 2 2
528 0 ▶ die "It should have been ready by now" if( time - $now > 5 * AUT );
Branch: 1 branch
BranchTrueFalseTotal
if &IO::Async::LoopTests::time() - $now > 5 0 0 0
529 0 $loop->loop_once( 0.1 * AUT );
530 }
531 2 } 1.5, 2.5, 'loop_once(5) while waiting for watch_time at';
532 }
533  
534 # cancelled timer
535 {
536 2 my $cancelled_fired = 0;
537 2 ▶ my $id = $loop->watch_time( after => 1 * AUT, code => sub { $cancelled_fired = 1 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
538 2 $loop->unwatch_time( $id );
539 2 undef $id;
540  
541 2 $loop->loop_once( 2 * AUT );
542  
543 2 ok( !$cancelled_fired, 'unwatched watch_time does not fire' );
544 }
545  
546 # ->watch_after negative time
547 {
548 2 my $done;
549 2 ▶ $loop->watch_time( after => -1, code => sub { $done = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
550  
551 time_between {
552 2 ▶ $loop->loop_once while !$done;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
not $done and do { $loop->loop_once } 2 2 4
553 2 } 0, 0.1, 'loop_once while waiting for negative interval timer';
554 }
555  
556 # self-cancellation
557 {
558 2 my $done;
559  
560 my $id;
561 $id = $loop->watch_time( after => 1 * AUT, code => sub {
562 2 ▶ $loop->unwatch_time( $id ); undef $id;
Subroutine __ANON__: called CC=1 SCAR=0.0
563 2 });
564  
565 $loop->watch_time( after => 1.1 * AUT, code => sub {
566 2 ▶ $done++;
Subroutine __ANON__: called CC=1 SCAR=0.0
567 2 });
568  
569 2 ▶ wait_for { $done };
Subroutine __ANON__: called CC=1 SCAR=0.0
570  
571 2 is( $done, 1, 'Other timers still fire after self-cancelling one' );
572 }
573  
574 SKIP: {
575 2 ▶ skip "Unable to handle sub-second timers accurately", 3 unless $loop->_CAN_SUBSECOND_ACCURATELY;
Branch: 1 branch
BranchTrueFalseTotal
unless $loop->_CAN_SUBSECOND_ACCURATELY 2 0 2
576  
577 # Check that short delays are achievable in one ->loop_once call
578 0 foreach my $delay ( 0.001, 0.01, 0.1 ) {
579 0 my $done;
580 0 my $count = 0;
581 0 my $start = time;
582  
583 0 ▶ $loop->watch_timer( delay => $delay, code => sub { $done++ } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
584  
585 0 ▶ while( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
586 0 $loop->loop_once( 1 );
587 0 $count++;
588 0 ▶ last if time - $start > 5; # bailout
Branch: 1 branch
BranchTrueFalseTotal
if &IO::Async::LoopTests::time() - $start > 5 0 0 0
589 }
590  
591 0 is( $count, 1, "One ->loop_once(1) sufficient for a single $delay second timer" );
592 }
593 }
594  
595 # ->watch_time after post_fork
596 {
597 2 my $done;
598 2 ▶ $loop->watch_time( after => 2 * AUT, code => sub { $done = 1; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
599 2 $loop->post_fork;
600  
601 time_between {
602 2 ▶ my $now = time;
Subroutine __ANON__: called CC=3 SCAR=15.1
603 2 $loop->loop_once( 5 * AUT );
604  
605 # poll might have returned just a little early, such that the TimerQueue
606 # doesn't think anything is ready yet. We need to handle that case.
607 2 ▶ while( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done) 0 2 2
608 0 ▶ die "It should have been ready by now" if( time - $now > 5 * AUT );
Branch: 1 branch
BranchTrueFalseTotal
if &IO::Async::LoopTests::time() - $now > 5 0 0 0
609 0 $loop->loop_once( 0.1 * AUT );
610 }
611 2 } 1.5, 2.5, 'loop_once(5) while waiting for watch_time after';
612 }
613 }
614  
615 =head2 signal
616  
617 Tests the Loop's ability to watch POSIX signals
618  
619 =cut
620  
621 sub run_tests_signal
622 {
623 2 ▶ unless( IO::Async::OS->HAVE_SIGNALS ) {
Subroutine run_tests_signal: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless ("IO::Async::OS"->HAVE_SIGNALS) 0 2 2
624 0 SKIP: { skip "This OS does not have signals", 14; }
625 0 return;
626 }
627  
628 2 my $caught = 0;
629  
630 2 ▶ $loop->watch_signal( TERM => sub { $caught++ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
631  
632 2 is_oneref( $loop, '$loop has refcount 1 after watch_signal' );
633  
634 2 $loop->loop_once( 0.1 );
635  
636 2 is( $caught, 0, '$caught idling' );
637  
638 2 kill SIGTERM, $$;
639  
640 2 is( $caught, 0, '$caught before ->loop_once' );
641  
642 2 $loop->loop_once( 0.1 );
643  
644 2 is( $caught, 1, '$caught after ->loop_once' );
645  
646 2 kill SIGTERM, $$;
647  
648 2 is( $caught, 1, 'second raise is still deferred' );
649  
650 2 $loop->loop_once( 0.1 );
651  
652 2 is( $caught, 2, '$caught after second ->loop_once' );
653  
654 2 is_oneref( $loop, '$loop has refcount 1 before unwatch_signal' );
655  
656 2 $loop->unwatch_signal( 'TERM' );
657  
658 2 is_oneref( $loop, '$loop has refcount 1 after unwatch_signal' );
659  
660 2 my ( $cA, $cB );
661  
662 2 ▶ my $idA = $loop->attach_signal( TERM => sub { $cA = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
663 2 ▶ my $idB = $loop->attach_signal( TERM => sub { $cB = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
664  
665 2 is_oneref( $loop, '$loop has refcount 1 after 2 * attach_signal' );
666  
667 2 kill SIGTERM, $$;
668  
669 2 $loop->loop_once( 0.1 );
670  
671 2 is( $cA, 1, '$cA after raise' );
672 2 is( $cB, 1, '$cB after raise' );
673  
674 2 $loop->detach_signal( 'TERM', $idA );
675  
676 2 undef $cA;
677 2 undef $cB;
678  
679 2 kill SIGTERM, $$;
680  
681 2 $loop->loop_once( 0.1 );
682  
683 2 is( $cA, undef, '$cA after raise' );
684 2 is( $cB, 1, '$cB after raise' );
685  
686 2 $loop->detach_signal( 'TERM', $idB );
687  
688 2 ▶ ok( dies { $loop->attach_signal( 'this signal name does not exist', sub {} ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
689 2 'Bad signal name fails' );
690  
691 2 undef $caught;
692 2 ▶ $loop->attach_signal( TERM => sub { $caught++ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
693  
694 2 $loop->post_fork;
695  
696 2 kill SIGTERM, $$;
697  
698 2 $loop->loop_once( 0.1 );
699  
700 2 is( $caught, 1, '$caught SIGTERM after ->post_fork' );
701 }
702  
703 =head2 idle
704  
705 Tests the Loop's support for idle handlers
706  
707 =cut
708  
709 sub run_tests_idle
710 {
711 2 ▶ my $called = 0;
Subroutine run_tests_idle: called CC=1 SCAR=0.0
Pod undocumented
712  
713 2 ▶ my $id = $loop->watch_idle( when => 'later', code => sub { $called = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
714  
715 2 ok( defined $id, 'idle watcher id is defined' );
716  
717 2 is( $called, 0, 'deferred sub not yet invoked' );
718  
719 2 ▶ time_between { $loop->loop_once( 3 * AUT ) } undef, 1.0, 'loop_once(3) with deferred sub';
Subroutine __ANON__: called CC=1 SCAR=0.0
720  
721 2 is( $called, 1, 'deferred sub called after loop_once' );
722  
723 $loop->watch_idle( when => 'later', code => sub {
724 2 ▶ $loop->watch_idle( when => 'later', code => sub { $called = 2 } )
Subroutine __ANON__: called CC=1 SCAR=0.0
725 2 ▶ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
726  
727 2 $loop->loop_once( 1 );
728  
729 2 is( $called, 1, 'inner deferral not yet invoked' );
730  
731 2 $loop->loop_once( 1 );
732  
733 2 is( $called, 2, 'inner deferral now invoked' );
734  
735 2 $called = 2; # set it anyway in case previous test fails
736  
737 2 ▶ $id = $loop->watch_idle( when => 'later', code => sub { $called = 20 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
738  
739 2 $loop->unwatch_idle( $id );
740  
741 # Some loop types (e.g. UV) need to clear a pending queue first and thus the
742 # first loop_once will take zero time
743 2 $loop->loop_once( 0 );
744  
745 2 ▶ time_between { $loop->loop_once( 1 * AUT ) } 0.5, 1.5, 'loop_once(1) with unwatched deferral';
Subroutine __ANON__: called CC=1 SCAR=0.0
746  
747 2 is( $called, 2, 'unwatched deferral not called' );
748  
749 2 ▶ $id = $loop->watch_idle( when => 'later', code => sub { $called = 3 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
750 2 ▶ my $timer_id = $loop->watch_time( after => 5, code => sub {} );
Subroutine __ANON__: not called CC=1 SCAR=0.0
751  
752 2 $loop->loop_once( 1 );
753  
754 2 is( $called, 3, '$loop->later still invoked with enqueued timer' );
755  
756 2 $loop->unwatch_time( $timer_id );
757  
758 2 ▶ $loop->later( sub { $called = 4 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
759  
760 2 $loop->loop_once( 1 );
761  
762 2 is( $called, 4, '$loop->later shortcut works' );
763 }
764  
765 =head2 process
766  
767 Tests the Loop's support for watching child processes by PID
768  
769 (Previously called C<child>)
770  
771 =cut
772  
773 sub run_in_child(&)
774 {
775 50 ▶ my $kid = fork;
Subroutine run_in_child: called CC=3 SCAR=11.3
Pod undocumented
776 50 ▶ defined $kid or die "Cannot fork() - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $kid or die "Cannot fork() - $!" 0 50 50
777 50 ▶ return $kid if $kid;
Branch: 1 branch
BranchTrueFalseTotal
if $kid 40 10 50
778  
779 10 shift->();
780 0 die "Fell out of run_in_child!\n";
781 }
782  
783 sub run_tests_process
784 {
785 my $kid = run_in_child {
786 2 ▶ exit( 3 );
Subroutine __ANON__: called CC=1 SCAR=0.0
787 12 ▶ };
Subroutine run_tests_process: called CC=3 SCAR=11.0
Pod undocumented
788  
789 10 my $exitcode;
790  
791 10 ▶ $loop->watch_process( $kid => sub { ( undef, $exitcode ) = @_; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
792  
793 10 is_oneref( $loop, '$loop has refcount 1 after watch_process' );
794 10 ok( !defined $exitcode, '$exitcode not defined before ->loop_once' );
795  
796 10 undef $exitcode;
797 10 ▶ wait_for { defined $exitcode };
Subroutine __ANON__: called CC=1 SCAR=0.0
798  
799 10 ok( ($exitcode & 0x7f) == 0, 'WIFEXITED($exitcode) after child exit' );
800 10 is( ($exitcode >> 8), 3, 'WEXITSTATUS($exitcode) after child exit' );
801  
802 SKIP: {
803 10 ▶ skip "This OS does not have signals", 1 unless IO::Async::OS->HAVE_SIGNALS;
Branch: 1 branch
BranchTrueFalseTotal
unless "IO::Async::OS"->HAVE_SIGNALS 0 10 10
804  
805 # We require that SIGTERM perform its default action; i.e. terminate the
806 # process. Ensure this definitely happens, in case the test harness has it
807 # ignored or handled elsewhere.
808 10 local $SIG{TERM} = "DEFAULT";
809  
810 $kid = run_in_child {
811 0 ▶ sleep( 10 );
Subroutine __ANON__: not called CC=1 SCAR=6.9
812 # Just in case the parent died already and didn't kill us
813 0 exit( 0 );
814 10 };
815  
816 10 ▶ $loop->watch_process( $kid => sub { ( undef, $exitcode ) = @_; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
817  
818 10 kill SIGTERM, $kid;
819  
820 10 undef $exitcode;
821 10 ▶ wait_for { defined $exitcode };
Subroutine __ANON__: called CC=1 SCAR=0.0
822  
823 10 is( ($exitcode & 0x7f), SIGTERM, 'WTERMSIG($exitcode) after SIGTERM' );
824 }
825  
826 SKIP: {
827 10 my %kids;
828  
829 10 ▶ $loop->_CAN_WATCH_ALL_PIDS or skip "Loop cannot watch_process for all PIDs", 2;
Branch: 1 branch
BranchTrueFalseTotal
$loop->_CAN_WATCH_ALL_PIDS or skip("Loop cannot watch_process for all PIDs", 2) 0 10 10
830  
831 10 ▶ $loop->watch_process( 0 => sub { my ( $kid ) = @_; delete $kids{$kid} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
832  
833 10 ▶ %kids = map { run_in_child { exit 0 } => 1 } 1 .. 3;
Subroutine __ANON__: called CC=1 SCAR=0.0
834  
835 4 is( scalar keys %kids, 3, 'Waiting for 3 child processes' );
836  
837 4 ▶ wait_for { !keys %kids };
Subroutine __ANON__: called CC=1 SCAR=0.0
838 4 ok( !keys %kids, 'All child processes reclaimed' );
839 }
840  
841 # Legacy API name
842 4 ▶ $kid = run_in_child { exit 2 };
Subroutine __ANON__: called CC=1 SCAR=0.0
843  
844 2 undef $exitcode;
845 2 ▶ $loop->watch_child( $kid => sub { ( undef, $exitcode ) = @_; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
846 2 ▶ wait_for { defined $exitcode };
Subroutine __ANON__: called CC=1 SCAR=0.0
847  
848 2 is( ($exitcode >> 8), 2, '$exitcode after child exit from legacy ->watch_child' );
849 }
850 24 *run_tests_child = \&run_tests_process; # old name
851  
852 =head2 control
853  
854 Tests that the C<run>, C<stop>, C<loop_once> and C<loop_forever> methods
855 behave correctly
856  
857 =cut
858  
859 sub run_tests_control
860 {
861 2 ▶ time_between { $loop->loop_once( 0 ) } 0, 0.1, 'loop_once(0) when idle';
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine run_tests_control: called CC=1 SCAR=0.0
Pod undocumented
862  
863 2 ▶ time_between { $loop->loop_once( 2 * AUT ) } 1.5, 2.5, 'loop_once(2) when idle';
Subroutine __ANON__: called CC=1 SCAR=0.0
864  
865 2 ▶ $loop->watch_time( after => 0.1, code => sub { $loop->stop( result => "here" ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
866  
867 2 ▶ local $SIG{ALRM} = sub { die "Test timed out before ->stop" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
868 2 alarm( 1 );
869  
870 2 my @result = $loop->run;
871  
872 2 alarm( 0 );
873  
874 2 is( \@result, [ result => "here" ], '->stop arguments returned by ->run' );
875  
876 2 ▶ $loop->watch_time( after => 0.1, code => sub { $loop->stop( result => "here" ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
877  
878 2 my $result = $loop->run;
879  
880 2 is( $result, "result", 'First ->stop argument returned by ->run in scalar context' );
881  
882 $loop->watch_time( after => 0.1, code => sub {
883 SKIP: {
884 2 ▶ unless( $loop->can( 'is_running' ) ) {
Subroutine __ANON__: called CC=2 SCAR=7.3
Branch: 1 branch
BranchTrueFalseTotal
unless ($loop->can("is_running")) 0 2 2
885 0 diag "Unsupported \$loop->is_running";
886 0 skip "Unsupported \$loop->is_running", 1;
887 }
888  
889 2 ok( $loop->is_running, '$loop->is_running' );
890 }
891  
892 2 ▶ $loop->watch_time( after => 0.1, code => sub { $loop->stop( "inner" ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
893 2 my @result = $loop->run;
894 2 $loop->stop( @result, "outer" );
895 2 } );
896  
897 2 @result = $loop->run;
898  
899 2 is( \@result, [ "inner", "outer" ], '->run can be nested properly' );
900  
901 2 ▶ $loop->watch_time( after => 0.1, code => sub { $loop->loop_stop } );
Subroutine __ANON__: called CC=1 SCAR=0.0
902  
903 2 ▶ local $SIG{ALRM} = sub { die "Test timed out before ->loop_stop" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
904 2 alarm( 1 );
905  
906 2 $loop->loop_forever;
907  
908 2 alarm( 0 );
909  
910 2 ok( 1, '$loop->loop_forever interruptable by ->loop_stop' );
911 }
912  
913 =head2 metrics
914  
915 Tests that metrics are generated appropriately using L<Metrics::Any>.
916  
917 =cut
918  
919 sub run_tests_metrics
920 {
921 2 ▶ my $loopclass = ref $loop;
Subroutine run_tests_metrics: called CC=2 SCAR=7.0
Pod undocumented
922  
923 2 ▶ return unless $IO::Async::Metrics::METRICS;
Branch: 1 branch
BranchTrueFalseTotal
unless $IO::Async::Metrics::METRICS 0 2 2
924  
925 # We should already at least have the loop-type metric
926 2 is_metrics(
927 {
928 "io_async_loops class:$loopclass" => 1,
929 },
930 'Constructing the loop creates a loop type metric'
931 );
932  
933 # The very first call won't create timing metrics because it isn't armed yet.
934 2 $loop->loop_once( 0 );
935  
936 is_metrics_from(
937 2 ▶ sub { $loop->loop_once( 0.1 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
938 {
939 2 io_async_processing_count => 1,
940 io_async_processing_total => Test::Metrics::Any::positive,
941 },
942 'loop_once(0) creates timing metrics'
943 );
944 }
945  
946 =head1 AUTHOR
947  
948 Paul Evans <leonerd@leonerd.org.uk>
949  
950 =cut
951  
952 24 0x55AA;
« blib/lib/IO/Async/Listener.pm ↑ Summary blib/lib/IO/Async/Timer/Periodic.pm »