| 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, 2018-2026 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Test::Future::Deferred 0.54; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use base qw( Future ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use feature qw( signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | no warnings qw( experimental::signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | =head1 NAME | ||||||||||
| 18 | |||||||||||
| 19 | C<Test::Future::Deferred> - a future which completes later | ||||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | =for highlighter language=perl | ||||||||||
| 24 | |||||||||||
| 25 | my $future = Test::Future::Deferred->done_later( 1, 2, 3 ); | ||||||||||
| 26 | |||||||||||
| 27 | # Future is not ready yet | ||||||||||
| 28 | |||||||||||
| 29 | my @result = $future->get; | ||||||||||
| 30 | |||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||
| 32 | |||||||||||
| 33 | This subclass of L<Future> provides two new methods and an implementation of | ||||||||||
| 34 | the C<await> interface, which allows the futures to appear pending at first, | ||||||||||
| 35 | but then to complete when C<get> is called at the toplevel on one of them. | ||||||||||
| 36 | |||||||||||
| 37 | This behaviour is useful in unit tests to check that behaviour of a module | ||||||||||
| 38 | under test is correct even with non-immediate futures, as it allows a future | ||||||||||
| 39 | to easily be constructed that will complete "soon", but not yet, without | ||||||||||
| 40 | needing an event loop. | ||||||||||
| 41 | |||||||||||
| 42 | Because these futures provide their own C<await> method, they shouldn't be | ||||||||||
| 43 | mixed in the same program with other kinds of futures from real event systems | ||||||||||
| 44 | or similar. | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | =head1 METHODS | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | =head2 flush | ||||||||||
| 53 | |||||||||||
| 54 | Test::Future::Deferred->flush; | ||||||||||
| 55 | |||||||||||
| 56 | I<Since version 0.52.> | ||||||||||
| 57 | |||||||||||
| 58 | Invokes all of the currently-pending deferrals, allowing any "later"-queued | ||||||||||
| 59 | activities to take place. | ||||||||||
| 60 | |||||||||||
| 61 | =cut | ||||||||||
| 62 | |||||||||||
| 63 | 1 | my @deferrals; | |||||||||
| 64 | |||||||||||
| 65 | 4 | ▶ | sub flush ( $ = undef ) | ||||||||
|
Subroutine
flush: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 66 | 4 | { | |||||||||
| 67 | 4 | ▶ | while( my $d = shift @deferrals ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 4 | my ( $f, $method, @args ) = @$d; | |||||||||
| 69 | 4 | $f->$method( @args ); | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 3 | ▶ | sub await ( $self ) | ||||||||
|
Subroutine
await: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 74 | { | ||||||||||
| 75 | 3 | Test::Future::Deferred->flush; | |||||||||
| 76 | |||||||||||
| 77 | 3 | ▶ | $self->is_ready or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | croak "$self ran out of things to do and is still not ready"; | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | =head2 done_later | ||||||||||
| 82 | |||||||||||
| 83 | $f->done_later( @args ); | ||||||||||
| 84 | |||||||||||
| 85 | Equivalent to invoking the regular C<done> method as part of the C<await> | ||||||||||
| 86 | operation called on the toplevel future. This makes the future complete with | ||||||||||
| 87 | the given result, but only when C<get> is called. | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||
| 90 | |||||||||||
| 91 | 2 | ▶ | sub done_later ( $self, @result ) | ||||||||
|
Subroutine
done_later: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 92 | { | ||||||||||
| 93 | 2 | ▶ | ref $self or $self = $self->new; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 2 | push @deferrals, [ $self, done => @result ]; | |||||||||
| 95 | 2 | return $self; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | =head2 fail_later | ||||||||||
| 99 | |||||||||||
| 100 | $f->fail_later( $message, $category, @details ); | ||||||||||
| 101 | |||||||||||
| 102 | Equivalent to invoking the regular C<fail> method as part of the C<await> | ||||||||||
| 103 | operation called on the toplevel future. This makes the future complete with | ||||||||||
| 104 | the given failure, but only when C<get> is called. As the C<failure> method | ||||||||||
| 105 | also waits for completion of the future, then it will return the failure | ||||||||||
| 106 | message given here also. | ||||||||||
| 107 | |||||||||||
| 108 | =cut | ||||||||||
| 109 | |||||||||||
| 110 | 2 | ▶ | sub fail_later ( $self, @failure ) | ||||||||
|
Subroutine
fail_later: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 111 | { | ||||||||||
| 112 | 2 | ▶ | ref $self or $self = $self->new; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 2 | push @deferrals, [ $self, fail => @failure ]; | |||||||||
| 114 | 2 | return $self; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | =head1 AUTHOR | ||||||||||
| 118 | |||||||||||
| 119 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 1 | 0x55AA; | |||||||||