Test/Future.pm

filter: Statementstmt 94.4% 51 / 54 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 90.5% 67 / 74 CC 6 SCAR 8.1
CC
6
Cov
90%
__ANON__
6.0
__ANON__
3.0
no_pending_futures
2.0
CRAP 2.3
« Future.pm ↑ Summary Future/Mutex.pm »
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, 2014-2015 -- leonerd@leonerd.org.uk
5  
6 package Test::Future 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( Test::Builder::Module );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Don't use feature 'signatures' in this module yet, because until perl 5.28
13 # it's awkward to use both signatures and (&...)-style prototypes at the same
14 # time.
15  
16 1 our @EXPORT = qw(
17 no_pending_futures
18 );
19  
20 1 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use constant HAVE_DEVEL_MAT_DUMPER => defined eval { require Devel::MAT::Dumper };
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 C<Test::Future> - unit test assertions for L<Future> instances
27  
28 =head1 SYNOPSIS
29  
30 =for highlighter language=perl
31  
32 use Test::More tests => 2;
33 use Test::Future;
34  
35 no_pending_futures {
36 my $f = some_function();
37  
38 is( $f->get, "result", 'Result of the some_function()' );
39 } 'some_function() leaves no pending Futures';
40  
41 =head1 DESCRIPTION
42  
43 This module provides unit testing assertions that may be useful when testing
44 code based on, or using L<Future> instances or subclasses.
45  
46 =cut
47  
48 =head1 FUNCTIONS
49  
50 =cut
51  
52 =head2 no_pending_futures
53  
54 no_pending_futures( \&code, $name );
55  
56 I<Since version 0.29.>
57  
58 Runs the given block of code, while keeping track of every C<Future> instance
59 constructed while doing so. After the code has returned, each of these
60 instances are inspected to check that they are not still pending. If they are
61 all either ready (by success or failure) or cancelled, the test will pass. If
62 any are still pending then the test fails.
63  
64 If L<Devel::MAT> is installed, it will be used to write a memory state dump
65 after a failure. It will create a F<.pmat> file named the same as the unit
66 test, but with the trailing F<.t> suffix replaced with F<-TEST.pmat> where
67 C<TEST> is the number of the test that failed (in case there was more than
68 one). A list of addresses of C<Future> instances that are still pending is
69 also printed to assist in debugging the issue.
70  
71 It is not an error if the code does not construct any C<Future> instances at
72 all. The block of code may contain other testing assertions; they will be run
73 before the assertion by C<no_pending_futures> itself.
74  
75 =cut
76  
77 sub no_pending_futures(&@)
78 4 ▶{
Subroutine no_pending_futures: called CC=2 SCAR=7.0
Pod documented
79 4 my ( $code, $name ) = @_;
80  
81 4 my @futures;
82  
83 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 4 my $new = Future->can( "new" );
86 4 ▶ local *Future::new = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 4 my $f = $new->(@_);
88 4 push @futures, $f;
89 2 ▶ $f->on_ready( sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
90 2 my $f = shift;
91 2 for ( 0 .. $#futures ) {
92 2 ▶ refaddr( $futures[$_] ) == refaddr( $f ) or next;
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($futures[$_]) == builtin::refaddr($f) or next 2 0 2
93  
94 2 splice @futures, $_, 1, ();
95 2 return;
96 }
97 4 });
98 4 return $f;
99 4 };
100  
101 4 my $done = Future->can( "done" );
102 3 ▶ local *Future::done = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
103 3 my $f = $done->(@_);
104 3 ▶ pop @futures if !ref $_[0]; # class method
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 2 1 3
105 3 return $f;
106 4 };
107  
108 4 my $fail = Future->can( "fail" );
109 0 ▶ local *Future::fail = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
110 0 my $f = $fail->(@_);
111 0 ▶ pop @futures if !ref $_[0]; # class method
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
112 0 return $f;
113 4 };
114  
115 4 my $tb = __PACKAGE__->builder;
116  
117 4 $code->();
118  
119 4 my @pending = grep { !$_->is_ready } @futures;
120  
121 4 ▶ return $tb->ok( 1, $name ) if !@pending;
Branch: 1 branch
BranchTrueFalseTotal
unless @pending 3 1 4
122  
123 1 my $ok = $tb->ok( 0, $name );
124  
125 1 $tb->diag( "The following Futures are still pending:" );
126 1 $tb->diag( join ", ", map { sprintf "0x%x", refaddr $_ } @pending );
127  
128 1 if( HAVE_DEVEL_MAT_DUMPER ) {
129 my $file = $0;
130 my $num = $tb->current_test;
131  
132 # Trim the .t off first then append -$num.pmat, in case $0 wasn't a .t file
133 $file =~ s/\.(?:t|pm|pl)$//;
134 $file .= "-$num.pmat";
135  
136 $tb->diag( "Writing heap dump to $file" );
137 Devel::MAT::Dumper::dump( $file );
138 }
139  
140 1 return $ok;
141 }
142  
143 =head1 AUTHOR
144  
145 Paul Evans <leonerd@leonerd.org.uk>
146  
147 =cut
148  
149 1 0x55AA;
« Future.pm ↑ Summary Future/Mutex.pm »