File Coverage

blib/lib/Test/Future/Deferred.pm
Criterion Covered Total %
statement 22 22 100.0
branch 3 6 50.0
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 37 40 92.5


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