blib/lib/DBIO/Test/Future.pm

filter: Statementstmt 100% 102 / 102 Branchbran 85.4% 41 / 48 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 12 totaltotal 88.7% 174 / 196 CC 28 SCAR 10.5
CC
28
Cov
95%
_run_callback
6.0
__ANON__
4.1
get
4.0
CRAP 2.9
« blib/lib/DBIO/ResultSet/Pager.pm ↑ Summary blib/lib/DBIO/DeploymentHandler/VersionStorage/Standard/Component.pm »
1 package DBIO::Test::Future;
2 # ABSTRACT: Manually settled Future for deterministic DBIO tests
3  
4 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 116 our $VERSION = '0.900007';
8  
9 116 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 116 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Also callable on an instance, for a new pending Future of the same class --
13 # the Future.pm idiom core's listen machinery relies on (karr k222).
14 27677 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
15 27677 my ($class) = @_;
16 27677 ▶ return bless {
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
398272790
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
17 state => 'pending',
18 values => [],
19 callbacks => [],
20 }, ref($class) || $class;
21 }
22  
23 24420 ▶sub done {
Subroutine done: called CC=2 SCAR=6.9
Pod undocumented
24 24420 my $invocant = shift;
25 24420 ▶ my $self = ref $invocant ? $invocant : $invocant->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 15828 8592 24420
26 24420 return $self->_settle(done => @_);
27 }
28  
29 2078 ▶sub fail {
Subroutine fail: called CC=2 SCAR=6.9
Pod undocumented
30 2078 my $invocant = shift;
31 2078 ▶ my $self = ref $invocant ? $invocant : $invocant->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 1480 598 2078
32 2078 return $self->_settle(failed => @_);
33 }
34  
35 26498 ▶sub _settle {
Subroutine _settle: called CC=2 SCAR=6.9
36 26498 my ($self, $state, @values) = @_;
37 DBIO::Exception->throw('DBIO::Test::Future is already settled')
38 26498 ▶ unless $self->{state} eq 'pending';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"} eq "pending" 4 26494 26498
39 26494 $self->{state} = $state;
40 26494 $self->{values} = \@values;
41 26494 $self->_drain_callbacks;
42 26494 return $self;
43 }
44  
45 301037 ▶sub is_ready { return $_[0]{state} ne 'pending' }
Subroutine is_ready: called CC=1 SCAR=0.0
Pod undocumented
46 19403 ▶sub is_failed { return $_[0]{state} eq 'failed' }
Subroutine is_failed: called CC=1 SCAR=0.0
Pod undocumented
47  
48 # Data a test lays on a Future, as Future.pm's set_udata / udata (Future 0.50):
49 # the way that works for Future::XS objects too, which are no hashes.
50 2 ▶sub set_udata {
Subroutine set_udata: called CC=1 SCAR=0.0
Pod undocumented
51 2 my ($self, $name, $value) = @_;
52 2 $self->{"u_$name"} = $value;
53 2 return $self;
54 }
55  
56 1 ▶sub udata {
Subroutine udata: called CC=1 SCAR=0.0
Pod undocumented
57 1 my ($self, $name) = @_;
58 1 return $self->{"u_$name"};
59 }
60  
61 1625 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod undocumented
62 1625 my ($self) = @_;
63 1625 ▶ DBIO::Exception->throw('Cannot get a pending DBIO::Test::Future')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_ready 1 1624 1625
64 unless $self->is_ready;
65 1624 ▶ DBIO::Exception->throw($self->{values}[0])
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_failed 287 1337 1624
66 if $self->is_failed;
67 1337 ▶ return wantarray ? @{ $self->{values} } : $self->{values}[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 260 1077 1337
68 }
69  
70 15675 ▶sub then {
Subroutine then: called CC=2 SCAR=6.9
Pod undocumented
71 15675 my ($self, $on_done, $on_fail) = @_;
72 15675 my $derived = ref($self)->new;
73 15675 push @{ $self->{callbacks} }, [ $derived, $on_done, $on_fail ];
74 15675 ▶ $self->_drain_callbacks if $self->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ready 14049 1626 15675
75 15675 return $derived;
76 }
77  
78 226 ▶sub catch {
Subroutine catch: called CC=1 SCAR=0.0
Pod undocumented
79 226 my ($self, $on_fail) = @_;
80 226 return $self->then(undef, $on_fail);
81 }
82  
83 # A derived Future is held strongly -- here, and in the closures below -- so the
84 # chain is held from its head: a ->then result nobody holds still runs its
85 # callback. It can be gone only in global destruction, where Perl clears
86 # references to objects in no particular order; nobody is left to be told
87 # there, so its callback does not run and nothing is settled (karr core k257).
88 40543 ▶sub _drain_callbacks {
Subroutine _drain_callbacks: called CC=3 SCAR=11.0
89 40543 my ($self) = @_;
90 40543 ▶ while (my $entry = shift @{ $self->{callbacks} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @{$$self{"callbacks"};}) 15219 40543 55762
91 15219 ▶ next unless defined $entry->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->[0] 0 15219 15219
92 15219 $self->_run_callback(@$entry);
93 }
94 40543 return;
95 }
96  
97 15219 ▶sub _run_callback {
Subroutine _run_callback: called CC=6 SCAR=17.9
98 15219 my ($self, $derived, $on_done, $on_fail) = @_;
99 15219 ▶ my $callback = $self->is_failed ? $on_fail : $on_done;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_failed ? : 1634 13585 15219
100  
101 15219 ▶ unless ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callback) 435 14784 15219
102 return $self->is_failed
103 286 ? $derived->fail(@{ $self->{values} })
104 435 ▶ : $derived->done(@{ $self->{values} });
Branch: 1 branch
BranchTrueFalseTotal
$self->is_failed ? : 286 149 435
105 }
106  
107 14784 my ($ok, $result, $error);
108 {
109 14784 local $@;
110 14784 $ok = eval {
111 14784 $result = scalar $callback->(@{ $self->{values} });
112 14764 1;
113 };
114 14784 ▶ $error = $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 20 14764 14784
115 }
116 14784 ▶ return $derived->fail($error) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 20 14764 14784
117 14764 return $self->_adopt_result($derived, $result);
118 }
119  
120 14764 ▶sub _adopt_result {
Subroutine _adopt_result: called CC=3 SCAR=11.0
121 14764 my ($self, $derived, $result) = @_;
122 14764 ▶ unless (Scalar::Util::blessed($result)
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($result) and $result->isa("DBIO::Test::Future")) 7713 7051 14764
Condition100%3/3
builtin::blessed($result) and $result->isa("DBIO::Test::Future")
!ll&&!rl&&r
75831307051
MC/DC100%2/2
builtin::blessed($result) and $result->isa("DBIO::Test::Future")
builtin::blessed($result) $result->isa("DBIO::Test::Future")
Truth table100%3/3
A and B
A= builtin::blessed($result)B= $result->isa("DBIO::Test::Future")
ABresult
0X0
100
111
123 && $result->isa('DBIO::Test::Future')) {
124 7713 return $derived->done($result);
125 }
126  
127 $result->then(
128 6163 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
129 6163 ▶ $derived->done(@_) if defined $derived;
Branch: 1 branch
BranchTrueFalseTotal
if defined $derived 6163 0 6163
130 6163 return;
131 },
132 833 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
133 833 ▶ $derived->fail($_[0]) if defined $derived;
Branch: 1 branch
BranchTrueFalseTotal
if defined $derived 833 0 833
134 833 return;
135 },
136 7051 );
137 7051 return $derived;
138 }
139  
140 # The Futures a needs_all aggregate is made of that are still pending, as
141 # Future.pm answers it for a convergent Future. Like Future.pm it refuses any
142 # other Future.
143 36 ▶sub pending_futures {
Subroutine pending_futures: called CC=2 SCAR=6.9
Pod undocumented
144 36 my ($self) = @_;
145 DBIO::Exception->throw('Cannot call ->pending_futures on a non-convergent Future')
146 36 ▶ unless $self->{subs};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"subs"} 34 2 36
147 2 return grep { !$_->is_ready } @{ $self->{subs} };
148 }
149  
150 195 ▶sub needs_all {
Subroutine needs_all: called CC=3 SCAR=11.1
Pod undocumented
151 195 my ($class, @futures) = @_;
152 195 my $aggregate = $class->new;
153 195 $aggregate->{subs} = [ @futures ];
154 195 ▶ return $aggregate->done unless @futures;
Branch: 1 branch
BranchTrueFalseTotal
unless @futures 18 177 195
155  
156 177 my $remaining = scalar @futures;
157 177 my @results;
158 177 for my $index (0 .. $#futures) {
159 $futures[$index]->then(
160 182 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
161 182 ▶ return unless defined $aggregate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aggregate 0 182 182
162 182 $results[$index] = [ @_ ];
163 182 ▶ unless (--$remaining) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$remaining) 154 28 182
164 154 ▶ $aggregate->done(map { @$_ } @results)
Branch: 1 branch
BranchTrueFalseTotal
unless $aggregate->is_ready 154 0 154
165 unless $aggregate->is_ready;
166 }
167 182 return;
168 },
169 3 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
170 3 ▶ return unless defined $aggregate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aggregate 0 3 3
171 3 ▶ $aggregate->fail($_[0]) unless $aggregate->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $aggregate->is_ready 3 0 3
172 3 return;
173 },
174 225 );
175 }
176 177 return $aggregate;
177 }
178  
179 116 1;
180  
181 __END__
« blib/lib/DBIO/ResultSet/Pager.pm ↑ Summary blib/lib/DBIO/DeploymentHandler/VersionStorage/Standard/Component.pm »