.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "IO::Async::Test 3pm" .TH IO::Async::Test 3pm 2024-02-04 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME "IO::Async::Test" \- utility functions for use in test scripts .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& use Test2::V0; \& use Future::AsyncAwait; \& use IO::Async::Test; \& \& use IO::Async::Loop; \& my $loop = IO::Async::Loop\->new; \& testing_loop( $loop ); \& \& my $result; \& \& $loop\->do_something( \& some => args, \& \& on_done => sub { \& $result = the_outcome; \& } \& ); \& \& wait_for { defined $result }; \& \& is( $result, what_we_expected, \*(AqThe event happened\*(Aq ); \& \& ... \& \& my $buffer = ""; \& my $handle = IO::Handle\-> ... \& \& wait_for_stream { length $buffer >= 10 } $handle => $buffer; \& \& is( substr( $buffer, 0, 10, "" ), "0123456789", \*(AqBuffer was correct\*(Aq ); \& \& my $result = await wait_for_future( $stream\->read_until( "\en" ) ); \& \& done_testing; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This module provides utility functions that may be useful when writing test scripts for code which uses IO::Async (as well as being used in the IO::Async test scripts themselves). .PP Test scripts are often synchronous by nature; they are a linear sequence of actions to perform, interspersed with assertions which check for given conditions. This goes against the very nature of IO::Async which, being an asynchronisation framework, does not provide a linear stepped way of working. .PP In order to write a test, the \f(CW\*(C`wait_for\*(C'\fR function provides a way of synchronising the code, so that a given condition is known to hold, which would typically signify that some event has occurred, the outcome of which can now be tested using the usual testing primitives. .PP Because the primary purpose of IO::Async is to provide IO operations on filehandles, a great many tests will likely be based around connected pipes or socket handles. The \f(CW\*(C`wait_for_stream\*(C'\fR function provides a convenient way to wait for some content to be written through such a connected stream. .SH FUNCTIONS .IX Header "FUNCTIONS" .SS testing_loop .IX Subsection "testing_loop" .Vb 1 \& testing_loop( $loop ); .Ve .PP Set the IO::Async::Loop object which the \f(CW\*(C`wait_for\*(C'\fR function will loop on. .SS wait_for .IX Subsection "wait_for" .Vb 1 \& wait_for { COND } OPTS; .Ve .PP Repeatedly call the \f(CW\*(C`loop_once\*(C'\fR method on the underlying loop (given to the \&\f(CW\*(C`testing_loop\*(C'\fR function), until the given condition function callback returns true. .PP To guard against stalled scripts, if the loop indicates a timeout for (a default of) 10 consequentive seconds, then an error is thrown. .PP Takes the following named options: .IP "timeout => NUM" 4 .IX Item "timeout => NUM" The time in seconds to wait before giving up the test as being stalled. Defaults to 10 seconds. .SS wait_for_stream .IX Subsection "wait_for_stream" .Vb 1 \& wait_for_stream { COND } $handle, $buffer; .Ve .PP As \f(CW\*(C`wait_for\*(C'\fR, but will also watch the given IO handle for readability, and whenever it is readable will read bytes in from it into the given buffer. The buffer is NOT initialised when the function is entered, in case data remains from a previous call. .PP \&\f(CW$buffer\fR can also be a CODE reference, in which case it will be invoked being passed data read from the handle, whenever it is readable. .SS wait_for_future .IX Subsection "wait_for_future" .Vb 1 \& $future = wait_for_future $future; .Ve .PP \&\fISince version 0.68.\fR .PP A handy wrapper around using \f(CW\*(C`wait_for\*(C'\fR to wait for a Future to become ready. The future instance itself is returned, allowing neater code. .SH AUTHOR .IX Header "AUTHOR" Paul Evans