1IO::Async::Test(3) User Contributed Perl Documentation IO::Async::Test(3)
2
3
4
6 "IO::Async::Test" - utility functions for use in test scripts
7
9 use Test::More tests => 1;
10 use IO::Async::Test;
11
12 use IO::Async::Loop;
13 my $loop = IO::Async::Loop->new();
14 testing_loop( $loop );
15
16 my $result;
17
18 $loop->do_something(
19 some => args,
20
21 on_done => sub {
22 $result = the_outcome;
23 }
24 );
25
26 wait_for { defined $result };
27
28 is( $result, what_we_expected, 'The event happened' );
29
30 ...
31
32 my $buffer = "";
33 my $handle = IO::Handle-> ...
34
35 wait_for_stream { length $buffer >= 10 } $handle => $buffer;
36
37 is( substr( $buffer, 0, 10, "" ), "0123456789", 'Buffer was correct' );
38
40 This module provides utility functions that may be useful when writing
41 test scripts for code which uses "IO::Async" (as well as being used in
42 the "IO::Async" test scripts themselves).
43
44 Test scripts are often synchronous by nature; they are a linear
45 sequence of actions to perform, interspersed with assertions which
46 check for given conditions. This goes against the very nature of
47 "IO::Async" which, being an asynchronisation framework, does not
48 provide a linear stepped way of working.
49
50 In order to write a test, the "wait_for()" function provides a way of
51 synchronising the code, so that a given condition is known to hold,
52 which would typically signify that some event has occured, the outcome
53 of which can now be tested using the usual testing primitives.
54
55 Because the primary purpose of "IO::Async" is to provide IO operations
56 on filehandles, a great many tests will likely be based around
57 connected pipes or socket handles. The "wait_for_stream()" function
58 provides a convenient way to wait for some content to be written
59 through such a connected stream.
60
62 testing_loop( $loop )
63 Set the "IO::Async::Loop" object which the "wait_for()" function will
64 loop on.
65
66 wait_for( $condfunc )
67 Repeatedly call the "loop_once()" method on the underlying loop (given
68 to the "testing_loop()" function), until the given condition function
69 callback returns true.
70
71 To guard against stalled scripts, if the loop indicates a timeout for
72 10 consequentive seconds, then an error is thrown.
73
74 wait_for_stream( $condfunc, $handle, $buffer )
75 Set up an "IO::Async::Stream" object around the given $handle. Data
76 read from the stream will be appended into $buffer (which is NOT
77 initialised when the function is entered, in case data remains from a
78 previous call). The "loop_once" method is then repeatedly called until
79 the condition function callback returns true. After this, the temporary
80 stream will be removed from the loop.
81
83 Paul Evans <leonerd@leonerd.org.uk>
84
85
86
87perl v5.12.1 2010-06-09 IO::Async::Test(3)