File: Deferred.pm

package info (click to toggle)
libfuture-perl 0.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: perl: 4,636; makefile: 2
file content (120 lines) | stat: -rw-r--r-- 2,670 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2018 -- leonerd@leonerd.org.uk

package Test::Future::Deferred 0.52;

use v5.14;
use warnings;
use base qw( Future );

use Carp;

=head1 NAME

C<Test::Future::Deferred> - a future which completes later

=head1 SYNOPSIS

=for highlighter language=perl

   my $future = Test::Future::Deferred->done_later( 1, 2, 3 );

   # Future is not ready yet

   my @result = $future->get;

=head1 DESCRIPTION

This subclass of L<Future> provides two new methods and an implementation of
the C<await> interface, which allows the futures to appear pending at first,
but then to complete when C<get> is called at the toplevel on one of them.

This behaviour is useful in unit tests to check that behaviour of a module
under test is correct even with non-immediate futures, as it allows a future
to easily be constructed that will complete "soon", but not yet, without
needing an event loop.

Because these futures provide their own C<await> method, they shouldn't be
mixed in the same program with other kinds of futures from real event systems
or similar.

=cut

=head1 METHODS

=cut

=head2 flush

   Test::Future::Deferred->flush

I<Since version 0.52.>

Invokes all of the currently-pending deferrals, allowing any "later"-queued
activities to take place.

=cut

my @deferrals;

sub flush
{
   while( my $d = shift @deferrals ) {
      my ( $f, $method, @args ) = @$d;
      $f->$method( @args );
   }
}

sub await
{
   Test::Future::Deferred->flush;

   $_[0]->is_ready or
      croak "$_[0] ran out of things to do and is still not ready";
}

=head2 done_later

   $f->done_later( @args );

Equivalent to invoking the regular C<done> method as part of the C<await>
operation called on the toplevel future. This makes the future complete with
the given result, but only when C<get> is called.

=cut

sub done_later
{
   my $self = ref $_[0] ? shift : shift->new;
   push @deferrals, [ $self, done => @_ ];
   return $self;
}

=head2 fail_later

   $f->fail_later( $message, $category, @details );

Equivalent to invoking the regular C<fail> method as part of the C<await>
operation called on the toplevel future. This makes the future complete with
the given failure, but only when C<get> is called. As the C<failure> method
also waits for completion of the future, then it will return the failure
message given here also.

=cut

sub fail_later
{
   my $self = ref $_[0] ? shift : shift->new;
   push @deferrals, [ $self, fail => @_ ];
   return $self;
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;