| 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, 2007-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IO::Async::Test 0.805; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 94 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 94 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 94 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 94 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 13 | testing_loop | ||||||||||||||||||||||||||||
| 14 | wait_for | ||||||||||||||||||||||||||||
| 15 | wait_for_stream | ||||||||||||||||||||||||||||
| 16 | wait_for_future | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | C<IO::Async::Test> - utility functions for use in test scripts | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =for highlighter language=perl | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use Test2::V0; | ||||||||||||||||||||||||||||
| 28 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||
| 29 | use IO::Async::Test; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use IO::Async::Loop; | ||||||||||||||||||||||||||||
| 32 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||
| 33 | testing_loop( $loop ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my $result; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $loop->do_something( | ||||||||||||||||||||||||||||
| 38 | some => args, | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | on_done => sub { | ||||||||||||||||||||||||||||
| 41 | $result = the_outcome; | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | wait_for { defined $result }; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | is( $result, what_we_expected, 'The event happened' ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | ... | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $buffer = ""; | ||||||||||||||||||||||||||||
| 52 | my $handle = IO::Handle-> ... | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | wait_for_stream { length $buffer >= 10 } $handle => $buffer; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | is( substr( $buffer, 0, 10, "" ), "0123456789", 'Buffer was correct' ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | my $result = await wait_for_future( $stream->read_until( "\n" ) ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | done_testing; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | This module provides utility functions that may be useful when writing test | ||||||||||||||||||||||||||||
| 65 | scripts for code which uses L<IO::Async> (as well as being used in the | ||||||||||||||||||||||||||||
| 66 | L<IO::Async> test scripts themselves). | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Test scripts are often synchronous by nature; they are a linear sequence of | ||||||||||||||||||||||||||||
| 69 | actions to perform, interspersed with assertions which check for given | ||||||||||||||||||||||||||||
| 70 | conditions. This goes against the very nature of L<IO::Async> which, being an | ||||||||||||||||||||||||||||
| 71 | asynchronisation framework, does not provide a linear stepped way of working. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | In order to write a test, the C<wait_for> function provides a way of | ||||||||||||||||||||||||||||
| 74 | synchronising the code, so that a given condition is known to hold, which | ||||||||||||||||||||||||||||
| 75 | would typically signify that some event has occurred, the outcome of which can | ||||||||||||||||||||||||||||
| 76 | now be tested using the usual testing primitives. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Because the primary purpose of L<IO::Async> is to provide IO operations on | ||||||||||||||||||||||||||||
| 79 | filehandles, a great many tests will likely be based around connected pipes or | ||||||||||||||||||||||||||||
| 80 | socket handles. The C<wait_for_stream> function provides a convenient way | ||||||||||||||||||||||||||||
| 81 | to wait for some content to be written through such a connected stream. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 94 | my $loop; | |||||||||||||||||||||||||||
| 86 | 66 | ▶ | END { undef $loop } | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 testing_loop | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | testing_loop( $loop ); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Set the L<IO::Async::Loop> object which the C<wait_for> function will loop | ||||||||||||||||||||||||||||
| 97 | on. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub testing_loop | ||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 122 | ▶ | $loop = shift; | ||||||||||||||||||||||||||
|
Subroutine
testing_loop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 wait_for | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | wait_for { COND } OPTS; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Repeatedly call the C<loop_once> method on the underlying loop (given to the | ||||||||||||||||||||||||||||
| 111 | C<testing_loop> function), until the given condition function callback | ||||||||||||||||||||||||||||
| 112 | returns true. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | To guard against stalled scripts, if the loop indicates a timeout for (a | ||||||||||||||||||||||||||||
| 115 | default of) 10 consequentive seconds, then an error is thrown. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Takes the following named options: | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =over 4 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =item timeout => NUM | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | The time in seconds to wait before giving up the test as being stalled. | ||||||||||||||||||||||||||||
| 124 | Defaults to 10 seconds. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =back | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 94 | our $Level = 0; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub wait_for(&@) | ||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 477 | ▶ | my ( $cond, %opts ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
wait_for: called CC=5 SCAR=17.1
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 477 | my ( undef, $callerfile, $callerline ) = caller( $Level ); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 477 | my $timedout = 0; | |||||||||||||||||||||||||||
| 139 | my $timerid = $loop->watch_time( | ||||||||||||||||||||||||||||
| 140 | after => $opts{timeout} // 10, | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | code => sub { $timedout = 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 142 | 477 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"timeout"} // 10
MC/DC0%0/1
$opts{"timeout"} // 10
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 477 | ▶ | $loop->loop_once( 1 ) while !$cond->() and !$timedout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&$cond() or $timedout
MC/DC50%1/2
&$cond() or $timedout
&$cond()
$timedout
Truth table66%2/3
A or B
A= &$cond()B= $timedout
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 475 | ▶ | if( $timedout ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | die "Nothing was ready after 10 second wait; called at $callerfile line $callerline\n"; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||
| 150 | 475 | $loop->unwatch_time( $timerid ); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head2 wait_for_stream | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | wait_for_stream { COND } $handle, $buffer; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | As C<wait_for>, but will also watch the given IO handle for readability, and | ||||||||||||||||||||||||||||
| 159 | whenever it is readable will read bytes in from it into the given buffer. The | ||||||||||||||||||||||||||||
| 160 | buffer is NOT initialised when the function is entered, in case data remains | ||||||||||||||||||||||||||||
| 161 | from a previous call. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | C<$buffer> can also be a CODE reference, in which case it will be invoked | ||||||||||||||||||||||||||||
| 164 | being passed data read from the handle, whenever it is readable. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | sub wait_for_stream(&$$) | ||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||
| 170 | 7 | ▶ | my ( $cond, $handle, undef ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
wait_for_stream: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 7 | my $on_read; | |||||||||||||||||||||||||||
| 173 | 7 | ▶ | if( ref $_[2] eq "CODE" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 1 | $on_read = $_[2]; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | else { | ||||||||||||||||||||||||||||
| 177 | 6 | my $varref = \$_[2]; | |||||||||||||||||||||||||||
| 178 | 6 | ▶ | $on_read = sub { $$varref .= $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | $loop->watch_io( | ||||||||||||||||||||||||||||
| 182 | handle => $handle, | ||||||||||||||||||||||||||||
| 183 | on_read_ready => sub { | ||||||||||||||||||||||||||||
| 184 | 7 | ▶ | my $ret = $handle->sysread( my $buffer, 8192 ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 185 | 7 | ▶ | if( !defined $ret ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 186 | 0 | die "Read failed on $handle - $!\n"; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | elsif( $ret == 0 ) { | ||||||||||||||||||||||||||||
| 189 | 0 | die "Read returned EOF on $handle\n"; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 7 | $on_read->( $buffer ); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 7 | ); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 7 | local $Level = $Level + 1; | |||||||||||||||||||||||||||
| 196 | # Have to defeat the prototype... grr I hate these | ||||||||||||||||||||||||||||
| 197 | 7 | &wait_for( $cond ); | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 7 | $loop->unwatch_io( | |||||||||||||||||||||||||||
| 200 | handle => $handle, | ||||||||||||||||||||||||||||
| 201 | on_read_ready => 1, | ||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 wait_for_future | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | $future = wait_for_future $future; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | I<Since version 0.68.> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | A handy wrapper around using C<wait_for> to wait for a L<Future> to become | ||||||||||||||||||||||||||||
| 212 | ready. The future instance itself is returned, allowing neater code. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub wait_for_future | ||||||||||||||||||||||||||||
| 217 | { | ||||||||||||||||||||||||||||
| 218 | 40 | ▶ | my ( $future ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
wait_for_future: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 40 | local $Level = $Level + 1; | |||||||||||||||||||||||||||
| 221 | 40 | ▶ | wait_for { $future->is_ready }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 40 | return $future; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 94 | 0x55AA; | |||||||||||||||||||||||||||