IO::Async::Test(3pm)
π IO::Async::Test(3pm) β bestie edition π
okay so this is a page from the User Contributed Perl Documentation, and it's literally called IO::Async::Test β utility functions for use in test scripts. yes babe, testing tools. we love a supportive module. π«Ά
β¨ NAME β¨
IO::Async::Test β utility functions for use in test scripts. that's the whole vibe. π
πΈ SYNOPSIS πΈ
like, here's the whole thing in action, no cap:
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, 'The event happened' );
...
my $buffer = "";
my $handle = IO::Handle-> ...
wait_for_stream { length $buffer >= 10 } $handle => $buffer;
is( substr( $buffer, 0, 10, "" ), "0123456789", 'Buffer was correct' );
my $result = await wait_for_future( $stream->read_until( "\n" ) );
done_testing;
periodt. that's the whole flow. β¨
π¦ DESCRIPTION π¦
so this module hands you utility functions that are useful when you're writing test scripts for code that uses IO::Async β and btw, it's also used in the IO::Async test scripts themselves. icon behaviour. π
here's the tea, bestie: test scripts are often synchronous by nature. they're just a linear sequence of actions to perform, with assertions sprinkled in between checking for given conditions. and that lowkey clashes with the very nature of IO::Async, because IO::Async is an asynchronisation framework and it just does not give you a linear stepped way of working. no linear steps for you. π
so, in order to write a test, the wait_for function gives you a way of synchronising the code β so that a given condition is known to hold. which typically means: some event happened, and now you can test the outcome using the usual testing primitives. slay.
and because the primary purpose of IO::Async is to provide IO operations on filehandles, a whole lot of tests are gonna be based around connected pipes or socket handles. so the wait_for_stream function is there to give you a convenient way to wait for some content to be written through such a connected stream. it's giving convenience. π
π₯ FUNCTIONS π₯
π testing_loop π
testing_loop( $loop );
this sets the IO::Async::Loop object that the wait_for function is gonna loop on. that's it, that's the job. π
π wait_for π
wait_for { COND } OPTS;
it repeatedly calls the loop_once method on the underlying loop (the one you gave to the testing_loop function) until the given condition function callback returns true. over and over and over until the vibe is right. β
bestie, real talk: to guard against stalled scripts, if the loop indicates a timeout for (a default of) 10 consecutive seconds, then an error is thrown. so don't be out here waiting forever. π
Takes the following named options:
timeout => NUMβ the time in seconds to wait before giving up the test as being stalled. Defaults to10seconds. π
π§ wait_for_stream π§
wait_for_stream { COND } $handle, $buffer;
it's like wait_for, but it will also watch the given IO handle for readability, and whenever it's readable it'll read bytes in from it into the given buffer. π
careful babes: the buffer is NOT initialised when the function is entered β in case data remains from a previous call. so like, whatever was in there is still in there. π
$buffer can also be a CODE reference β in which case it gets invoked, being passed data read from the handle, whenever it's readable. yes, a coderef as your buffer, we're flexible like that. π
π wait_for_future π
$future = wait_for_future $future;
Since version 0.68.
this is a handy wrapper around using wait_for to wait for a Future to become ready. and the future instance itself is returned, which lets you write neater code. it's giving neat. β¨
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk> π
okay that's the whole page bestie, go forth and test your async code like the main character you are. no notes. πΈπ¦β¨
sign-off time: this was perl v5.40.1, dated 2026-02-21, and the page you just read is IO::Async::Test(3pm). mwah. π
π
π¦ where this page comes from
- ubuntu stonking: libio-async-perl 0.805-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms